annotate vendor/psy/psysh/test/ShellTest.php @ 19:fa3358dc1485 tip

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