ShellTest.php 10.0 KB

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