comparison vendor/psy/psysh/test/ShellTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
23 private $streams = []; 23 private $streams = [];
24 24
25 public function tearDown() 25 public function tearDown()
26 { 26 {
27 foreach ($this->streams as $stream) { 27 foreach ($this->streams as $stream) {
28 fclose($stream); 28 \fclose($stream);
29 } 29 }
30 } 30 }
31 31
32 public function testScopeVariables() 32 public function testScopeVariables()
33 { 33 {
37 $__psysh__ = 'ignore this'; 37 $__psysh__ = 'ignore this';
38 $_ = 'ignore this'; 38 $_ = 'ignore this';
39 $_e = 'ignore this'; 39 $_e = 'ignore this';
40 40
41 $shell = new Shell($this->getConfig()); 41 $shell = new Shell($this->getConfig());
42 $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this')); 42 $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
43 43
44 $this->assertNotContains('__psysh__', $shell->getScopeVariableNames()); 44 $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
45 $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames()); 45 $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames());
46 $this->assertSame('banana', $shell->getScopeVariable('one')); 46 $this->assertSame('banana', $shell->getScopeVariable('one'));
47 $this->assertSame(123, $shell->getScopeVariable('two')); 47 $this->assertSame(123, $shell->getScopeVariable('two'));
48 $this->assertSame($three, $shell->getScopeVariable('three')); 48 $this->assertSame($three, $shell->getScopeVariable('three'));
49 $this->assertNull($shell->getScopeVariable('_')); 49 $this->assertNull($shell->getScopeVariable('_'));
50 50
51 $diff = $shell->getScopeVariablesDiff(['one' => $one, 'two' => 'not two']);
52 $this->assertSame(['two' => $two, 'three' => $three, '_' => null], $diff);
53
51 $shell->setScopeVariables([]); 54 $shell->setScopeVariables([]);
52 $this->assertSame(['_'], $shell->getScopeVariableNames()); 55 $this->assertSame(['_'], $shell->getScopeVariableNames());
53 56
54 $shell->setBoundObject($this); 57 $shell->setBoundObject($this);
55 $this->assertSame(['_', 'this'], $shell->getScopeVariableNames()); 58 $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
78 $_e = 'ignore this'; 81 $_e = 'ignore this';
79 82
80 $config = $this->getConfig(['usePcntl' => false]); 83 $config = $this->getConfig(['usePcntl' => false]);
81 84
82 $shell = new Shell($config); 85 $shell = new Shell($config);
83 $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this')); 86 $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
84 $shell->addInput('exit', true); 87 $shell->addInput('exit', true);
85 88
86 // This is super slow and we shouldn't do this :( 89 // This is super slow and we shouldn't do this :(
87 $shell->run(null, $this->getOutput()); 90 $shell->run(null, $this->getOutput());
88 91
158 161
159 $this->assertSame($e, $shell->getScopeVariable('_e')); 162 $this->assertSame($e, $shell->getScopeVariable('_e'));
160 $this->assertFalse($shell->hasCode()); 163 $this->assertFalse($shell->hasCode());
161 $this->assertEmpty($shell->getCodeBuffer()); 164 $this->assertEmpty($shell->getCodeBuffer());
162 165
163 rewind($stream); 166 \rewind($stream);
164 $streamContents = stream_get_contents($stream); 167 $streamContents = \stream_get_contents($stream);
165 168
166 $this->assertContains('PHP Parse error', $streamContents); 169 $this->assertContains('PHP Parse error', $streamContents);
167 $this->assertContains('message', $streamContents); 170 $this->assertContains('message', $streamContents);
168 $this->assertContains('line 13', $streamContents); 171 $this->assertContains('line 13', $streamContents);
169 } 172 }
173 $shell = new Shell($this->getConfig()); 176 $shell = new Shell($this->getConfig());
174 $output = $this->getOutput(); 177 $output = $this->getOutput();
175 $stream = $output->getStream(); 178 $stream = $output->getStream();
176 $shell->setOutput($output); 179 $shell->setOutput($output);
177 180
178 $oldLevel = error_reporting(); 181 $oldLevel = \error_reporting();
179 error_reporting($oldLevel & ~E_USER_NOTICE); 182 \error_reporting($oldLevel & ~E_USER_NOTICE);
180 183
181 try { 184 try {
182 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); 185 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
183 } catch (ErrorException $e) { 186 } catch (ErrorException $e) {
184 error_reporting($oldLevel); 187 \error_reporting($oldLevel);
185 $this->fail('Unexpected error exception'); 188 $this->fail('Unexpected error exception');
186 } 189 }
187 error_reporting($oldLevel); 190 \error_reporting($oldLevel);
188 191
189 rewind($stream); 192 \rewind($stream);
190 $streamContents = stream_get_contents($stream); 193 $streamContents = \stream_get_contents($stream);
191 194
192 $this->assertContains('PHP Notice:', $streamContents); 195 $this->assertContains('PHP Notice:', $streamContents);
193 $this->assertContains('wheee', $streamContents); 196 $this->assertContains('wheee', $streamContents);
194 $this->assertContains('line 13', $streamContents); 197 $this->assertContains('line 13', $streamContents);
195 } 198 }
198 * @expectedException \Psy\Exception\ErrorException 201 * @expectedException \Psy\Exception\ErrorException
199 */ 202 */
200 public function testNotHandlingErrors() 203 public function testNotHandlingErrors()
201 { 204 {
202 $shell = new Shell($this->getConfig()); 205 $shell = new Shell($this->getConfig());
203 $oldLevel = error_reporting(); 206 $oldLevel = \error_reporting();
204 error_reporting($oldLevel | E_USER_NOTICE); 207 \error_reporting($oldLevel | E_USER_NOTICE);
205 208
206 try { 209 try {
207 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); 210 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
208 } catch (ErrorException $e) { 211 } catch (ErrorException $e) {
209 error_reporting($oldLevel); 212 \error_reporting($oldLevel);
210 throw $e; 213 throw $e;
211 } 214 }
212 } 215 }
213 216
214 public function testVersion() 217 public function testVersion()
215 { 218 {
216 $shell = new Shell($this->getConfig()); 219 $shell = new Shell($this->getConfig());
217 220
218 $this->assertInstanceOf('Symfony\Component\Console\Application', $shell); 221 $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
219 $this->assertContains(Shell::VERSION, $shell->getVersion()); 222 $this->assertContains(Shell::VERSION, $shell->getVersion());
220 $this->assertContains(phpversion(), $shell->getVersion()); 223 $this->assertContains(PHP_VERSION, $shell->getVersion());
221 $this->assertContains(php_sapi_name(), $shell->getVersion()); 224 $this->assertContains(PHP_SAPI, $shell->getVersion());
222 } 225 }
223 226
224 public function testCodeBuffer() 227 public function testCodeBuffer()
225 { 228 {
226 $shell = new Shell($this->getConfig()); 229 $shell = new Shell($this->getConfig());
234 $this->assertTrue($shell->hasCode()); 237 $this->assertTrue($shell->hasCode());
235 238
236 $shell->addCode('{}'); 239 $shell->addCode('{}');
237 $code = $shell->flushCode(); 240 $code = $shell->flushCode();
238 $this->assertFalse($shell->hasCode()); 241 $this->assertFalse($shell->hasCode());
239 $code = preg_replace('/\s+/', ' ', $code); 242 $code = \preg_replace('/\s+/', ' ', $code);
240 $this->assertNotNull($code); 243 $this->assertNotNull($code);
241 $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code); 244 $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
242 } 245 }
243 246
244 public function testKeepCodeBufferOpen() 247 public function testKeepCodeBufferOpen()
254 $this->assertTrue($shell->hasCode()); 257 $this->assertTrue($shell->hasCode());
255 258
256 $shell->addCode('+ 1'); 259 $shell->addCode('+ 1');
257 $code = $shell->flushCode(); 260 $code = $shell->flushCode();
258 $this->assertFalse($shell->hasCode()); 261 $this->assertFalse($shell->hasCode());
259 $code = preg_replace('/\s+/', ' ', $code); 262 $code = \preg_replace('/\s+/', ' ', $code);
260 $this->assertNotNull($code); 263 $this->assertNotNull($code);
261 $this->assertSame('return 1 + 1 + 1;', $code); 264 $this->assertSame('return 1 + 1 + 1;', $code);
262 } 265 }
263 266
264 /** 267 /**
289 $shell = new Shell($this->getConfig()); 292 $shell = new Shell($this->getConfig());
290 $shell->setOutput($output); 293 $shell->setOutput($output);
291 294
292 $shell->writeStdout("{{stdout}}\n"); 295 $shell->writeStdout("{{stdout}}\n");
293 296
294 rewind($stream); 297 \rewind($stream);
295 $streamContents = stream_get_contents($stream); 298 $streamContents = \stream_get_contents($stream);
296 299
297 $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents); 300 $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
298 } 301 }
299 302
300 public function testWriteStdoutWithoutNewline() 303 public function testWriteStdoutWithoutNewline()
304 $shell = new Shell($this->getConfig()); 307 $shell = new Shell($this->getConfig());
305 $shell->setOutput($output); 308 $shell->setOutput($output);
306 309
307 $shell->writeStdout('{{stdout}}'); 310 $shell->writeStdout('{{stdout}}');
308 311
309 rewind($stream); 312 \rewind($stream);
310 $streamContents = stream_get_contents($stream); 313 $streamContents = \stream_get_contents($stream);
311 314
312 $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents); 315 $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
313 } 316 }
314 317
315 /** 318 /**
321 $stream = $output->getStream(); 324 $stream = $output->getStream();
322 $shell = new Shell($this->getConfig()); 325 $shell = new Shell($this->getConfig());
323 $shell->setOutput($output); 326 $shell->setOutput($output);
324 327
325 $shell->writeReturnValue($input); 328 $shell->writeReturnValue($input);
326 rewind($stream); 329 \rewind($stream);
327 $this->assertEquals($expected, stream_get_contents($stream)); 330 $this->assertEquals($expected, \stream_get_contents($stream));
328 } 331 }
329 332
330 public function getReturnValues() 333 public function getReturnValues()
331 { 334 {
332 return [ 335 return [
344 $stream = $output->getStream(); 347 $stream = $output->getStream();
345 $shell = new Shell($this->getConfig()); 348 $shell = new Shell($this->getConfig());
346 $shell->setOutput($output); 349 $shell->setOutput($output);
347 350
348 $shell->writeException($exception); 351 $shell->writeException($exception);
349 rewind($stream); 352 \rewind($stream);
350 $this->assertSame($expected, stream_get_contents($stream)); 353 $this->assertSame($expected, \stream_get_contents($stream));
351 } 354 }
352 355
353 public function getRenderedExceptions() 356 public function getRenderedExceptions()
354 { 357 {
355 return [ 358 return [
365 $output = $this->getOutput(); 368 $output = $this->getOutput();
366 $stream = $output->getStream(); 369 $stream = $output->getStream();
367 $shell = new Shell($this->getConfig()); 370 $shell = new Shell($this->getConfig());
368 $shell->setOutput($output); 371 $shell->setOutput($output);
369 $this->assertEquals($expected, $shell->execute($input)); 372 $this->assertEquals($expected, $shell->execute($input));
370 rewind($stream); 373 \rewind($stream);
371 $this->assertSame('', stream_get_contents($stream)); 374 $this->assertSame('', \stream_get_contents($stream));
372 } 375 }
373 376
374 public function getExecuteValues() 377 public function getExecuteValues()
375 { 378 {
376 return [ 379 return [
412 ]; 415 ];
413 } 416 }
414 417
415 private function getOutput() 418 private function getOutput()
416 { 419 {
417 $stream = fopen('php://memory', 'w+'); 420 $stream = \fopen('php://memory', 'w+');
418 $this->streams[] = $stream; 421 $this->streams[] = $stream;
419 422
420 $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false); 423 $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
421 424
422 return $output; 425 return $output;
423 } 426 }
424 427
425 private function getConfig(array $config = []) 428 private function getConfig(array $config = [])
426 { 429 {
427 // Mebbe there's a better way than this? 430 // Mebbe there's a better way than this?
428 $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_'); 431 $dir = \tempnam(\sys_get_temp_dir(), 'psysh_shell_test_');
429 unlink($dir); 432 \unlink($dir);
430 433
431 $defaults = [ 434 $defaults = [
432 'configDir' => $dir, 435 'configDir' => $dir,
433 'dataDir' => $dir, 436 'dataDir' => $dir,
434 'runtimeDir' => $dir, 437 'runtimeDir' => $dir,
435 ]; 438 ];
436 439
437 return new Configuration(array_merge($defaults, $config)); 440 return new Configuration(\array_merge($defaults, $config));
438 } 441 }
439 } 442 }