1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WyriHaximus\TestUtilities; |
6
|
|
|
|
7
|
|
|
use FilesystemIterator; |
8
|
|
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
9
|
|
|
use PHPUnit\Framework\TestCase as PHPUnitTestCase; |
10
|
|
|
use RecursiveDirectoryIterator; |
11
|
|
|
use RecursiveIteratorIterator; |
12
|
|
|
use SplFileInfo; |
13
|
|
|
|
14
|
|
|
use function assert; |
15
|
|
|
use function file_exists; |
16
|
|
|
use function is_dir; |
17
|
|
|
use function is_file; |
18
|
|
|
use function Safe\mkdir; |
19
|
|
|
use function Safe\rmdir; |
20
|
|
|
use function Safe\unlink; |
21
|
|
|
use function strtoupper; |
22
|
|
|
use function substr; |
23
|
|
|
use function sys_get_temp_dir; |
24
|
|
|
use function time; |
25
|
|
|
use function uniqid; |
26
|
|
|
use function usleep; |
27
|
|
|
|
28
|
|
|
use const DIRECTORY_SEPARATOR; |
29
|
|
|
use const PHP_OS; |
30
|
|
|
|
31
|
|
|
abstract class TestCase extends PHPUnitTestCase |
32
|
|
|
{ |
33
|
|
|
use MockeryPHPUnitIntegration; |
34
|
|
|
|
35
|
|
|
public const string WINDOWS_TEMP_DIR_PREFIX = 'C:\\t\\'; |
|
|
|
|
36
|
|
|
public const int DEFAULT_AWAIT_TIMEOUT = 60; |
37
|
74 |
|
public const int WIN_START = 0; |
38
|
|
|
public const int WIN_END = 2; |
39
|
74 |
|
public const int USLEEP = 50; |
40
|
74 |
|
public const int DEFAULT_MODE = 0777; |
41
|
74 |
|
|
42
|
74 |
|
private string $baseTmpDir; |
43
|
74 |
|
|
44
|
74 |
|
private string $tmpDir; |
45
|
74 |
|
|
46
|
74 |
|
private string $tmpNamespace; |
47
|
|
|
|
48
|
|
|
protected function setUp(): void |
49
|
74 |
|
{ |
50
|
74 |
|
$this->baseTmpDir = $this->getSysTempDir() . |
51
|
|
|
DIRECTORY_SEPARATOR . |
52
|
74 |
|
'w-h-p-t-u-' . |
53
|
|
|
uniqid() . |
54
|
74 |
|
DIRECTORY_SEPARATOR; |
55
|
68 |
|
$this->tmpDir = $this->baseTmpDir . |
56
|
|
|
uniqid() . |
57
|
74 |
|
DIRECTORY_SEPARATOR; |
58
|
|
|
|
59
|
|
|
$this->tmpNamespace = uniqid('WHPTU'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function tearDown(): void |
63
|
|
|
{ |
64
|
|
|
if (! file_exists($this->baseTmpDir)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->rmdir($this->baseTmpDir); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @return array<int, array<int, bool>> */ |
72
|
|
|
final public static function provideTrueFalse(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
[true], |
76
|
|
|
[false], |
77
|
74 |
|
]; |
78
|
|
|
} |
79
|
74 |
|
|
80
|
|
|
final protected function getSysTempDir(): string |
81
|
|
|
{ |
82
|
|
|
if (strtoupper(substr(PHP_OS, self::WIN_START, self::WIN_END)) === 'WIN') { |
83
|
74 |
|
return self::WINDOWS_TEMP_DIR_PREFIX; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return sys_get_temp_dir(); |
87
|
|
|
} |
88
|
|
|
|
89
|
69 |
|
final protected function rmdir(string $dir): void |
90
|
|
|
{ |
91
|
69 |
|
$directory = new FilesystemIterator($dir); |
92
|
|
|
|
93
|
|
|
foreach ($directory as $node) { |
94
|
69 |
|
assert($node instanceof SplFileInfo); |
95
|
68 |
|
if (is_dir($node->getPathname())) { |
96
|
68 |
|
$this->rmdir($node->getPathname()); |
97
|
68 |
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
67 |
|
if (! is_file($node->getPathname())) { |
101
|
67 |
|
continue; |
102
|
67 |
|
} |
103
|
|
|
|
104
|
|
|
unlink($node->getPathname()); |
105
|
|
|
} |
106
|
69 |
|
|
107
|
69 |
|
rmdir($dir); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
final protected function getTmpDir(): string |
111
|
|
|
{ |
112
|
68 |
|
if (! file_exists($this->tmpDir)) { |
113
|
|
|
mkdir($this->tmpDir, self::DEFAULT_MODE, true); |
114
|
68 |
|
} |
115
|
68 |
|
|
116
|
|
|
return $this->tmpDir; |
117
|
|
|
} |
118
|
68 |
|
|
119
|
|
|
final protected function getRandomNameSpace(): string |
120
|
|
|
{ |
121
|
|
|
return $this->tmpNamespace; |
122
|
|
|
} |
123
|
|
|
|
124
|
67 |
|
/** @return array<string> */ |
125
|
|
|
final protected function getFilesInDirectory(string $path): array |
126
|
67 |
|
{ |
127
|
|
|
$files = []; |
128
|
|
|
|
129
|
|
|
/** @var iterable<SplFileInfo> $directory */ |
130
|
|
|
$directory = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
131
|
|
|
|
132
|
|
|
foreach ($directory as $node) { |
133
|
67 |
|
if (! is_file($node->getPathname())) { |
134
|
|
|
continue; |
135
|
67 |
|
} |
136
|
|
|
|
137
|
67 |
|
$files[] = $node->getPathname(); |
138
|
67 |
|
} |
139
|
|
|
|
140
|
67 |
|
return $files; |
141
|
67 |
|
} |
142
|
67 |
|
|
143
|
|
|
final protected static function waitUntilTheNextSecond(): void |
144
|
|
|
{ |
145
|
67 |
|
$now = time(); |
146
|
|
|
do { |
147
|
|
|
// @codeCoverageIgnoreStart |
148
|
67 |
|
usleep(self::USLEEP); |
149
|
|
|
// @codeCoverageIgnoreEnd |
150
|
|
|
} while ($now === time()); |
151
|
1 |
|
} |
152
|
|
|
} |
153
|
|
|
|