annotate vendor/psy/psysh/test/ShellTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children c2387f117808
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@13 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@13 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@13 51 $shell->setScopeVariables([]);
Chris@13 52 $this->assertSame(['_'], $shell->getScopeVariableNames());
Chris@13 53
Chris@13 54 $shell->setBoundObject($this);
Chris@13 55 $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
Chris@13 56 $this->assertSame($this, $shell->getScopeVariable('this'));
Chris@13 57 $this->assertSame(['_' => null], $shell->getScopeVariables(false));
Chris@13 58 $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables());
Chris@13 59 }
Chris@13 60
Chris@13 61 /**
Chris@13 62 * @expectedException \InvalidArgumentException
Chris@13 63 */
Chris@13 64 public function testUnknownScopeVariablesThrowExceptions()
Chris@13 65 {
Chris@13 66 $shell = new Shell($this->getConfig());
Chris@13 67 $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]);
Chris@13 68 $shell->getScopeVariable('baz');
Chris@13 69 }
Chris@13 70
Chris@13 71 public function testIncludesWithScopeVariables()
Chris@13 72 {
Chris@13 73 $one = 'banana';
Chris@13 74 $two = 123;
Chris@13 75 $three = new \StdClass();
Chris@13 76 $__psysh__ = 'ignore this';
Chris@13 77 $_ = 'ignore this';
Chris@13 78 $_e = 'ignore this';
Chris@13 79
Chris@13 80 $config = $this->getConfig(['usePcntl' => false]);
Chris@13 81
Chris@13 82 $shell = new Shell($config);
Chris@13 83 $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
Chris@13 84 $shell->addInput('exit', true);
Chris@13 85
Chris@13 86 // This is super slow and we shouldn't do this :(
Chris@13 87 $shell->run(null, $this->getOutput());
Chris@13 88
Chris@13 89 $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
Chris@13 90 $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames());
Chris@13 91 $this->assertSame('banana', $shell->getScopeVariable('one'));
Chris@13 92 $this->assertSame(123, $shell->getScopeVariable('two'));
Chris@13 93 $this->assertSame($three, $shell->getScopeVariable('three'));
Chris@13 94 $this->assertNull($shell->getScopeVariable('_'));
Chris@13 95 }
Chris@13 96
Chris@13 97 public function testIncludes()
Chris@13 98 {
Chris@13 99 $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']);
Chris@13 100
Chris@13 101 $shell = new Shell($config);
Chris@13 102 $this->assertEmpty($shell->getIncludes());
Chris@13 103 $shell->setIncludes(['foo', 'bar', 'baz']);
Chris@13 104 $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes());
Chris@13 105 }
Chris@13 106
Chris@13 107 public function testIncludesConfig()
Chris@13 108 {
Chris@13 109 $config = $this->getConfig([
Chris@13 110 'defaultIncludes' => ['/file.php'],
Chris@13 111 'configFile' => __DIR__ . '/fixtures/empty.php',
Chris@13 112 ]);
Chris@13 113
Chris@13 114 $shell = new Shell($config);
Chris@13 115
Chris@13 116 $includes = $shell->getIncludes();
Chris@13 117 $this->assertSame('/file.php', $includes[0]);
Chris@13 118 }
Chris@13 119
Chris@13 120 public function testAddMatchersViaConfig()
Chris@13 121 {
Chris@13 122 $shell = new FakeShell();
Chris@13 123 $matcher = new ClassMethodsMatcher();
Chris@13 124
Chris@13 125 $config = $this->getConfig([
Chris@13 126 'matchers' => [$matcher],
Chris@13 127 ]);
Chris@13 128 $config->setShell($shell);
Chris@13 129
Chris@13 130 $this->assertSame([$matcher], $shell->matchers);
Chris@13 131 }
Chris@13 132
Chris@13 133 public function testAddMatchersViaConfigAfterShell()
Chris@13 134 {
Chris@13 135 $shell = new FakeShell();
Chris@13 136 $matcher = new ClassMethodsMatcher();
Chris@13 137
Chris@13 138 $config = $this->getConfig([]);
Chris@13 139 $config->setShell($shell);
Chris@13 140 $config->addMatchers([$matcher]);
Chris@13 141
Chris@13 142 $this->assertSame([$matcher], $shell->matchers);
Chris@13 143 }
Chris@13 144
Chris@13 145 public function testRenderingExceptions()
Chris@13 146 {
Chris@13 147 $shell = new Shell($this->getConfig());
Chris@13 148 $output = $this->getOutput();
Chris@13 149 $stream = $output->getStream();
Chris@13 150 $e = new ParseErrorException('message', 13);
Chris@13 151
Chris@13 152 $shell->setOutput($output);
Chris@13 153 $shell->addCode('code');
Chris@13 154 $this->assertTrue($shell->hasCode());
Chris@13 155 $this->assertNotEmpty($shell->getCodeBuffer());
Chris@13 156
Chris@13 157 $shell->writeException($e);
Chris@13 158
Chris@13 159 $this->assertSame($e, $shell->getScopeVariable('_e'));
Chris@13 160 $this->assertFalse($shell->hasCode());
Chris@13 161 $this->assertEmpty($shell->getCodeBuffer());
Chris@13 162
Chris@13 163 rewind($stream);
Chris@13 164 $streamContents = stream_get_contents($stream);
Chris@13 165
Chris@13 166 $this->assertContains('PHP Parse error', $streamContents);
Chris@13 167 $this->assertContains('message', $streamContents);
Chris@13 168 $this->assertContains('line 13', $streamContents);
Chris@13 169 }
Chris@13 170
Chris@13 171 public function testHandlingErrors()
Chris@13 172 {
Chris@13 173 $shell = new Shell($this->getConfig());
Chris@13 174 $output = $this->getOutput();
Chris@13 175 $stream = $output->getStream();
Chris@13 176 $shell->setOutput($output);
Chris@13 177
Chris@13 178 $oldLevel = error_reporting();
Chris@13 179 error_reporting($oldLevel & ~E_USER_NOTICE);
Chris@13 180
Chris@13 181 try {
Chris@13 182 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
Chris@13 183 } catch (ErrorException $e) {
Chris@13 184 error_reporting($oldLevel);
Chris@13 185 $this->fail('Unexpected error exception');
Chris@13 186 }
Chris@13 187 error_reporting($oldLevel);
Chris@13 188
Chris@13 189 rewind($stream);
Chris@13 190 $streamContents = stream_get_contents($stream);
Chris@13 191
Chris@13 192 $this->assertContains('PHP Notice:', $streamContents);
Chris@13 193 $this->assertContains('wheee', $streamContents);
Chris@13 194 $this->assertContains('line 13', $streamContents);
Chris@13 195 }
Chris@13 196
Chris@13 197 /**
Chris@13 198 * @expectedException \Psy\Exception\ErrorException
Chris@13 199 */
Chris@13 200 public function testNotHandlingErrors()
Chris@13 201 {
Chris@13 202 $shell = new Shell($this->getConfig());
Chris@13 203 $oldLevel = error_reporting();
Chris@13 204 error_reporting($oldLevel | E_USER_NOTICE);
Chris@13 205
Chris@13 206 try {
Chris@13 207 $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
Chris@13 208 } catch (ErrorException $e) {
Chris@13 209 error_reporting($oldLevel);
Chris@13 210 throw $e;
Chris@13 211 }
Chris@13 212 }
Chris@13 213
Chris@13 214 public function testVersion()
Chris@13 215 {
Chris@13 216 $shell = new Shell($this->getConfig());
Chris@13 217
Chris@13 218 $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
Chris@13 219 $this->assertContains(Shell::VERSION, $shell->getVersion());
Chris@13 220 $this->assertContains(phpversion(), $shell->getVersion());
Chris@13 221 $this->assertContains(php_sapi_name(), $shell->getVersion());
Chris@13 222 }
Chris@13 223
Chris@13 224 public function testCodeBuffer()
Chris@13 225 {
Chris@13 226 $shell = new Shell($this->getConfig());
Chris@13 227
Chris@13 228 $shell->addCode('class');
Chris@13 229 $this->assertNull($shell->flushCode());
Chris@13 230 $this->assertTrue($shell->hasCode());
Chris@13 231
Chris@13 232 $shell->addCode('a');
Chris@13 233 $this->assertNull($shell->flushCode());
Chris@13 234 $this->assertTrue($shell->hasCode());
Chris@13 235
Chris@13 236 $shell->addCode('{}');
Chris@13 237 $code = $shell->flushCode();
Chris@13 238 $this->assertFalse($shell->hasCode());
Chris@13 239 $code = preg_replace('/\s+/', ' ', $code);
Chris@13 240 $this->assertNotNull($code);
Chris@13 241 $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
Chris@13 242 }
Chris@13 243
Chris@13 244 public function testKeepCodeBufferOpen()
Chris@13 245 {
Chris@13 246 $shell = new Shell($this->getConfig());
Chris@13 247
Chris@13 248 $shell->addCode('1 \\');
Chris@13 249 $this->assertNull($shell->flushCode());
Chris@13 250 $this->assertTrue($shell->hasCode());
Chris@13 251
Chris@13 252 $shell->addCode('+ 1 \\');
Chris@13 253 $this->assertNull($shell->flushCode());
Chris@13 254 $this->assertTrue($shell->hasCode());
Chris@13 255
Chris@13 256 $shell->addCode('+ 1');
Chris@13 257 $code = $shell->flushCode();
Chris@13 258 $this->assertFalse($shell->hasCode());
Chris@13 259 $code = preg_replace('/\s+/', ' ', $code);
Chris@13 260 $this->assertNotNull($code);
Chris@13 261 $this->assertSame('return 1 + 1 + 1;', $code);
Chris@13 262 }
Chris@13 263
Chris@13 264 /**
Chris@13 265 * @expectedException \Psy\Exception\ParseErrorException
Chris@13 266 */
Chris@13 267 public function testCodeBufferThrowsParseExceptions()
Chris@13 268 {
Chris@13 269 $shell = new Shell($this->getConfig());
Chris@13 270 $shell->addCode('this is not valid');
Chris@13 271 $shell->flushCode();
Chris@13 272 }
Chris@13 273
Chris@13 274 public function testClosuresSupport()
Chris@13 275 {
Chris@13 276 $shell = new Shell($this->getConfig());
Chris@13 277 $code = '$test = function () {}';
Chris@13 278 $shell->addCode($code);
Chris@13 279 $shell->flushCode();
Chris@13 280 $code = '$test()';
Chris@13 281 $shell->addCode($code);
Chris@13 282 $this->assertSame($shell->flushCode(), 'return $test();');
Chris@13 283 }
Chris@13 284
Chris@13 285 public function testWriteStdout()
Chris@13 286 {
Chris@13 287 $output = $this->getOutput();
Chris@13 288 $stream = $output->getStream();
Chris@13 289 $shell = new Shell($this->getConfig());
Chris@13 290 $shell->setOutput($output);
Chris@13 291
Chris@13 292 $shell->writeStdout("{{stdout}}\n");
Chris@13 293
Chris@13 294 rewind($stream);
Chris@13 295 $streamContents = stream_get_contents($stream);
Chris@13 296
Chris@13 297 $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
Chris@13 298 }
Chris@13 299
Chris@13 300 public function testWriteStdoutWithoutNewline()
Chris@13 301 {
Chris@13 302 $output = $this->getOutput();
Chris@13 303 $stream = $output->getStream();
Chris@13 304 $shell = new Shell($this->getConfig());
Chris@13 305 $shell->setOutput($output);
Chris@13 306
Chris@13 307 $shell->writeStdout('{{stdout}}');
Chris@13 308
Chris@13 309 rewind($stream);
Chris@13 310 $streamContents = stream_get_contents($stream);
Chris@13 311
Chris@13 312 $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
Chris@13 313 }
Chris@13 314
Chris@13 315 /**
Chris@13 316 * @dataProvider getReturnValues
Chris@13 317 */
Chris@13 318 public function testWriteReturnValue($input, $expected)
Chris@13 319 {
Chris@13 320 $output = $this->getOutput();
Chris@13 321 $stream = $output->getStream();
Chris@13 322 $shell = new Shell($this->getConfig());
Chris@13 323 $shell->setOutput($output);
Chris@13 324
Chris@13 325 $shell->writeReturnValue($input);
Chris@13 326 rewind($stream);
Chris@13 327 $this->assertEquals($expected, stream_get_contents($stream));
Chris@13 328 }
Chris@13 329
Chris@13 330 public function getReturnValues()
Chris@13 331 {
Chris@13 332 return [
Chris@13 333 ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL],
Chris@13 334 [1, "=> \033[35m1\033[39m" . PHP_EOL],
Chris@13 335 ];
Chris@13 336 }
Chris@13 337
Chris@13 338 /**
Chris@13 339 * @dataProvider getRenderedExceptions
Chris@13 340 */
Chris@13 341 public function testWriteException($exception, $expected)
Chris@13 342 {
Chris@13 343 $output = $this->getOutput();
Chris@13 344 $stream = $output->getStream();
Chris@13 345 $shell = new Shell($this->getConfig());
Chris@13 346 $shell->setOutput($output);
Chris@13 347
Chris@13 348 $shell->writeException($exception);
Chris@13 349 rewind($stream);
Chris@13 350 $this->assertSame($expected, stream_get_contents($stream));
Chris@13 351 }
Chris@13 352
Chris@13 353 public function getRenderedExceptions()
Chris@13 354 {
Chris@13 355 return [
Chris@13 356 [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL],
Chris@13 357 ];
Chris@13 358 }
Chris@13 359
Chris@13 360 /**
Chris@13 361 * @dataProvider getExecuteValues
Chris@13 362 */
Chris@13 363 public function testShellExecute($input, $expected)
Chris@13 364 {
Chris@13 365 $output = $this->getOutput();
Chris@13 366 $stream = $output->getStream();
Chris@13 367 $shell = new Shell($this->getConfig());
Chris@13 368 $shell->setOutput($output);
Chris@13 369 $this->assertEquals($expected, $shell->execute($input));
Chris@13 370 rewind($stream);
Chris@13 371 $this->assertSame('', stream_get_contents($stream));
Chris@13 372 }
Chris@13 373
Chris@13 374 public function getExecuteValues()
Chris@13 375 {
Chris@13 376 return [
Chris@13 377 ['return 12', 12],
Chris@13 378 ['"{{return value}}"', '{{return value}}'],
Chris@13 379 ['1', '1'],
Chris@13 380 ];
Chris@13 381 }
Chris@13 382
Chris@13 383 private function getOutput()
Chris@13 384 {
Chris@13 385 $stream = fopen('php://memory', 'w+');
Chris@13 386 $this->streams[] = $stream;
Chris@13 387
Chris@13 388 $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
Chris@13 389
Chris@13 390 return $output;
Chris@13 391 }
Chris@13 392
Chris@13 393 private function getConfig(array $config = [])
Chris@13 394 {
Chris@13 395 // Mebbe there's a better way than this?
Chris@13 396 $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_');
Chris@13 397 unlink($dir);
Chris@13 398
Chris@13 399 $defaults = [
Chris@13 400 'configDir' => $dir,
Chris@13 401 'dataDir' => $dir,
Chris@13 402 'runtimeDir' => $dir,
Chris@13 403 ];
Chris@13 404
Chris@13 405 return new Configuration(array_merge($defaults, $config));
Chris@13 406 }
Chris@13 407 }