Chris@0: streams as $stream) { Chris@0: fclose($stream); Chris@0: } Chris@0: } Chris@0: Chris@0: public function testScopeVariables() Chris@0: { Chris@0: $one = 'banana'; Chris@0: $two = 123; Chris@0: $three = new \StdClass(); Chris@0: $__psysh__ = 'ignore this'; Chris@0: $_ = 'ignore this'; Chris@0: $_e = 'ignore this'; Chris@0: Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this')); Chris@0: Chris@0: $this->assertNotContains('__psysh__', $shell->getScopeVariableNames()); Chris@0: $this->assertEquals(array('one', 'two', 'three', '_'), $shell->getScopeVariableNames()); Chris@0: $this->assertEquals('banana', $shell->getScopeVariable('one')); Chris@0: $this->assertEquals(123, $shell->getScopeVariable('two')); Chris@0: $this->assertSame($three, $shell->getScopeVariable('three')); Chris@0: $this->assertNull($shell->getScopeVariable('_')); Chris@0: Chris@0: $shell->setScopeVariables(array()); Chris@0: $this->assertEquals(array('_'), $shell->getScopeVariableNames()); Chris@0: Chris@0: $shell->setBoundObject($this); Chris@0: $this->assertEquals(array('_', 'this'), $shell->getScopeVariableNames()); Chris@0: $this->assertSame($this, $shell->getScopeVariable('this')); Chris@0: $this->assertEquals(array('_' => null), $shell->getScopeVariables(false)); Chris@0: $this->assertEquals(array('_' => null, 'this' => $this), $shell->getScopeVariables()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \InvalidArgumentException Chris@0: */ Chris@0: public function testUnknownScopeVariablesThrowExceptions() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setScopeVariables(array('foo' => 'FOO', 'bar' => 1)); Chris@0: $shell->getScopeVariable('baz'); Chris@0: } Chris@0: Chris@0: public function testIncludes() Chris@0: { Chris@0: $config = $this->getConfig(array('configFile' => __DIR__ . '/../../fixtures/empty.php')); Chris@0: Chris@0: $shell = new Shell($config); Chris@0: $this->assertEmpty($shell->getIncludes()); Chris@0: $shell->setIncludes(array('foo', 'bar', 'baz')); Chris@0: $this->assertEquals(array('foo', 'bar', 'baz'), $shell->getIncludes()); Chris@0: } Chris@0: Chris@0: public function testIncludesConfig() Chris@0: { Chris@0: $config = $this->getConfig(array( Chris@0: 'defaultIncludes' => array('/file.php'), Chris@0: 'configFile' => __DIR__ . '/../../fixtures/empty.php', Chris@0: )); Chris@0: Chris@0: $shell = new Shell($config); Chris@0: Chris@0: $includes = $shell->getIncludes(); Chris@0: $this->assertEquals('/file.php', $includes[0]); Chris@0: } Chris@0: Chris@0: public function testAddMatchersViaConfig() Chris@0: { Chris@0: $config = $this->getConfig(array( Chris@0: 'tabCompletionMatchers' => array( Chris@0: new ClassMethodsMatcher(), Chris@0: ), Chris@0: )); Chris@0: Chris@0: $matchers = $config->getTabCompletionMatchers(); Chris@0: Chris@0: $this->assertTrue(array_pop($matchers) instanceof ClassMethodsMatcher); Chris@0: } Chris@0: Chris@0: public function testRenderingExceptions() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $e = new ParseErrorException('message', 13); Chris@0: Chris@0: $shell->setOutput($output); Chris@0: $shell->addCode('code'); Chris@0: $this->assertTrue($shell->hasCode()); Chris@0: $this->assertNotEmpty($shell->getCodeBuffer()); Chris@0: Chris@0: $shell->writeException($e); Chris@0: Chris@0: $this->assertSame($e, $shell->getScopeVariable('_e')); Chris@0: $this->assertFalse($shell->hasCode()); Chris@0: $this->assertEmpty($shell->getCodeBuffer()); Chris@0: Chris@0: rewind($stream); Chris@0: $streamContents = stream_get_contents($stream); Chris@0: Chris@0: $this->assertContains('PHP Parse error', $streamContents); Chris@0: $this->assertContains('message', $streamContents); Chris@0: $this->assertContains('line 13', $streamContents); Chris@0: } Chris@0: Chris@0: public function testHandlingErrors() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $shell->setOutput($output); Chris@0: Chris@0: $oldLevel = error_reporting(); Chris@0: error_reporting($oldLevel & ~E_USER_NOTICE); Chris@0: Chris@0: try { Chris@0: $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); Chris@0: } catch (ErrorException $e) { Chris@0: error_reporting($oldLevel); Chris@0: $this->fail('Unexpected error exception'); Chris@0: } Chris@0: error_reporting($oldLevel); Chris@0: Chris@0: rewind($stream); Chris@0: $streamContents = stream_get_contents($stream); Chris@0: Chris@0: $this->assertContains('PHP Notice:', $streamContents); Chris@0: $this->assertContains('wheee', $streamContents); Chris@0: $this->assertContains('line 13', $streamContents); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \Psy\Exception\ErrorException Chris@0: */ Chris@0: public function testNotHandlingErrors() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $oldLevel = error_reporting(); Chris@0: error_reporting($oldLevel | E_USER_NOTICE); Chris@0: Chris@0: try { Chris@0: $shell->handleError(E_USER_NOTICE, 'wheee', null, 13); Chris@0: } catch (ErrorException $e) { Chris@0: error_reporting($oldLevel); Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: Chris@0: public function testVersion() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: Chris@0: $this->assertInstanceOf('Symfony\Component\Console\Application', $shell); Chris@0: $this->assertContains(Shell::VERSION, $shell->getVersion()); Chris@0: $this->assertContains(phpversion(), $shell->getVersion()); Chris@0: $this->assertContains(php_sapi_name(), $shell->getVersion()); Chris@0: } Chris@0: Chris@0: public function testCodeBuffer() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: Chris@0: $shell->addCode('class'); Chris@0: $this->assertNull($shell->flushCode()); Chris@0: $this->assertTrue($shell->hasCode()); Chris@0: Chris@0: $shell->addCode('a'); Chris@0: $this->assertNull($shell->flushCode()); Chris@0: $this->assertTrue($shell->hasCode()); Chris@0: Chris@0: $shell->addCode('{}'); Chris@0: $code = $shell->flushCode(); Chris@0: $this->assertFalse($shell->hasCode()); Chris@0: $code = preg_replace('/\s+/', ' ', $code); Chris@0: $this->assertNotNull($code); Chris@0: $this->assertEquals('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code); Chris@0: } Chris@0: Chris@0: public function testKeepCodeBufferOpen() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: Chris@0: $shell->addCode('1 \\'); Chris@0: $this->assertNull($shell->flushCode()); Chris@0: $this->assertTrue($shell->hasCode()); Chris@0: Chris@0: $shell->addCode('+ 1 \\'); Chris@0: $this->assertNull($shell->flushCode()); Chris@0: $this->assertTrue($shell->hasCode()); Chris@0: Chris@0: $shell->addCode('+ 1'); Chris@0: $code = $shell->flushCode(); Chris@0: $this->assertFalse($shell->hasCode()); Chris@0: $code = preg_replace('/\s+/', ' ', $code); Chris@0: $this->assertNotNull($code); Chris@0: $this->assertEquals('return 1 + 1 + 1;', $code); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \Psy\Exception\ParseErrorException Chris@0: */ Chris@0: public function testCodeBufferThrowsParseExceptions() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->addCode('this is not valid'); Chris@0: $shell->flushCode(); Chris@0: } Chris@0: Chris@0: public function testClosuresSupport() Chris@0: { Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $code = '$test = function () {}'; Chris@0: $shell->addCode($code); Chris@0: $shell->flushCode(); Chris@0: $code = '$test()'; Chris@0: $shell->addCode($code); Chris@0: $shell->flushCode(); Chris@0: } Chris@0: Chris@0: public function testWriteStdout() Chris@0: { Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setOutput($output); Chris@0: Chris@0: $shell->writeStdout("{{stdout}}\n"); Chris@0: Chris@0: rewind($stream); Chris@0: $streamContents = stream_get_contents($stream); Chris@0: Chris@0: $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents); Chris@0: } Chris@0: Chris@0: public function testWriteStdoutWithoutNewline() Chris@0: { Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setOutput($output); Chris@0: Chris@0: $shell->writeStdout('{{stdout}}'); Chris@0: Chris@0: rewind($stream); Chris@0: $streamContents = stream_get_contents($stream); Chris@0: Chris@0: $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getReturnValues Chris@0: */ Chris@0: public function testWriteReturnValue($input, $expected) Chris@0: { Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setOutput($output); Chris@0: Chris@0: $shell->writeReturnValue($input); Chris@0: rewind($stream); Chris@0: $this->assertEquals($expected, stream_get_contents($stream)); Chris@0: } Chris@0: Chris@0: public function getReturnValues() Chris@0: { Chris@0: return array( Chris@0: array('{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL), Chris@0: array(1, "=> \033[35m1\033[39m" . PHP_EOL), Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getRenderedExceptions Chris@0: */ Chris@0: public function testWriteException($exception, $expected) Chris@0: { Chris@0: $output = $this->getOutput(); Chris@0: $stream = $output->getStream(); Chris@0: $shell = new Shell($this->getConfig()); Chris@0: $shell->setOutput($output); Chris@0: Chris@0: $shell->writeException($exception); Chris@0: rewind($stream); Chris@0: $this->assertEquals($expected, stream_get_contents($stream)); Chris@0: } Chris@0: Chris@0: public function getRenderedExceptions() Chris@0: { Chris@0: return array( Chris@0: array(new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL), Chris@0: ); Chris@0: } Chris@0: Chris@0: private function getOutput() Chris@0: { Chris@0: $stream = fopen('php://memory', 'w+'); Chris@0: $this->streams[] = $stream; Chris@0: Chris@0: $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false); Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: private function getConfig(array $config = array()) Chris@0: { Chris@0: // Mebbe there's a better way than this? Chris@0: $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_'); Chris@0: unlink($dir); Chris@0: Chris@0: $defaults = array( Chris@0: 'configDir' => $dir, Chris@0: 'dataDir' => $dir, Chris@0: 'runtimeDir' => $dir, Chris@0: ); Chris@0: Chris@0: return new Configuration(array_merge($defaults, $config)); Chris@0: } Chris@0: }