1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\TestUtilities; |
4
|
|
|
|
5
|
|
|
use FilesystemIterator; |
6
|
|
|
use PHPUnit\Framework\TestCase as PHPUnitTestCase; |
7
|
|
|
use React\EventLoop\Factory; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use React\Promise\PromiseInterface; |
10
|
|
|
use RecursiveDirectoryIterator; |
11
|
|
|
use RecursiveIteratorIterator; |
12
|
|
|
use function Clue\React\Block\await; |
13
|
|
|
use function Clue\React\Block\awaitAll; |
14
|
|
|
use function Clue\React\Block\awaitAny; |
15
|
|
|
|
16
|
|
|
abstract class TestCase extends PHPUnitTestCase |
17
|
|
|
{ |
18
|
|
|
const DEFAULT_AWAIT_TIMEOUT = 60; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $baseTmpDir; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $tmpDir; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $tmpNamespace; |
34
|
|
|
|
35
|
84 |
|
protected function setUp(): void |
36
|
|
|
{ |
37
|
84 |
|
parent::setUp(); |
38
|
|
|
|
39
|
84 |
|
$this->baseTmpDir = $this->getSysTempDir() . |
40
|
84 |
|
DIRECTORY_SEPARATOR . |
41
|
84 |
|
'p-a-c-t-' . |
42
|
84 |
|
\uniqid() . |
43
|
84 |
|
DIRECTORY_SEPARATOR; |
44
|
84 |
|
$this->tmpDir = $this->baseTmpDir . |
45
|
84 |
|
\uniqid() . |
46
|
84 |
|
DIRECTORY_SEPARATOR; |
47
|
|
|
; |
48
|
|
|
|
49
|
84 |
|
$this->tmpNamespace = \uniqid('PACTN'); |
50
|
84 |
|
} |
51
|
|
|
|
52
|
84 |
|
protected function tearDown(): void |
53
|
|
|
{ |
54
|
84 |
|
parent::tearDown(); |
55
|
|
|
|
56
|
84 |
|
if (\file_exists($this->baseTmpDir)) { |
57
|
68 |
|
$this->rmdir($this->baseTmpDir); |
58
|
|
|
} |
59
|
84 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function provideTrueFalse(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
[ |
68
|
|
|
true, |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
false, |
72
|
|
|
], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
84 |
|
protected function getSysTempDir(): string |
80
|
|
|
{ |
81
|
84 |
|
if (\strtoupper(\substr(PHP_OS, 0, 3)) === 'WIN') { |
82
|
|
|
return 'C:\\t\\'; |
83
|
|
|
} |
84
|
|
|
|
85
|
84 |
|
return \sys_get_temp_dir(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $dir |
90
|
|
|
*/ |
91
|
68 |
|
protected function rmdir(string $dir): void |
92
|
|
|
{ |
93
|
68 |
|
$directory = new FilesystemIterator($dir); |
94
|
|
|
|
95
|
68 |
|
foreach ($directory as $node) { |
96
|
68 |
|
if (\is_dir($node->getPathname())) { |
97
|
68 |
|
$this->rmdir($node->getPathname()); |
98
|
68 |
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
67 |
|
if (\is_file($node->getPathname())) { |
102
|
67 |
|
\unlink($node->getPathname()); |
103
|
67 |
|
continue; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
68 |
|
\rmdir($dir); |
108
|
68 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
68 |
|
protected function getTmpDir(): string |
114
|
|
|
{ |
115
|
68 |
|
if (!\file_exists($this->tmpDir)) { |
116
|
68 |
|
\mkdir($this->tmpDir, 0777, true); |
117
|
|
|
} |
118
|
|
|
|
119
|
68 |
|
return $this->tmpDir; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
67 |
|
protected function getRandomNameSpace(): string |
126
|
|
|
{ |
127
|
67 |
|
return $this->tmpNamespace; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $path |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
67 |
|
protected function getFilesInDirectory(string $path): array |
135
|
|
|
{ |
136
|
67 |
|
$files = []; |
137
|
|
|
|
138
|
67 |
|
$directory = new RecursiveDirectoryIterator($path); |
139
|
67 |
|
$directory = new RecursiveIteratorIterator($directory); |
140
|
|
|
|
141
|
67 |
|
foreach ($directory as $node) { |
142
|
67 |
|
if (!\is_file($node->getPathname())) { |
143
|
67 |
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
67 |
|
$files[] = $node->getPathname(); |
147
|
|
|
} |
148
|
|
|
|
149
|
67 |
|
return $files; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param PromiseInterface $promise |
154
|
|
|
* @param LoopInterface|null $loop |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
6 |
|
protected function await(PromiseInterface $promise, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
158
|
|
|
{ |
159
|
6 |
|
if (!($loop instanceof LoopInterface)) { |
160
|
2 |
|
$loop = Factory::create(); |
161
|
|
|
} |
162
|
|
|
|
163
|
6 |
|
return await($promise, $loop, $timeout); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $promises |
168
|
|
|
* @param LoopInterface|null $loop |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
3 |
View Code Duplication |
protected function awaitAll(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
|
|
|
|
172
|
|
|
{ |
173
|
3 |
|
if (!($loop instanceof LoopInterface)) { |
174
|
1 |
|
$loop = Factory::create(); |
175
|
|
|
} |
176
|
|
|
|
177
|
3 |
|
return awaitAll($promises, $loop, $timeout); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $promises |
182
|
|
|
* @param LoopInterface|null $loop |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
3 |
View Code Duplication |
protected function awaitAny(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
|
|
|
|
186
|
|
|
{ |
187
|
3 |
|
if (!($loop instanceof LoopInterface)) { |
188
|
1 |
|
$loop = Factory::create(); |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
return awaitAny($promises, $loop, $timeout); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.