1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Helper; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Api; |
19
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class TestHelper |
23
|
|
|
* @package phpFastCache\Helper |
24
|
|
|
*/ |
25
|
|
|
class TestHelper |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $exitCode = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TestHelper constructor. |
34
|
|
|
* @param $testName |
35
|
|
|
*/ |
36
|
|
|
public function __construct($testName) |
37
|
|
|
{ |
38
|
|
|
$this->printText('[PhpFastCache CORE v' . Api::getPhpFastCacheVersion() . Api::getPhpFastCacheGitHeadHash() . ']', true); |
39
|
|
|
$this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true); |
40
|
|
|
$this->printText('[PHP v' . PHP_VERSION . ']', true); |
41
|
|
|
$this->printText("[Begin Test: '{$testName}']"); |
42
|
|
|
$this->printText('---'); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Catch all uncaught exception |
46
|
|
|
* to our own exception handler |
47
|
|
|
*/ |
48
|
|
|
set_exception_handler([$this, 'exceptionHandler']); |
49
|
|
|
set_error_handler([$this, 'errorHandler']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function getExitCode(): int |
56
|
|
|
{ |
57
|
|
|
return $this->exitCode; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function resetExitCode(): self |
64
|
|
|
{ |
65
|
|
|
$this->exitCode = 0; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $string |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function printSkipText($string): self |
75
|
|
|
{ |
76
|
|
|
$this->printText("[SKIP] {$string}"); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $string |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function printPassText($string): self |
86
|
|
|
{ |
87
|
|
|
$this->printText("[PASS] {$string}"); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $string |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function printInfoText($string): self |
97
|
|
|
{ |
98
|
|
|
$this->printText("[INFO] {$string}"); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $string |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function printDebugText($string): self |
108
|
|
|
{ |
109
|
|
|
$this->printText("[DEBUG] {$string}"); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $string |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function printFailText($string): self |
119
|
|
|
{ |
120
|
|
|
$this->printText("[FAIL] {$string}"); |
121
|
|
|
$this->exitCode = 1; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param int $count |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function printNewLine($count = 1): self |
131
|
|
|
{ |
132
|
|
|
for ($i = 0; $i < $count; $i++) { |
133
|
|
|
print PHP_EOL; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $string |
141
|
|
|
* @param bool $strtoupper |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function printText($string, $strtoupper = false): self |
145
|
|
|
{ |
146
|
|
|
if (!$strtoupper) { |
147
|
|
|
print \trim($string) . PHP_EOL; |
148
|
|
|
} else { |
149
|
|
|
print strtoupper(\trim($string) . PHP_EOL); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $cmd |
157
|
|
|
*/ |
158
|
|
|
public function runAsyncProcess($cmd) |
159
|
|
|
{ |
160
|
|
|
if (\substr(php_uname(), 0, 7) === 'Windows') { |
161
|
|
|
pclose(popen('start /B ' . $cmd, 'r')); |
|
|
|
|
162
|
|
|
} else { |
163
|
|
|
exec($cmd . ' > /dev/null &'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $file |
169
|
|
|
* @param string $ext |
170
|
|
|
*/ |
171
|
|
|
public function runSubProcess($file, $ext = '.php') |
172
|
|
|
{ |
173
|
|
|
$this->runAsyncProcess(($this->isHHVM() ? 'hhvm ' : 'php ') . getcwd() . DIRECTORY_SEPARATOR . 'subprocess' . DIRECTORY_SEPARATOR . $file . '.subprocess' . $ext); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function terminateTest() |
180
|
|
|
{ |
181
|
|
|
exit($this->exitCode); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function isHHVM(): bool |
188
|
|
|
{ |
189
|
|
|
return \defined('HHVM_VERSION'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $obj |
194
|
|
|
* @param $prop |
195
|
|
|
* @return mixed |
196
|
|
|
* @throws \ReflectionException |
197
|
|
|
*/ |
198
|
|
|
public function accessInaccessibleMember($obj, $prop) |
199
|
|
|
{ |
200
|
|
|
$reflection = new \ReflectionClass($obj); |
201
|
|
|
$property = $reflection->getProperty($prop); |
202
|
|
|
$property->setAccessible(true); |
203
|
|
|
return $property->getValue($obj); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param \Throwable $exception |
208
|
|
|
*/ |
209
|
|
|
public function exceptionHandler(\Throwable $exception) |
210
|
|
|
{ |
211
|
|
|
if ($exception instanceof PhpfastcacheDriverCheckException) { |
212
|
|
|
$this->printSkipText('A driver could not be initialized due to missing requirement: ' . $exception->getMessage()); |
213
|
|
|
} else { |
214
|
|
|
$this->printFailText(sprintf( |
215
|
|
|
'Uncaught exception "%s" in "%s" line %d with message: "%s"', |
216
|
|
|
\get_class($exception), |
217
|
|
|
$exception->getFile(), |
218
|
|
|
$exception->getLine(), |
219
|
|
|
$exception->getMessage() |
220
|
|
|
)); |
221
|
|
|
} |
222
|
|
|
$this->terminateTest(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param int $errno |
227
|
|
|
* @param string $errstr |
228
|
|
|
* @param string $errfile |
229
|
|
|
* @param int $errline |
230
|
|
|
*/ |
231
|
|
|
public function errorHandler(int $errno, string $errstr, string $errfile, int $errline) |
232
|
|
|
{ |
233
|
|
|
$errorType = ''; |
234
|
|
|
|
235
|
|
|
switch ($errno) { |
236
|
|
|
case E_PARSE: |
237
|
|
|
case E_ERROR: |
238
|
|
|
case E_CORE_ERROR: |
239
|
|
|
case E_COMPILE_ERROR: |
240
|
|
|
case E_USER_ERROR: |
241
|
|
|
$errorType = '[FATAL ERROR]'; |
242
|
|
|
break; |
243
|
|
|
case E_WARNING: |
244
|
|
|
case E_USER_WARNING: |
245
|
|
|
case E_COMPILE_WARNING: |
246
|
|
|
case E_RECOVERABLE_ERROR: |
247
|
|
|
$errorType = '[WARNING]'; |
248
|
|
|
break; |
249
|
|
|
case E_NOTICE: |
250
|
|
|
case E_USER_NOTICE: |
251
|
|
|
$errorType = '[NOTICE]'; |
252
|
|
|
break; |
253
|
|
|
case E_STRICT: |
254
|
|
|
$errorType = '[STRICT]'; |
255
|
|
|
break; |
256
|
|
|
case E_DEPRECATED: |
257
|
|
|
case E_USER_DEPRECATED: |
258
|
|
|
$errorType = '[DEPRECATED]'; |
259
|
|
|
break; |
260
|
|
|
default : |
|
|
|
|
261
|
|
|
break; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if ($errorType === '[FATAL ERROR]') { |
265
|
|
|
$this->printFailText(sprintf( |
266
|
|
|
"A critical error has been caught: \"%s\" in %s line %d", |
267
|
|
|
"$errorType $errstr", |
268
|
|
|
$errfile, |
269
|
|
|
$errline |
270
|
|
|
)); |
271
|
|
|
} else { |
272
|
|
|
$this->printDebugText(sprintf( |
273
|
|
|
"A non-critical error has been caught: \"%s\" in %s line %d", |
274
|
|
|
"$errorType $errstr", |
275
|
|
|
$errfile, |
276
|
|
|
$errline |
277
|
|
|
)); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |