-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestData.php
74 lines (59 loc) · 1.96 KB
/
TestData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Utils;
use DOMDocument;
use LastDragon_ru\LaraASP\Testing\Exceptions\InvalidArgumentClass;
use ReflectionClass;
use RuntimeException;
use SplFileInfo;
use function basename;
use function dirname;
use function mb_ltrim;
use function sprintf;
use function str_replace;
use function str_starts_with;
/**
* Small helper to load data associated with test.
*/
class TestData {
private string $path;
/**
* @param class-string $test
*/
public function __construct(string $test) {
$path = (new ReflectionClass($test))->getFileName();
if ($path === false) {
throw new InvalidArgumentClass('$test', $test);
}
$this->path = $path;
}
public function path(string $path): string {
$dir = dirname(str_replace('\\', '/', $this->path));
$name = basename($this->path, '.php');
$path = str_replace('\\', '/', $path);
$path = (str_starts_with($path, '.') && !str_starts_with($path, './') && !str_starts_with($path, '../')) || str_starts_with($path, '~')
? $path
: '/'.mb_ltrim($path, '/');
$path = "{$dir}/{$name}{$path}";
return $path;
}
public function file(string $path): SplFileInfo {
return new SplFileInfo($this->path($path));
}
public function content(string $path): string {
return Args::content($this->file($path));
}
/**
* @return array<array-key, mixed>|string|int|float|bool|null
*/
public function json(string $path = '.json'): array|string|int|float|bool|null {
return Args::getJson($this->file($path), true);
}
public function dom(string $path = '.xml'): DOMDocument {
$dom = new DOMDocument();
$path = $this->path($path);
if (!$dom->load($path)) {
throw new RuntimeException(sprintf('Failed to load XML from `%s`', $path));
}
return $dom;
}
}