1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\TestUtilities; |
4
|
|
|
|
5
|
|
|
use FilesystemIterator; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
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 PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $baseTmpDir; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $tmpDir; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $tmpNamespace; |
32
|
|
|
|
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
parent::setUp(); |
36
|
|
|
|
37
|
|
|
$this->baseTmpDir = sys_get_temp_dir() . |
38
|
|
|
DIRECTORY_SEPARATOR . |
39
|
|
|
'php-api-clients-tests-' . |
40
|
|
|
uniqid() . |
41
|
|
|
DIRECTORY_SEPARATOR; |
42
|
|
|
$this->tmpDir = $this->baseTmpDir . |
43
|
|
|
uniqid() . |
44
|
|
|
DIRECTORY_SEPARATOR; |
45
|
|
|
; |
46
|
|
|
|
47
|
|
|
mkdir($this->tmpDir, 0777, true); |
48
|
|
|
$this->tmpNamespace = uniqid('PACTN'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function tearDown() |
52
|
|
|
{ |
53
|
|
|
parent::tearDown(); |
54
|
|
|
$this->rmdir($this->baseTmpDir); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function rmdir($dir) |
58
|
|
|
{ |
59
|
|
|
$directory = new FilesystemIterator($dir); |
60
|
|
|
|
61
|
|
|
foreach ($directory as $node) { |
62
|
|
|
if (is_dir($node->getPathname())) { |
63
|
|
|
$this->rmdir($node->getPathname()); |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (is_file($node->getPathname())) { |
68
|
|
|
unlink($node->getPathname()); |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
rmdir($dir); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getTmpDir(): string |
77
|
|
|
{ |
78
|
|
|
return $this->tmpDir; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getRandomNameSpace(): string |
82
|
|
|
{ |
83
|
|
|
return $this->tmpNamespace; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getFilesInDirectory(string $path): array |
87
|
|
|
{ |
88
|
|
|
$files = []; |
89
|
|
|
|
90
|
|
|
$directory = new RecursiveDirectoryIterator($path); |
91
|
|
|
$directory = new RecursiveIteratorIterator($directory); |
92
|
|
|
|
93
|
|
|
foreach ($directory as $node) { |
94
|
|
|
if (!is_file($node->getPathname())) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$files[] = $node->getPathname(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $files; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function provideTrueFalse(): array |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
[ |
108
|
|
|
true, |
109
|
|
|
], |
110
|
|
|
[ |
111
|
|
|
false, |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function await(PromiseInterface $promise, LoopInterface $loop = null) |
117
|
|
|
{ |
118
|
|
|
if (!($loop instanceof LoopInterface)) { |
119
|
|
|
$loop = Factory::create(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return await($promise, $loop); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function awaitAll(array $promises, LoopInterface $loop = null) |
126
|
|
|
{ |
127
|
|
|
if (!($loop instanceof LoopInterface)) { |
128
|
|
|
$loop = Factory::create(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return awaitAll($promises, $loop); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function awaitAny(array $promises, LoopInterface $loop = null) |
135
|
|
|
{ |
136
|
|
|
if (!($loop instanceof LoopInterface)) { |
137
|
|
|
$loop = Factory::create(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return awaitAny($promises, $loop); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|