annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Test;
Chris@0 13
Chris@0 14 use Psy\Configuration;
Chris@0 15 use Psy\Exception\ErrorException;
Chris@0 16 use Psy\Exception\ParseErrorException;
Chris@0 17 use Psy\Shell;
Chris@0 18 use Psy\TabCompletion\Matcher\ClassMethodsMatcher;
Chris@0 19 use Symfony\Component\Console\Output\StreamOutput;
Chris@0 20
Chris@0 21 class ShellTest extends \PHPUnit\Framework\TestCase
Chris@0 22 {
Chris@0 23 private $streams = [];
Chris@0 24
Chris@0 25 public function tearDown()
Chris@0 26 {
Chris@0 27 foreach ($this->streams as $stream) {
Chris@4 28 \fclose($stream);
Chris@0 29 }
Chris@0 30 }
Chris@0 31
Chris@0 32 public function testScopeVariables()
Chris@0 33 {
Chris@0 34 $one = 'banana';
Chris@0 35 $two = 123;
Chris@0 36 $three = new \StdClass();
Chris@0 37 $__psysh__ = 'ignore this';
Chris@0 38 $_ = 'ignore this';
Chris@0 39 $_e = 'ignore this';
Chris@0 40
Chris@0 41 $shell = new Shell($this->getConfig());
Chris@4 42 $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
Chris@0 43
Chris@0 44 $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
Chris@0 45 $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames());
Chris@0 46 $this->assertSame('banana', $shell->getScopeVariable('one'));
Chris@0 47 $this->assertSame(123, $shell->getScopeVariable('two'));
Chris@0 48 $this->assertSame($three, $shell->getScopeVariable('three'));
Chris@0 49 $this->assertNull($shell->getScopeVariable('_'));
Chris@0 50
Chris@4 51 $diff = $shell->getScopeVariablesDiff(['one' => $one, 'two' => 'not two']);
Chris@4 52 $this->assertSame(['two' => $two, 'three' => $three, '_' => null], $diff);
Chris@4 53
Chris@0 54 $shell->setScopeVariables([]);
Chris@0 55 $this->assertSame(['_'], $shell->getScopeVariableNames());
Chris@0 56
Chris@0 57 $shell->setBoundObject($this);
Chris@0 58 $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
Chris@0 59 $this->assertSame($this, $shell->getScopeVariable('this'));
Chris@0 60 $this->assertSame(['_' => null], $shell->getScopeVariables(false));
Chris@0 61 $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables());
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * @expectedException \InvalidArgumentException
Chris@0 66 */
Chris@0 67 public function testUnknownScopeVariablesThrowExceptions()
Chris@0 68 {
Chris@0 69 $shell = new Shell($this->getConfig());
Chris@0 70 $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]);
Chris@0 71 $shell->getScopeVariable('baz');
Chris@0 72 }
Chris@0 73
Chris@0 74 public function testIncludesWithScopeVariables()
Chris@0 75 {
Chris@0 76 $one = 'banana';
Chris@0 77 $two = 123;
Chris@0 78 $three = new \StdClass();
Chris@0 79 $__psysh__ = 'ignore this';
Chris@0 80 $_ = 'ignore this';
Chris@0 81 $_e = 'ignore this';
Chris@0 82
Chris@0 83 $config = $this->getConfig(['usePcntl' => false]);
Chris@0 84
Chris@0 85 $shell = new Shell($config);
Chris@4 86 $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
Chris@0 87 $shell->addInput('exit', true);
Chris@0 88
Chris@0 89 // This is super slow and we shouldn't do this :(
Chris@0 90 $shell->run(null, $this->getOutput());
Chris@0 91
Chris@0 92 $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
Chris@0 93 $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames());
Chris@0 94 $this->assertSame('banana', $shell->getScopeVariable('one'));
Chris@0 95 $this->assertSame(123, $shell->getScopeVariable('two'));
Chris@0 96 $this->assertSame($three, $shell->getScopeVariable('three'));
Chris@0 97 $this->assertNull($shell->getScopeVariable('_'));
Chris@0 98 }
Chris@0 99
Chris@0 100 public function testIncludes()
Chris@0 101 {
Chris@0 102 $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']);
Chris@0 103
Chris@0 104 $shell = new Shell($config);
Chris@0 105 $this->assertEmpty($shell->getIncludes());
Chris@0 106 $shell->setIncludes(['foo', 'bar', 'baz']);
Chris@0 107 $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes());
Chris@0 108 }
Chris@0 109
Chris@0 110 public function testIncludesConfig()
Chris@0 111 {
Chris@0 112 $config = $this->getConfig([
Chris@0 113 'defaultIncludes' => ['/file.php'],
Chris@0 114 'configFile' => __DIR__ . '/fixtures/empty.php',
Chris@0 115 ]);
Chris@0 116
Chris@0 117 $shell = new Shell($config);
Chris@0 118
Chris@0 119 $includes = $shell->getIncludes();
Chris@0 120 $this->assertSame('/file.php', $includes[0]);
Chris@0 121 }
Chris@0 122
Chris@0 123 public function testAddMatchersViaConfig()
Chris@0 124 {
Chris@0 125 $shell = new FakeShell();
Chris@0 126 $matcher = new ClassMethodsMatcher();
Chris@0 127
Chris@0 128 $config = $this->getConfig([
Chris@0 129 'matchers' => [$matcher],
Chris@0 130 ]);
Chris@0 131 $config->setShell($shell);
Chris@0 132
Chris@0 133 $this->assertSame([$matcher], $shell->matchers);
Chris@0 134 }
Chris@0 135
Chris@0 136 public function testAddMatchersViaConfigAfterShell()
Chris@0 137 {
Chris@0 138 $shell = new FakeShell();
Chris@0 139 $matcher = new ClassMethodsMatcher();
Chris@0 140
Chris@0 141 $config = $this->getConfig([]);
Chris@0 142 $config->setShell($shell);
Chris@0 143 $config->addMatchers([$matcher]);
Chris@0 144
Chris@0 145 $this->assertSame([$matcher], $shell->matchers);
Chris@0 146 }
Chris@0 147
Chris@0 148 public function testRenderingExceptions()
Chris@0 149 {
Chris@0 150 $shell = new Shell($this->getConfig());
Chris@0 151 $output = $this->getOutput();
Chris@0 152 $stream = $output->getStream();
Chris@0 153 $e = new ParseErrorException('message', 13);
Chris@0 154
Chris@0 155 $shell->setOutput($output);
Chris@0 156 $shell->addCode('code');
Chris@0 157 $this->assertTrue($shell->hasCode());
Chris@0 158 $this->assertNotEmpty($shell->getCodeBuffer());
Chris@0 159
Chris@0 160 $shell->writeException($e);
Chris@0 161
Chris@0 162 $this->assertSame($e, $shell->getScopeVariable('_e'));
Chris@0 163 $this->assertFalse($shell->hasCode());
Chris@0 164 $this->assertEmpty($shell->getCodeBuffer());
Chris@0 165
Chris@4 166 \rewind($stream);
Chris@4 167 $streamContents = \stream_get_contents($stream);
Chris@0 168
Chris@0 169 $this->assertContains('PHP Parse error', $streamContents);
Chris@0 170 $this->assertContains('message', $streamContents);
Chris@0 171 $this->assertContains('line 13', $streamContents);
Chris@0 172 }
Chris@0 173
Chris@0 174 public function testHandlingErrors()
Chris@0 175 {
Chris@0 176 $shell = new Shell($this->getConfig());
Chris@0 177 $output = $this->getOutput();
Chris@0 178 $stream = $output->getStream();
Chris@0 179 $shell->setOutput($output);
Chris@0 180
Chris@4 181 $oldLevel = \error_reporting();
Chris@4 182 \error_reporting($oldLevel & ~E_USER_NOTICE);
Chris@0 183
Chris@0 184 try {
Chris@0 185 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
Chris@0 186 } catch (ErrorException $e) {
Chris@4 187 \error_reporting($oldLevel);
Chris@0 188 $this->fail('Unexpected error exception');
Chris@0 189 }
Chris@4 190 \error_reporting($oldLevel);
Chris@0 191
Chris@4 192 \rewind($stream);
Chris@4 193 $streamContents = \stream_get_contents($stream);
Chris@0 194
Chris@0 195 $this->assertContains('PHP Notice:', $streamContents);
Chris@0 196 $this->assertContains('wheee', $streamContents);
Chris@0 197 $this->assertContains('line 13', $streamContents);
Chris@0 198 }
Chris@0 199
Chris@0 200 /**
Chris@0 201 * @expectedException \Psy\Exception\ErrorException
Chris@0 202 */
Chris@0 203 public function testNotHandlingErrors()
Chris@0 204 {
Chris@0 205 $shell = new Shell($this->getConfig());
Chris@4 206 $oldLevel = \error_reporting();
Chris@4 207 \error_reporting($oldLevel | E_USER_NOTICE);
Chris@0 208
Chris@0 209 try {
Chris@0 210 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
Chris@0 211 } catch (ErrorException $e) {
Chris@4 212 \error_reporting($oldLevel);
Chris@0 213 throw $e;
Chris@0 214 }
Chris@0 215 }
Chris@0 216
Chris@0 217 public function testVersion()
Chris@0 218 {
Chris@0 219 $shell = new Shell($this->getConfig());
Chris@0 220
Chris@0 221 $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
Chris@0 222 $this->assertContains(Shell::VERSION, $shell->getVersion());
Chris@4 223 $this->assertContains(PHP_VERSION, $shell->getVersion());
Chris@4 224 $this->assertContains(PHP_SAPI, $shell->getVersion());
Chris@0 225 }
Chris@0 226
Chris@0 227 public function testCodeBuffer()
Chris@0 228 {
Chris@0 229 $shell = new Shell($this->getConfig());
Chris@0 230
Chris@0 231 $shell->addCode('class');
Chris@0 232 $this->assertNull($shell->flushCode());
Chris@0 233 $this->assertTrue($shell->hasCode());
Chris@0 234
Chris@0 235 $shell->addCode('a');
Chris@0 236 $this->assertNull($shell->flushCode());
Chris@0 237 $this->assertTrue($shell->hasCode());
Chris@0 238
Chris@0 239 $shell->addCode('{}');
Chris@0 240 $code = $shell->flushCode();
Chris@0 241 $this->assertFalse($shell->hasCode());
Chris@4 242 $code = \preg_replace('/\s+/', ' ', $code);
Chris@0 243 $this->assertNotNull($code);
Chris@0 244 $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
Chris@0 245 }
Chris@0 246
Chris@0 247 public function testKeepCodeBufferOpen()
Chris@0 248 {
Chris@0 249 $shell = new Shell($this->getConfig());
Chris@0 250
Chris@0 251 $shell->addCode('1 \\');
Chris@0 252 $this->assertNull($shell->flushCode());
Chris@0 253 $this->assertTrue($shell->hasCode());
Chris@0 254
Chris@0 255 $shell->addCode('+ 1 \\');
Chris@0 256 $this->assertNull($shell->flushCode());
Chris@0 257 $this->assertTrue($shell->hasCode());
Chris@0 258
Chris@0 259 $shell->addCode('+ 1');
Chris@0 260 $code = $shell->flushCode();
Chris@0 261 $this->assertFalse($shell->hasCode());
Chris@4 262 $code = \preg_replace('/\s+/', ' ', $code);
Chris@0 263 $this->assertNotNull($code);
Chris@0 264 $this->assertSame('return 1 + 1 + 1;', $code);
Chris@0 265 }
Chris@0 266
Chris@0 267 /**
Chris@0 268 * @expectedException \Psy\Exception\ParseErrorException
Chris@0 269 */
Chris@0 270 public function testCodeBufferThrowsParseExceptions()
Chris@0 271 {
Chris@0 272 $shell = new Shell($this->getConfig());
Chris@0 273 $shell->addCode('this is not valid');
Chris@0 274 $shell->flushCode();
Chris@0 275 }
Chris@0 276
Chris@0 277 public function testClosuresSupport()
Chris@0 278 {
Chris@0 279 $shell = new Shell($this->getConfig());
Chris@0 280 $code = '$test = function () {}';
Chris@0 281 $shell->addCode($code);
Chris@0 282 $shell->flushCode();
Chris@0 283 $code = '$test()';
Chris@0 284 $shell->addCode($code);
Chris@0 285 $this->assertSame($shell->flushCode(), 'return $test();');
Chris@0 286 }
Chris@0 287
Chris@0 288 public function testWriteStdout()
Chris@0 289 {
Chris@0 290 $output = $this->getOutput();
Chris@0 291 $stream = $output->getStream();
Chris@0 292 $shell = new Shell($this->getConfig());
Chris@0 293 $shell->setOutput($output);
Chris@0 294
Chris@0 295 $shell->writeStdout("{{stdout}}\n");
Chris@0 296
Chris@4 297 \rewind($stream);
Chris@4 298 $streamContents = \stream_get_contents($stream);
Chris@0 299
Chris@0 300 $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
Chris@0 301 }
Chris@0 302
Chris@0 303 public function testWriteStdoutWithoutNewline()
Chris@0 304 {
Chris@0 305 $output = $this->getOutput();
Chris@0 306 $stream = $output->getStream();
Chris@0 307 $shell = new Shell($this->getConfig());
Chris@0 308 $shell->setOutput($output);
Chris@0 309
Chris@0 310 $shell->writeStdout('{{stdout}}');
Chris@0 311
Chris@4 312 \rewind($stream);
Chris@4 313 $streamContents = \stream_get_contents($stream);
Chris@0 314
Chris@0 315 $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
Chris@0 316 }
Chris@0 317
Chris@0 318 /**
Chris@0 319 * @dataProvider getReturnValues
Chris@0 320 */
Chris@0 321 public function testWriteReturnValue($input, $expected)
Chris@0 322 {
Chris@0 323 $output = $this->getOutput();
Chris@0 324 $stream = $output->getStream();
Chris@0 325 $shell = new Shell($this->getConfig());
Chris@0 326 $shell->setOutput($output);
Chris@0 327
Chris@0 328 $shell->writeReturnValue($input);
Chris@4 329 \rewind($stream);
Chris@4 330 $this->assertEquals($expected, \stream_get_contents($stream));
Chris@0 331 }
Chris@0 332
Chris@0 333 public function getReturnValues()
Chris@0 334 {
Chris@0 335 return [
Chris@0 336 ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL],
Chris@0 337 [1, "=> \033[35m1\033[39m" . PHP_EOL],
Chris@0 338 ];
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * @dataProvider getRenderedExceptions
Chris@0 343 */
Chris@0 344 public function testWriteException($exception, $expected)
Chris@0 345 {
Chris@0 346 $output = $this->getOutput();
Chris@0 347 $stream = $output->getStream();
Chris@0 348 $shell = new Shell($this->getConfig());
Chris@0 349 $shell->setOutput($output);
Chris@0 350
Chris@0 351 $shell->writeException($exception);
Chris@4 352 \rewind($stream);
Chris@4 353 $this->assertSame($expected, \stream_get_contents($stream));
Chris@0 354 }
Chris@0 355
Chris@0 356 public function getRenderedExceptions()
Chris@0 357 {
Chris@0 358 return [
Chris@0 359 [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL],
Chris@0 360 ];
Chris@0 361 }
Chris@0 362
Chris@0 363 /**
Chris@0 364 * @dataProvider getExecuteValues
Chris@0 365 */
Chris@0 366 public function testShellExecute($input, $expected)
Chris@0 367 {
Chris@0 368 $output = $this->getOutput();
Chris@0 369 $stream = $output->getStream();
Chris@0 370 $shell = new Shell($this->getConfig());
Chris@0 371 $shell->setOutput($output);
Chris@0 372 $this->assertEquals($expected, $shell->execute($input));
Chris@4 373 \rewind($stream);
Chris@4 374 $this->assertSame('', \stream_get_contents($stream));
Chris@0 375 }
Chris@0 376
Chris@0 377 public function getExecuteValues()
Chris@0 378 {
Chris@0 379 return [
Chris@0 380 ['return 12', 12],
Chris@0 381 ['"{{return value}}"', '{{return value}}'],
Chris@0 382 ['1', '1'],
Chris@0 383 ];
Chris@0 384 }
Chris@0 385
Chris@0 386 /**
Chris@0 387 * @dataProvider commandsToHas
Chris@0 388 */
Chris@0 389 public function testHasCommand($command, $has)
Chris@0 390 {
Chris@0 391 $shell = new Shell($this->getConfig());
Chris@0 392
Chris@0 393 // :-/
Chris@0 394 $refl = new \ReflectionClass('Psy\\Shell');
Chris@0 395 $method = $refl->getMethod('hasCommand');
Chris@0 396 $method->setAccessible(true);
Chris@0 397
Chris@0 398 $this->assertEquals($method->invokeArgs($shell, [$command]), $has);
Chris@0 399 }
Chris@0 400
Chris@0 401 public function commandsToHas()
Chris@0 402 {
Chris@0 403 return [
Chris@0 404 ['help', true],
Chris@0 405 ['help help', true],
Chris@0 406 ['"help"', false],
Chris@0 407 ['"help help"', false],
Chris@0 408 ['ls -al ', true],
Chris@0 409 ['ls "-al" ', true],
Chris@0 410 ['ls"-al"', false],
Chris@0 411 [' q', true],
Chris@0 412 [' q --help', true],
Chris@0 413 ['"q"', false],
Chris@0 414 ['"q",', false],
Chris@0 415 ];
Chris@0 416 }
Chris@0 417
Chris@0 418 private function getOutput()
Chris@0 419 {
Chris@4 420 $stream = \fopen('php://memory', 'w+');
Chris@0 421 $this->streams[] = $stream;
Chris@0 422
Chris@0 423 $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
Chris@0 424
Chris@0 425 return $output;
Chris@0 426 }
Chris@0 427
Chris@0 428 private function getConfig(array $config = [])
Chris@0 429 {
Chris@0 430 // Mebbe there's a better way than this?
Chris@4 431 $dir = \tempnam(\sys_get_temp_dir(), 'psysh_shell_test_');
Chris@4 432 \unlink($dir);
Chris@0 433
Chris@0 434 $defaults = [
Chris@0 435 'configDir' => $dir,
Chris@0 436 'dataDir' => $dir,
Chris@0 437 'runtimeDir' => $dir,
Chris@0 438 ];
Chris@0 439
Chris@4 440 return new Configuration(\array_merge($defaults, $config));
Chris@0 441 }
Chris@0 442 }