/*
* This file is part of Psy Shell.
*
- * (c) 2012-2017 Justin Hileman
+ * (c) 2012-2018 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
class ShellTest extends \PHPUnit\Framework\TestCase
{
- private $streams = array();
+ private $streams = [];
public function tearDown()
{
$shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
$this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
- $this->assertEquals(array('one', 'two', 'three', '_'), $shell->getScopeVariableNames());
- $this->assertEquals('banana', $shell->getScopeVariable('one'));
- $this->assertEquals(123, $shell->getScopeVariable('two'));
+ $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames());
+ $this->assertSame('banana', $shell->getScopeVariable('one'));
+ $this->assertSame(123, $shell->getScopeVariable('two'));
$this->assertSame($three, $shell->getScopeVariable('three'));
$this->assertNull($shell->getScopeVariable('_'));
- $shell->setScopeVariables(array());
- $this->assertEquals(array('_'), $shell->getScopeVariableNames());
+ $shell->setScopeVariables([]);
+ $this->assertSame(['_'], $shell->getScopeVariableNames());
$shell->setBoundObject($this);
- $this->assertEquals(array('_', 'this'), $shell->getScopeVariableNames());
+ $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
$this->assertSame($this, $shell->getScopeVariable('this'));
- $this->assertEquals(array('_' => null), $shell->getScopeVariables(false));
- $this->assertEquals(array('_' => null, 'this' => $this), $shell->getScopeVariables());
+ $this->assertSame(['_' => null], $shell->getScopeVariables(false));
+ $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables());
}
/**
public function testUnknownScopeVariablesThrowExceptions()
{
$shell = new Shell($this->getConfig());
- $shell->setScopeVariables(array('foo' => 'FOO', 'bar' => 1));
+ $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]);
$shell->getScopeVariable('baz');
}
+ public function testIncludesWithScopeVariables()
+ {
+ $one = 'banana';
+ $two = 123;
+ $three = new \StdClass();
+ $__psysh__ = 'ignore this';
+ $_ = 'ignore this';
+ $_e = 'ignore this';
+
+ $config = $this->getConfig(['usePcntl' => false]);
+
+ $shell = new Shell($config);
+ $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
+ $shell->addInput('exit', true);
+
+ // This is super slow and we shouldn't do this :(
+ $shell->run(null, $this->getOutput());
+
+ $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
+ $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames());
+ $this->assertSame('banana', $shell->getScopeVariable('one'));
+ $this->assertSame(123, $shell->getScopeVariable('two'));
+ $this->assertSame($three, $shell->getScopeVariable('three'));
+ $this->assertNull($shell->getScopeVariable('_'));
+ }
+
public function testIncludes()
{
- $config = $this->getConfig(array('configFile' => __DIR__ . '/../../fixtures/empty.php'));
+ $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']);
$shell = new Shell($config);
$this->assertEmpty($shell->getIncludes());
- $shell->setIncludes(array('foo', 'bar', 'baz'));
- $this->assertEquals(array('foo', 'bar', 'baz'), $shell->getIncludes());
+ $shell->setIncludes(['foo', 'bar', 'baz']);
+ $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes());
}
public function testIncludesConfig()
{
- $config = $this->getConfig(array(
- 'defaultIncludes' => array('/file.php'),
- 'configFile' => __DIR__ . '/../../fixtures/empty.php',
- ));
+ $config = $this->getConfig([
+ 'defaultIncludes' => ['/file.php'],
+ 'configFile' => __DIR__ . '/fixtures/empty.php',
+ ]);
$shell = new Shell($config);
$includes = $shell->getIncludes();
- $this->assertEquals('/file.php', $includes[0]);
+ $this->assertSame('/file.php', $includes[0]);
}
public function testAddMatchersViaConfig()
{
- $config = $this->getConfig(array(
- 'tabCompletionMatchers' => array(
- new ClassMethodsMatcher(),
- ),
- ));
+ $shell = new FakeShell();
+ $matcher = new ClassMethodsMatcher();
- $matchers = $config->getTabCompletionMatchers();
+ $config = $this->getConfig([
+ 'matchers' => [$matcher],
+ ]);
+ $config->setShell($shell);
- $this->assertTrue(array_pop($matchers) instanceof ClassMethodsMatcher);
+ $this->assertSame([$matcher], $shell->matchers);
+ }
+
+ public function testAddMatchersViaConfigAfterShell()
+ {
+ $shell = new FakeShell();
+ $matcher = new ClassMethodsMatcher();
+
+ $config = $this->getConfig([]);
+ $config->setShell($shell);
+ $config->addMatchers([$matcher]);
+
+ $this->assertSame([$matcher], $shell->matchers);
}
public function testRenderingExceptions()
$this->assertFalse($shell->hasCode());
$code = preg_replace('/\s+/', ' ', $code);
$this->assertNotNull($code);
- $this->assertEquals('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
+ $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
}
public function testKeepCodeBufferOpen()
$this->assertFalse($shell->hasCode());
$code = preg_replace('/\s+/', ' ', $code);
$this->assertNotNull($code);
- $this->assertEquals('return 1 + 1 + 1;', $code);
+ $this->assertSame('return 1 + 1 + 1;', $code);
}
/**
$shell->flushCode();
$code = '$test()';
$shell->addCode($code);
- $this->assertEquals($shell->flushCode(), 'return $test();');
+ $this->assertSame($shell->flushCode(), 'return $test();');
}
public function testWriteStdout()
rewind($stream);
$streamContents = stream_get_contents($stream);
- $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents);
+ $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
}
public function testWriteStdoutWithoutNewline()
rewind($stream);
$streamContents = stream_get_contents($stream);
- $this->assertEquals('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
+ $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
}
/**
public function getReturnValues()
{
- return array(
- array('{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL),
- array(1, "=> \033[35m1\033[39m" . PHP_EOL),
- );
+ return [
+ ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL],
+ [1, "=> \033[35m1\033[39m" . PHP_EOL],
+ ];
}
/**
$shell->writeException($exception);
rewind($stream);
- $this->assertEquals($expected, stream_get_contents($stream));
+ $this->assertSame($expected, stream_get_contents($stream));
}
public function getRenderedExceptions()
{
- return array(
- array(new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL),
- );
+ return [
+ [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL],
+ ];
+ }
+
+ /**
+ * @dataProvider getExecuteValues
+ */
+ public function testShellExecute($input, $expected)
+ {
+ $output = $this->getOutput();
+ $stream = $output->getStream();
+ $shell = new Shell($this->getConfig());
+ $shell->setOutput($output);
+ $this->assertEquals($expected, $shell->execute($input));
+ rewind($stream);
+ $this->assertSame('', stream_get_contents($stream));
+ }
+
+ public function getExecuteValues()
+ {
+ return [
+ ['return 12', 12],
+ ['"{{return value}}"', '{{return value}}'],
+ ['1', '1'],
+ ];
}
private function getOutput()
return $output;
}
- private function getConfig(array $config = array())
+ private function getConfig(array $config = [])
{
// Mebbe there's a better way than this?
$dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_');
unlink($dir);
- $defaults = array(
+ $defaults = [
'configDir' => $dir,
'dataDir' => $dir,
'runtimeDir' => $dir,
- );
+ ];
return new Configuration(array_merge($defaults, $config));
}