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