Chris@13: streams as $stream) { Chris@17: \fclose($stream); Chris@13: } Chris@13: } Chris@13: Chris@13: public function testScopeVariables() Chris@13: { Chris@13: $one = 'banana'; Chris@13: $two = 123; Chris@13: $three = new \StdClass(); Chris@13: $__psysh__ = 'ignore this'; Chris@13: $_ = 'ignore this'; Chris@13: $_e = 'ignore this'; Chris@13: Chris@13: $shell = new Shell($this->getConfig()); Chris@17: $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this')); Chris@13: Chris@13: $this->assertNotContains('__psysh__', $shell->getScopeVariableNames()); Chris@13: $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames()); Chris@13: $this->assertSame('banana', $shell->getScopeVariable('one')); Chris@13: $this->assertSame(123, $shell->getScopeVariable('two')); Chris@13: $this->assertSame($three, $shell->getScopeVariable('three')); Chris@13: $this->assertNull($shell->getScopeVariable('_')); Chris@13: Chris@17: $diff = $shell->getScopeVariablesDiff(['one' => $one, 'two' => 'not two']); Chris@17: $this->assertSame(['two' => $two, 'three' => $three, '_' => null], $diff); Chris@17: Chris@13: $shell->setScopeVariables([]); Chris@13: $this->assertSame(['_'], $shell->getScopeVariableNames()); Chris@13: Chris@13: $shell->setBoundObject($this); Chris@13: $this->assertSame(['_', 'this'], $shell->getScopeVariableNames()); Chris@13: $this->assertSame($this, $shell->getScopeVariable('this')); Chris@13: $this->assertSame(['_' => null], $shell->getScopeVariables(false)); Chris@13: $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables()); Chris@13: } Chris@13: Chris@13: /** Chris@13: * @expectedException \InvalidArgumentException Chris@13: */ Chris@13: public function testUnknownScopeVariablesThrowExceptions() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]); Chris@13: $shell->getScopeVariable('baz'); Chris@13: } Chris@13: Chris@13: public function testIncludesWithScopeVariables() Chris@13: { Chris@13: $one = 'banana'; Chris@13: $two = 123; Chris@13: $three = new \StdClass(); Chris@13: $__psysh__ = 'ignore this'; Chris@13: $_ = 'ignore this'; Chris@13: $_e = 'ignore this'; Chris@13: Chris@13: $config = $this->getConfig(['usePcntl' => false]); Chris@13: Chris@13: $shell = new Shell($config); Chris@17: $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this')); Chris@13: $shell->addInput('exit', true); Chris@13: Chris@13: // This is super slow and we shouldn't do this :( Chris@13: $shell->run(null, $this->getOutput()); Chris@13: Chris@13: $this->assertNotContains('__psysh__', $shell->getScopeVariableNames()); Chris@13: $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames()); Chris@13: $this->assertSame('banana', $shell->getScopeVariable('one')); Chris@13: $this->assertSame(123, $shell->getScopeVariable('two')); Chris@13: $this->assertSame($three, $shell->getScopeVariable('three')); Chris@13: $this->assertNull($shell->getScopeVariable('_')); Chris@13: } Chris@13: Chris@13: public function testIncludes() Chris@13: { Chris@13: $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']); Chris@13: Chris@13: $shell = new Shell($config); Chris@13: $this->assertEmpty($shell->getIncludes()); Chris@13: $shell->setIncludes(['foo', 'bar', 'baz']); Chris@13: $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes()); Chris@13: } Chris@13: Chris@13: public function testIncludesConfig() Chris@13: { Chris@13: $config = $this->getConfig([ Chris@13: 'defaultIncludes' => ['/file.php'], Chris@13: 'configFile' => __DIR__ . '/fixtures/empty.php', Chris@13: ]); Chris@13: Chris@13: $shell = new Shell($config); Chris@13: Chris@13: $includes = $shell->getIncludes(); Chris@13: $this->assertSame('/file.php', $includes[0]); Chris@13: } Chris@13: Chris@13: public function testAddMatchersViaConfig() Chris@13: { Chris@13: $shell = new FakeShell(); Chris@13: $matcher = new ClassMethodsMatcher(); Chris@13: Chris@13: $config = $this->getConfig([ Chris@13: 'matchers' => [$matcher], Chris@13: ]); Chris@13: $config->setShell($shell); Chris@13: Chris@13: $this->assertSame([$matcher], $shell->matchers); Chris@13: } Chris@13: Chris@13: public function testAddMatchersViaConfigAfterShell() Chris@13: { Chris@13: $shell = new FakeShell(); Chris@13: $matcher = new ClassMethodsMatcher(); Chris@13: Chris@13: $config = $this->getConfig([]); Chris@13: $config->setShell($shell); Chris@13: $config->addMatchers([$matcher]); Chris@13: Chris@13: $this->assertSame([$matcher], $shell->matchers); Chris@13: } Chris@13: Chris@13: public function testRenderingExceptions() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $e = new ParseErrorException('message', 13); Chris@13: Chris@13: $shell->setOutput($output); Chris@13: $shell->addCode('code'); Chris@13: $this->assertTrue($shell->hasCode()); Chris@13: $this->assertNotEmpty($shell->getCodeBuffer()); Chris@13: Chris@13: $shell->writeException($e); Chris@13: Chris@13: $this->assertSame($e, $shell->getScopeVariable('_e')); Chris@13: $this->assertFalse($shell->hasCode()); Chris@13: $this->assertEmpty($shell->getCodeBuffer()); Chris@13: Chris@17: \rewind($stream); Chris@17: $streamContents = \stream_get_contents($stream); Chris@13: Chris@13: $this->assertContains('PHP Parse error', $streamContents); Chris@13: $this->assertContains('message', $streamContents); Chris@13: $this->assertContains('line 13', $streamContents); Chris@13: } Chris@13: Chris@13: public function testHandlingErrors() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell->setOutput($output); Chris@13: Chris@17: $oldLevel = \error_reporting(); Chris@17: \error_reporting($oldLevel & ~E_USER_NOTICE); Chris@13: Chris@13: try { Chris@13: $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); Chris@13: } catch (ErrorException $e) { Chris@17: \error_reporting($oldLevel); Chris@13: $this->fail('Unexpected error exception'); Chris@13: } Chris@17: \error_reporting($oldLevel); Chris@13: Chris@17: \rewind($stream); Chris@17: $streamContents = \stream_get_contents($stream); Chris@13: Chris@13: $this->assertContains('PHP Notice:', $streamContents); Chris@13: $this->assertContains('wheee', $streamContents); Chris@13: $this->assertContains('line 13', $streamContents); Chris@13: } Chris@13: Chris@13: /** Chris@13: * @expectedException \Psy\Exception\ErrorException Chris@13: */ Chris@13: public function testNotHandlingErrors() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@17: $oldLevel = \error_reporting(); Chris@17: \error_reporting($oldLevel | E_USER_NOTICE); Chris@13: Chris@13: try { Chris@13: $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); Chris@13: } catch (ErrorException $e) { Chris@17: \error_reporting($oldLevel); Chris@13: throw $e; Chris@13: } Chris@13: } Chris@13: Chris@13: public function testVersion() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: Chris@13: $this->assertInstanceOf('Symfony\Component\Console\Application', $shell); Chris@13: $this->assertContains(Shell::VERSION, $shell->getVersion()); Chris@17: $this->assertContains(PHP_VERSION, $shell->getVersion()); Chris@17: $this->assertContains(PHP_SAPI, $shell->getVersion()); Chris@13: } Chris@13: Chris@13: public function testCodeBuffer() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: Chris@13: $shell->addCode('class'); Chris@13: $this->assertNull($shell->flushCode()); Chris@13: $this->assertTrue($shell->hasCode()); Chris@13: Chris@13: $shell->addCode('a'); Chris@13: $this->assertNull($shell->flushCode()); Chris@13: $this->assertTrue($shell->hasCode()); Chris@13: Chris@13: $shell->addCode('{}'); Chris@13: $code = $shell->flushCode(); Chris@13: $this->assertFalse($shell->hasCode()); Chris@17: $code = \preg_replace('/\s+/', ' ', $code); Chris@13: $this->assertNotNull($code); Chris@13: $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code); Chris@13: } Chris@13: Chris@13: public function testKeepCodeBufferOpen() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: Chris@13: $shell->addCode('1 \\'); Chris@13: $this->assertNull($shell->flushCode()); Chris@13: $this->assertTrue($shell->hasCode()); Chris@13: Chris@13: $shell->addCode('+ 1 \\'); Chris@13: $this->assertNull($shell->flushCode()); Chris@13: $this->assertTrue($shell->hasCode()); Chris@13: Chris@13: $shell->addCode('+ 1'); Chris@13: $code = $shell->flushCode(); Chris@13: $this->assertFalse($shell->hasCode()); Chris@17: $code = \preg_replace('/\s+/', ' ', $code); Chris@13: $this->assertNotNull($code); Chris@13: $this->assertSame('return 1 + 1 + 1;', $code); Chris@13: } Chris@13: Chris@13: /** Chris@13: * @expectedException \Psy\Exception\ParseErrorException Chris@13: */ Chris@13: public function testCodeBufferThrowsParseExceptions() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->addCode('this is not valid'); Chris@13: $shell->flushCode(); Chris@13: } Chris@13: Chris@13: public function testClosuresSupport() Chris@13: { Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $code = '$test = function () {}'; Chris@13: $shell->addCode($code); Chris@13: $shell->flushCode(); Chris@13: $code = '$test()'; Chris@13: $shell->addCode($code); Chris@13: $this->assertSame($shell->flushCode(), 'return $test();'); Chris@13: } Chris@13: Chris@13: public function testWriteStdout() Chris@13: { Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setOutput($output); Chris@13: Chris@13: $shell->writeStdout("{{stdout}}\n"); Chris@13: Chris@17: \rewind($stream); Chris@17: $streamContents = \stream_get_contents($stream); Chris@13: Chris@13: $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents); Chris@13: } Chris@13: Chris@13: public function testWriteStdoutWithoutNewline() Chris@13: { Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setOutput($output); Chris@13: Chris@13: $shell->writeStdout('{{stdout}}'); Chris@13: Chris@17: \rewind($stream); Chris@17: $streamContents = \stream_get_contents($stream); Chris@13: Chris@13: $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents); Chris@13: } Chris@13: Chris@13: /** Chris@13: * @dataProvider getReturnValues Chris@13: */ Chris@13: public function testWriteReturnValue($input, $expected) Chris@13: { Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setOutput($output); Chris@13: Chris@13: $shell->writeReturnValue($input); Chris@17: \rewind($stream); Chris@17: $this->assertEquals($expected, \stream_get_contents($stream)); Chris@13: } Chris@13: Chris@13: public function getReturnValues() Chris@13: { Chris@13: return [ Chris@13: ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL], Chris@13: [1, "=> \033[35m1\033[39m" . PHP_EOL], Chris@13: ]; Chris@13: } Chris@13: Chris@13: /** Chris@13: * @dataProvider getRenderedExceptions Chris@13: */ Chris@13: public function testWriteException($exception, $expected) Chris@13: { Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setOutput($output); Chris@13: Chris@13: $shell->writeException($exception); Chris@17: \rewind($stream); Chris@17: $this->assertSame($expected, \stream_get_contents($stream)); Chris@13: } Chris@13: Chris@13: public function getRenderedExceptions() Chris@13: { Chris@13: return [ Chris@13: [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL], Chris@13: ]; Chris@13: } Chris@13: Chris@13: /** Chris@13: * @dataProvider getExecuteValues Chris@13: */ Chris@13: public function testShellExecute($input, $expected) Chris@13: { Chris@13: $output = $this->getOutput(); Chris@13: $stream = $output->getStream(); Chris@13: $shell = new Shell($this->getConfig()); Chris@13: $shell->setOutput($output); Chris@13: $this->assertEquals($expected, $shell->execute($input)); Chris@17: \rewind($stream); Chris@17: $this->assertSame('', \stream_get_contents($stream)); Chris@13: } Chris@13: Chris@13: public function getExecuteValues() Chris@13: { Chris@13: return [ Chris@13: ['return 12', 12], Chris@13: ['"{{return value}}"', '{{return value}}'], Chris@13: ['1', '1'], Chris@13: ]; Chris@13: } Chris@13: Chris@16: /** Chris@16: * @dataProvider commandsToHas Chris@16: */ Chris@16: public function testHasCommand($command, $has) Chris@16: { Chris@16: $shell = new Shell($this->getConfig()); Chris@16: Chris@16: // :-/ Chris@16: $refl = new \ReflectionClass('Psy\\Shell'); Chris@16: $method = $refl->getMethod('hasCommand'); Chris@16: $method->setAccessible(true); Chris@16: Chris@16: $this->assertEquals($method->invokeArgs($shell, [$command]), $has); Chris@16: } Chris@16: Chris@16: public function commandsToHas() Chris@16: { Chris@16: return [ Chris@16: ['help', true], Chris@16: ['help help', true], Chris@16: ['"help"', false], Chris@16: ['"help help"', false], Chris@16: ['ls -al ', true], Chris@16: ['ls "-al" ', true], Chris@16: ['ls"-al"', false], Chris@16: [' q', true], Chris@16: [' q --help', true], Chris@16: ['"q"', false], Chris@16: ['"q",', false], Chris@16: ]; Chris@16: } Chris@16: Chris@13: private function getOutput() Chris@13: { Chris@17: $stream = \fopen('php://memory', 'w+'); Chris@13: $this->streams[] = $stream; Chris@13: Chris@13: $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false); Chris@13: Chris@13: return $output; Chris@13: } Chris@13: Chris@13: private function getConfig(array $config = []) Chris@13: { Chris@13: // Mebbe there's a better way than this? Chris@17: $dir = \tempnam(\sys_get_temp_dir(), 'psysh_shell_test_'); Chris@17: \unlink($dir); Chris@13: Chris@13: $defaults = [ Chris@13: 'configDir' => $dir, Chris@13: 'dataDir' => $dir, Chris@13: 'runtimeDir' => $dir, Chris@13: ]; Chris@13: Chris@17: return new Configuration(\array_merge($defaults, $config)); Chris@13: } Chris@13: }