ShellTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test;
  11. use Psy\Configuration;
  12. use Psy\Exception\ErrorException;
  13. use Psy\Exception\ParseErrorException;
  14. use Psy\Shell;
  15. use Psy\TabCompletion\Matcher\ClassMethodsMatcher;
  16. use Symfony\Component\Console\Output\StreamOutput;
  17. class ShellTest extends \PHPUnit_Framework_TestCase
  18. {
  19. private $streams = array();
  20. public function tearDown()
  21. {
  22. foreach ($this->streams as $stream) {
  23. fclose($stream);
  24. }
  25. }
  26. public function testScopeVariables()
  27. {
  28. $one = 'banana';
  29. $two = 123;
  30. $three = new \StdClass();
  31. $__psysh__ = 'ignore this';
  32. $_ = 'ignore this';
  33. $_e = 'ignore this';
  34. $shell = new Shell($this->getConfig());
  35. $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
  36. $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
  37. $this->assertEquals(array('one', 'two', 'three', '_'), $shell->getScopeVariableNames());
  38. $this->assertEquals('banana', $shell->getScopeVariable('one'));
  39. $this->assertEquals(123, $shell->getScopeVariable('two'));
  40. $this->assertSame($three, $shell->getScopeVariable('three'));
  41. $this->assertNull($shell->getScopeVariable('_'));
  42. $shell->setScopeVariables(array());
  43. $this->assertEquals(array('_'), $shell->getScopeVariableNames());
  44. $shell->setBoundObject($this);
  45. $this->assertEquals(array('_', 'this'), $shell->getScopeVariableNames());
  46. $this->assertSame($this, $shell->getScopeVariable('this'));
  47. $this->assertEquals(array('_' => null), $shell->getScopeVariables(false));
  48. $this->assertEquals(array('_' => null, 'this' => $this), $shell->getScopeVariables());
  49. }
  50. /**
  51. * @expectedException \InvalidArgumentException
  52. */
  53. public function testUnknownScopeVariablesThrowExceptions()
  54. {
  55. $shell = new Shell($this->getConfig());
  56. $shell->setScopeVariables(array('foo' => 'FOO', 'bar' => 1));
  57. $shell->getScopeVariable('baz');
  58. }
  59. public function testIncludes()
  60. {
  61. $config = $this->getConfig(array('configFile' => __DIR__ . '/../../fixtures/empty.php'));
  62. $shell = new Shell($config);
  63. $this->assertEmpty($shell->getIncludes());
  64. $shell->setIncludes(array('foo', 'bar', 'baz'));
  65. $this->assertEquals(array('foo', 'bar', 'baz'), $shell->getIncludes());
  66. }
  67. public function testIncludesConfig()
  68. {
  69. $config = $this->getConfig(array(
  70. 'defaultIncludes' => array('/file.php'),
  71. 'configFile' => __DIR__ . '/../../fixtures/empty.php',
  72. ));
  73. $shell = new Shell($config);
  74. $includes = $shell->getIncludes();
  75. $this->assertEquals('/file.php', $includes[0]);
  76. }
  77. public function testAddMatchersViaConfig()
  78. {
  79. $config = $this->getConfig(array(
  80. 'tabCompletionMatchers' => array(
  81. new ClassMethodsMatcher(),
  82. ),
  83. ));
  84. $matchers = $config->getTabCompletionMatchers();
  85. $this->assertTrue(array_pop($matchers) instanceof ClassMethodsMatcher);
  86. }
  87. public function testRenderingExceptions()
  88. {
  89. $shell = new Shell($this->getConfig());
  90. $output = $this->getOutput();
  91. $stream = $output->getStream();
  92. $e = new ParseErrorException('message', 13);
  93. $shell->setOutput($output);
  94. $shell->addCode('code');
  95. $this->assertTrue($shell->hasCode());
  96. $this->assertNotEmpty($shell->getCodeBuffer());
  97. $shell->writeException($e);
  98. $this->assertSame($e, $shell->getScopeVariable('_e'));
  99. $this->assertFalse($shell->hasCode());
  100. $this->assertEmpty($shell->getCodeBuffer());
  101. rewind($stream);
  102. $streamContents = stream_get_contents($stream);
  103. $this->assertContains('PHP Parse error', $streamContents);
  104. $this->assertContains('message', $streamContents);
  105. $this->assertContains('line 13', $streamContents);
  106. }
  107. public function testHandlingErrors()
  108. {
  109. $shell = new Shell($this->getConfig());
  110. $output = $this->getOutput();
  111. $stream = $output->getStream();
  112. $shell->setOutput($output);
  113. $oldLevel = error_reporting();
  114. error_reporting($oldLevel & ~E_USER_NOTICE);
  115. try {
  116. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  117. } catch (ErrorException $e) {
  118. error_reporting($oldLevel);
  119. $this->fail('Unexpected error exception');
  120. }
  121. error_reporting($oldLevel);
  122. rewind($stream);
  123. $streamContents = stream_get_contents($stream);
  124. $this->assertContains('PHP error:', $streamContents);
  125. $this->assertContains('wheee', $streamContents);
  126. $this->assertContains('line 13', $streamContents);
  127. }
  128. /**
  129. * @expectedException \Psy\Exception\ErrorException
  130. */
  131. public function testNotHandlingErrors()
  132. {
  133. $shell = new Shell($this->getConfig());
  134. $oldLevel = error_reporting();
  135. error_reporting($oldLevel | E_USER_NOTICE);
  136. try {
  137. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  138. } catch (ErrorException $e) {
  139. error_reporting($oldLevel);
  140. throw $e;
  141. }
  142. }
  143. public function testVersion()
  144. {
  145. $shell = new Shell($this->getConfig());
  146. $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
  147. $this->assertContains(Shell::VERSION, $shell->getVersion());
  148. $this->assertContains(phpversion(), $shell->getVersion());
  149. $this->assertContains(php_sapi_name(), $shell->getVersion());
  150. }
  151. public function testCodeBuffer()
  152. {
  153. $shell = new Shell($this->getConfig());
  154. $shell->addCode('class');
  155. $this->assertNull($shell->flushCode());
  156. $this->assertTrue($shell->hasCode());
  157. $shell->addCode('a');
  158. $this->assertNull($shell->flushCode());
  159. $this->assertTrue($shell->hasCode());
  160. $shell->addCode('{}');
  161. $code = $shell->flushCode();
  162. $this->assertFalse($shell->hasCode());
  163. $code = preg_replace('/\s+/', ' ', $code);
  164. $this->assertNotNull($code);
  165. $this->assertEquals('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
  166. }
  167. public function testKeepCodeBufferOpen()
  168. {
  169. $shell = new Shell($this->getConfig());
  170. $shell->addCode('1 \\');
  171. $this->assertNull($shell->flushCode());
  172. $this->assertTrue($shell->hasCode());
  173. $shell->addCode('+ 1 \\');
  174. $this->assertNull($shell->flushCode());
  175. $this->assertTrue($shell->hasCode());
  176. $shell->addCode('+ 1');
  177. $code = $shell->flushCode();
  178. $this->assertFalse($shell->hasCode());
  179. $code = preg_replace('/\s+/', ' ', $code);
  180. $this->assertNotNull($code);
  181. $this->assertEquals('return 1 + 1 + 1;', $code);
  182. }
  183. /**
  184. * @expectedException \Psy\Exception\ParseErrorException
  185. */
  186. public function testCodeBufferThrowsParseExceptions()
  187. {
  188. $shell = new Shell($this->getConfig());
  189. $shell->addCode('this is not valid');
  190. $shell->flushCode();
  191. }
  192. public function testClosuresSupport()
  193. {
  194. $shell = new Shell($this->getConfig());
  195. $code = '$test = function () {}';
  196. $shell->addCode($code);
  197. $shell->flushCode();
  198. $code = '$test()';
  199. $shell->addCode($code);
  200. $shell->flushCode();
  201. }
  202. public function testWriteStdout()
  203. {
  204. $output = $this->getOutput();
  205. $stream = $output->getStream();
  206. $shell = new Shell($this->getConfig());
  207. $shell->setOutput($output);
  208. $shell->writeStdout("{{stdout}}\n");
  209. rewind($stream);
  210. $streamContents = stream_get_contents($stream);
  211. $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents);
  212. }
  213. public function testWriteStdoutWithoutNewline()
  214. {
  215. $output = $this->getOutput();
  216. $stream = $output->getStream();
  217. $shell = new Shell($this->getConfig());
  218. $shell->setOutput($output);
  219. $shell->writeStdout('{{stdout}}');
  220. rewind($stream);
  221. $streamContents = stream_get_contents($stream);
  222. $this->assertEquals('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
  223. }
  224. /**
  225. * @dataProvider getReturnValues
  226. */
  227. public function testWriteReturnValue($input, $expected)
  228. {
  229. $output = $this->getOutput();
  230. $stream = $output->getStream();
  231. $shell = new Shell($this->getConfig());
  232. $shell->setOutput($output);
  233. $shell->writeReturnValue($input);
  234. rewind($stream);
  235. $this->assertEquals($expected, stream_get_contents($stream));
  236. }
  237. public function getReturnValues()
  238. {
  239. return array(
  240. array('{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL),
  241. array(1, "=> \033[35m1\033[39m" . PHP_EOL),
  242. );
  243. }
  244. /**
  245. * @dataProvider getRenderedExceptions
  246. */
  247. public function testWriteException($exception, $expected)
  248. {
  249. $output = $this->getOutput();
  250. $stream = $output->getStream();
  251. $shell = new Shell($this->getConfig());
  252. $shell->setOutput($output);
  253. $shell->writeException($exception);
  254. rewind($stream);
  255. $this->assertEquals($expected, stream_get_contents($stream));
  256. }
  257. public function getRenderedExceptions()
  258. {
  259. return array(
  260. array(new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL),
  261. );
  262. }
  263. private function getOutput()
  264. {
  265. $stream = fopen('php://memory', 'w+');
  266. $this->streams[] = $stream;
  267. $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
  268. return $output;
  269. }
  270. private function getConfig(array $config = array())
  271. {
  272. // Mebbe there's a better way than this?
  273. $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_');
  274. unlink($dir);
  275. $defaults = array(
  276. 'configDir' => $dir,
  277. 'dataDir' => $dir,
  278. 'runtimeDir' => $dir,
  279. );
  280. return new Configuration(array_merge($defaults, $config));
  281. }
  282. }