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
|
|
|
namespace phpFastCache\Helper; |
15
|
|
|
|
16
|
|
|
use phpFastCache\Api; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TestHelper |
20
|
|
|
* @package phpFastCache\Helper |
21
|
|
|
*/ |
22
|
|
|
class TestHelper |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $exitCode = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* TestHelper constructor. |
31
|
|
|
* @param $testName |
32
|
|
|
*/ |
33
|
|
|
public function __construct($testName) |
34
|
|
|
{ |
35
|
|
|
$this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true); |
36
|
|
|
$this->printText("[Begin Test: '{$testName}']"); |
37
|
|
|
$this->printText('---'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
|
|
public function getExitCode() |
44
|
|
|
{ |
45
|
|
|
return $this->exitCode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function resetExitCode() |
52
|
|
|
{ |
53
|
|
|
$this->exitCode = 0; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $string |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function printSkipText($string) |
63
|
|
|
{ |
64
|
|
|
$this->printText("[SKIP] {$string}"); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $string |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function printPassText($string) |
74
|
|
|
{ |
75
|
|
|
$this->printText("[PASS] {$string}"); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $string |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function printFailText($string) |
85
|
|
|
{ |
86
|
|
|
$this->printText("[FAIL] {$string}"); |
87
|
|
|
$this->exitCode = 1; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $count |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function printNewLine($count = 1) |
97
|
|
|
{ |
98
|
|
|
for($i = 0; $i < $count; $i++){ |
99
|
|
|
print PHP_EOL; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $string |
107
|
|
|
* @param bool $strtoupper |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function printText($string, $strtoupper = false) |
111
|
|
|
{ |
112
|
|
|
if(!$strtoupper){ |
113
|
|
|
print trim($string) . PHP_EOL; |
114
|
|
|
}else{ |
115
|
|
|
print strtoupper(trim($string) . PHP_EOL); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $cmd |
123
|
|
|
*/ |
124
|
|
|
public function runAsyncProcess($cmd) |
125
|
|
|
{ |
126
|
|
|
if (substr(php_uname(), 0, 7) === 'Windows'){ |
127
|
|
|
pclose(popen('start /B '. $cmd, 'r')); |
128
|
|
|
} |
129
|
|
|
else { |
130
|
|
|
exec($cmd . ' > /dev/null &'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $file |
136
|
|
|
* @param string $ext |
137
|
|
|
*/ |
138
|
|
|
public function runSubProcess($file, $ext = '.php') |
139
|
|
|
{ |
140
|
|
|
$this->runAsyncProcess('php ' . getcwd() . DIRECTORY_SEPARATOR . 'subprocess' . DIRECTORY_SEPARATOR . $file . '.subprocess' . $ext); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function terminateTest() |
147
|
|
|
{ |
148
|
|
|
exit($this->exitCode); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.