PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / exec-with-fallback / tests / BaseTest.php

BaseTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/exec-with-fallback/tests/BaseTest.php

277 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ExecWithFallback\Tests;
3
4 use PHPUnit\Framework\TestCase;
5
6 class BaseTest extends TestCase
7 {
8 public $className = '';
9 public $supportsResultCode = true;
10
11 public function checkAvailability()
12 {
13 $this->assertEquals(0, 0); // to awoid warnings about risky tests
14 return $this->isAvailable();
15 }
16
17 public function isAvailable()
18 {
19 $this->assertEquals(0, 0);
20 return function_exists('exec');
21 }
22
23 public function runExec($command, &$output = null, &$result_code = null)
24 {
25 return exec($command, $output, $result_code);
26 }
27
28
29 /*
30 Doesn't work with ProcOpen
31 public function testDevNull()
32 {
33 $output = [];
34 $execResult = $this->runExec('ls 1>/dev/null', $output, $result_code);
35 $this->assertEquals(0, $result_code);
36 $this->assertEquals(0, count($output));
37 $this->assertEquals('', $execResult);
38 }
39 */
40
41
42 public function testNoReturnCodeSupplied()
43 {
44 if ($this->checkAvailability()) {
45 $output = [];
46 $execResult = $this->runExec('echo hi', $output);
47
48 $this->assertEquals('hi', $execResult);
49 }
50 }
51
52 public function testNoOutputSupplied()
53 {
54 if ($this->checkAvailability()) {
55 $result = $this->runExec('echo hi');
56 $this->assertEquals('hi', $result);
57 }
58 }
59
60 /*
61 TODO: enable this test in PHP 8
62 public function testNoOutputSuppliedButReturnCodeIs()
63 {
64 if ($this->checkAvailability()) {
65 //$result = $this->runExec('echo hi', null, $result_code);
66 //$result = exec('echo hi', null, $result_code);
67 // https://stackoverflow.com/questions/1066625/how-would-i-skip-optional-arguments-in-a-function-call
68 //$result = exec(command:'echo hi', result_code:$result_code);
69 $result = $this->runExec(command:'echo hi', result_code:$result_code);
70
71 $this->assertEquals('hi', $result);
72 $this->assertSame(0, $result_code);
73 }
74 }
75 */
76
77 public function testTwoLines()
78 {
79 if ($this->checkAvailability()) {
80 if ($this->supportsResultCode) {
81 $result = $this->runExec('echo hi && echo world', $output, $result_code);
82 $this->assertEquals(0, $result_code);
83 } else {
84 $result = $this->runExec('echo hi && echo world', $output);
85 }
86 $this->assertEquals('world', $result);
87 $this->assertEquals(count($output), 2, print_r($output, true));
88 $this->assertEquals('hi', trim($output[0])); // "hi " on windows, using exec()
89 $this->assertEquals('world', $output[1]);
90 }
91 }
92
93 public function testWhiteSpace()
94 {
95 if ($this->checkAvailability()) {
96 if ($this->supportsResultCode) {
97 $result = $this->runExec('echo " hi " && echo " world "', $output, $result_code);
98 } else {
99 $result = $this->runExec('echo " hi " && echo " world "', $output);
100 }
101 $this->assertThat(
102 $result,
103 $this->logicalOr(
104 $this->identicalTo(' world'), // Linux
105 $this->identicalTo('" world "') // Windows
106 )
107 );
108 $this->assertThat(
109 $output,
110 $this->logicalOr(
111 $this->equalTo([' hi', ' world']), // Linux
112 $this->identicalTo(['" hi "', '" world "']) // Windows
113 )
114 );
115 }
116 }
117
118 public function testNoOutput()
119 {
120 if ($this->checkAvailability()) {
121 $result = $this->runExec('echo hi 1>/dev/null', $output);
122 $this->assertSame('', $result);
123 }
124 }
125
126 public function testStdErrPipedToStdOut()
127 {
128 if ($this->checkAvailability()) {
129 $result = $this->runExec('echo hi && aoeuaoeuaoeu 2>&1', $output);
130 $this->assertTrue(count($output) > 1);
131 }
132 }
133
134 public function testOutputIsInt()
135 {
136 if ($this->checkAvailability()) {
137 $output = 10;
138
139 if ($this->supportsResultCode) {
140 $result = $this->runExec('echo hi', $output, $result_code);
141 $this->assertEquals(0, $result_code);
142 } else {
143 $result = $this->runExec('echo hi', $output);
144 }
145 $this->assertSame('hi', $result);
146 $this->assertEquals('array', gettype($output));
147 $this->assertSame($output[0], 'hi');
148 }
149 }
150
151 public function testOutputIsString()
152 {
153 if ($this->checkAvailability()) {
154 $output = 'abc';
155 if ($this->supportsResultCode) {
156 $result = $this->runExec('echo hi', $output, $result_code);
157 $this->assertEquals(0, $result_code);
158 } else {
159 $result = $this->runExec('echo hi', $output);
160 }
161 $this->assertEquals('hi', $result);
162 $this->assertEquals('array', gettype($output));
163 $this->assertEquals('hi', $output[0]);
164 }
165 }
166
167 public function testOutputIsExistingArray()
168 {
169 if ($this->checkAvailability()) {
170 $output = ['abc'];
171
172 if ($this->supportsResultCode) {
173 $result = $this->runExec('echo hi', $output, $result_code);
174 $this->assertEquals(0, $result_code);
175 } else {
176 $result = $this->runExec('echo hi', $output);
177 }
178 $this->assertEquals('hi', $result);
179 $this->assertEquals('array', gettype($output));
180 $this->assertEquals('abc', $output[0]);
181 $this->assertEquals('hi', $output[1]);
182 }
183 }
184
185 public function testUnknownCommand()
186 {
187 if ($this->checkAvailability()) {
188 if ($this->supportsResultCode) {
189 $result = $this->runExec('aoebuaoeu', $output, $result_code);
190 $this->assertNotEquals(0, $result_code); // 127 on linux, 1 on windows
191 } else {
192 $result = $this->runExec('aoebuaoeu', $output);
193 }
194 $this->assertEquals('', $result);
195 }
196 }
197
198 public function testUnknownCommand2()
199 {
200 if ($this->checkAvailability()) {
201 if ($this->supportsResultCode) {
202 $result = $this->runExec('aoebuaoeu 2>&1', $output, $result_code);
203 $this->assertNotEquals(0, $result_code); // 127 on linux, 1 on windows
204 } else {
205 $result = $this->runExec('aoebuaoeu 2>&1', $output);
206 }
207 $this->assertSame('string', gettype($result));
208 $this->assertTrue(strlen($result) > 0);
209 }
210 }
211
212 public function testIsAvailable()
213 {
214 // The purpose of this test is merely to inform
215 //echo $this->className . function_exists('proc_open') ? 'yes' : 'no';
216 if ($this->isAvailable()) {
217 } else {
218 echo "\nNote: " . ($this->className ? $this->className : 'exec()') . ' is not tested, as it is unavailable';
219 }
220 $this->assertEquals(0, 0);
221
222 }
223
224 public function testWhenUnavailable()
225 {
226 if ($this->isAvailable()) {
227 $this->assertEquals(0, 0);
228 } else {
229 $hasThrown = false;
230 //$result = $this->runExec('echo "hi"', $output, $result_code);
231 try {
232 $result = $this->runExec('echo hi', $output, $result_code);
233 } catch (\Exception $e) {
234 $hasThrown = true;
235 } catch (\Throwable $e) {
236 $hasThrown = true;
237 }
238 $this->assertEquals(true, $hasThrown, "Expected it to throw when unavailable");
239 }
240
241 }
242
243 /*
244 // test wait script (only locally available...)
245 public function testWaitScript()
246 {
247 $result = $this->runExec('wait.sh 0.1 0', $output, $result_code);
248 $this->assertEquals(0, $result_code, 'wait.sh command did not return with exit code 0');
249 $this->assertEquals('Sleep for 0.1 seconds', $output[0]);
250 $this->assertEquals('OK', $output[1]);
251 }
252
253 // test wait script (only locally available...)
254 public function testWaitScriptReturnCode2()
255 {
256 $result = $this->runExec('wait.sh 0.1 2', $output, $result_code);
257 $this->assertEquals(2, $result_code, 'wait.sh command did not return with exit code 2');
258 $this->assertEquals('NOT OK', $output[1]);
259 }
260
261 public function testWaitScriptLongWait()
262 {
263 $result = $this->runExec('wait.sh 5 0', $output, $result_code);
264 $this->assertEquals(0, $result_code, 'wait.sh command did not return with exit code 0');
265 $this->assertEquals('OK', $output[1]);
266 }
267 */
268 /*
269 public function all()
270 {
271 $this->devNull($className);
272 $this->noReturnCodeSupplied($className);
273 $this->noOutputSupplied($className);
274 }
275 */
276 }
277