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 = $this->getSysTempDir() . |
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
|
|
|
protected function getSysTempDir() |
52
|
|
|
{ |
53
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
54
|
|
|
return 'C:\\t\\'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return sys_get_temp_dir(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function tearDown() |
61
|
|
|
{ |
62
|
|
|
parent::tearDown(); |
63
|
|
|
$this->rmdir($this->baseTmpDir); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function rmdir($dir) |
67
|
|
|
{ |
68
|
|
|
$directory = new FilesystemIterator($dir); |
69
|
|
|
|
70
|
|
|
foreach ($directory as $node) { |
71
|
|
|
if (is_dir($node->getPathname())) { |
72
|
|
|
$this->rmdir($node->getPathname()); |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_file($node->getPathname())) { |
77
|
|
|
unlink($node->getPathname()); |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
rmdir($dir); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getTmpDir(): string |
86
|
|
|
{ |
87
|
|
|
return $this->tmpDir; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function getRandomNameSpace(): string |
91
|
|
|
{ |
92
|
|
|
return $this->tmpNamespace; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getFilesInDirectory(string $path): array |
96
|
|
|
{ |
97
|
|
|
$files = []; |
98
|
|
|
|
99
|
|
|
$directory = new RecursiveDirectoryIterator($path); |
100
|
|
|
$directory = new RecursiveIteratorIterator($directory); |
101
|
|
|
|
102
|
|
|
foreach ($directory as $node) { |
103
|
|
|
if (!is_file($node->getPathname())) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$files[] = $node->getPathname(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $files; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function provideTrueFalse(): array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
[ |
117
|
|
|
true, |
118
|
|
|
], |
119
|
|
|
[ |
120
|
|
|
false, |
121
|
|
|
], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function await(PromiseInterface $promise, LoopInterface $loop = null) |
126
|
|
|
{ |
127
|
|
|
if (!($loop instanceof LoopInterface)) { |
128
|
|
|
$loop = Factory::create(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return await($promise, $loop); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function awaitAll(array $promises, LoopInterface $loop = null) |
135
|
|
|
{ |
136
|
|
|
if (!($loop instanceof LoopInterface)) { |
137
|
|
|
$loop = Factory::create(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return awaitAll($promises, $loop); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function awaitAny(array $promises, LoopInterface $loop = null) |
144
|
|
|
{ |
145
|
|
|
if (!($loop instanceof LoopInterface)) { |
146
|
|
|
$loop = Factory::create(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return awaitAny($promises, $loop); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|