ShellTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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 = [];
  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->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames());
  38. $this->assertSame('banana', $shell->getScopeVariable('one'));
  39. $this->assertSame(123, $shell->getScopeVariable('two'));
  40. $this->assertSame($three, $shell->getScopeVariable('three'));
  41. $this->assertNull($shell->getScopeVariable('_'));
  42. $diff = $shell->getScopeVariablesDiff(['one' => $one, 'two' => 'not two']);
  43. $this->assertSame(['two' => $two, 'three' => $three, '_' => null], $diff);
  44. $shell->setScopeVariables([]);
  45. $this->assertSame(['_'], $shell->getScopeVariableNames());
  46. $shell->setBoundObject($this);
  47. $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
  48. $this->assertSame($this, $shell->getScopeVariable('this'));
  49. $this->assertSame(['_' => null], $shell->getScopeVariables(false));
  50. $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables());
  51. }
  52. /**
  53. * @expectedException \InvalidArgumentException
  54. */
  55. public function testUnknownScopeVariablesThrowExceptions()
  56. {
  57. $shell = new Shell($this->getConfig());
  58. $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]);
  59. $shell->getScopeVariable('baz');
  60. }
  61. public function testIncludesWithScopeVariables()
  62. {
  63. $one = 'banana';
  64. $two = 123;
  65. $three = new \StdClass();
  66. $__psysh__ = 'ignore this';
  67. $_ = 'ignore this';
  68. $_e = 'ignore this';
  69. $config = $this->getConfig(['usePcntl' => false]);
  70. $shell = new Shell($config);
  71. $shell->setScopeVariables(\compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
  72. $shell->addInput('exit', true);
  73. // This is super slow and we shouldn't do this :(
  74. $shell->run(null, $this->getOutput());
  75. $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
  76. $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames());
  77. $this->assertSame('banana', $shell->getScopeVariable('one'));
  78. $this->assertSame(123, $shell->getScopeVariable('two'));
  79. $this->assertSame($three, $shell->getScopeVariable('three'));
  80. $this->assertNull($shell->getScopeVariable('_'));
  81. }
  82. public function testIncludes()
  83. {
  84. $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']);
  85. $shell = new Shell($config);
  86. $this->assertEmpty($shell->getIncludes());
  87. $shell->setIncludes(['foo', 'bar', 'baz']);
  88. $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes());
  89. }
  90. public function testIncludesConfig()
  91. {
  92. $config = $this->getConfig([
  93. 'defaultIncludes' => ['/file.php'],
  94. 'configFile' => __DIR__ . '/fixtures/empty.php',
  95. ]);
  96. $shell = new Shell($config);
  97. $includes = $shell->getIncludes();
  98. $this->assertSame('/file.php', $includes[0]);
  99. }
  100. public function testAddMatchersViaConfig()
  101. {
  102. $shell = new FakeShell();
  103. $matcher = new ClassMethodsMatcher();
  104. $config = $this->getConfig([
  105. 'matchers' => [$matcher],
  106. ]);
  107. $config->setShell($shell);
  108. $this->assertSame([$matcher], $shell->matchers);
  109. }
  110. public function testAddMatchersViaConfigAfterShell()
  111. {
  112. $shell = new FakeShell();
  113. $matcher = new ClassMethodsMatcher();
  114. $config = $this->getConfig([]);
  115. $config->setShell($shell);
  116. $config->addMatchers([$matcher]);
  117. $this->assertSame([$matcher], $shell->matchers);
  118. }
  119. public function testRenderingExceptions()
  120. {
  121. $shell = new Shell($this->getConfig());
  122. $output = $this->getOutput();
  123. $stream = $output->getStream();
  124. $e = new ParseErrorException('message', 13);
  125. $shell->setOutput($output);
  126. $shell->addCode('code');
  127. $this->assertTrue($shell->hasCode());
  128. $this->assertNotEmpty($shell->getCodeBuffer());
  129. $shell->writeException($e);
  130. $this->assertSame($e, $shell->getScopeVariable('_e'));
  131. $this->assertFalse($shell->hasCode());
  132. $this->assertEmpty($shell->getCodeBuffer());
  133. \rewind($stream);
  134. $streamContents = \stream_get_contents($stream);
  135. $this->assertContains('PHP Parse error', $streamContents);
  136. $this->assertContains('message', $streamContents);
  137. $this->assertContains('line 13', $streamContents);
  138. }
  139. public function testHandlingErrors()
  140. {
  141. $shell = new Shell($this->getConfig());
  142. $output = $this->getOutput();
  143. $stream = $output->getStream();
  144. $shell->setOutput($output);
  145. $oldLevel = \error_reporting();
  146. \error_reporting($oldLevel & ~E_USER_NOTICE);
  147. try {
  148. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  149. } catch (ErrorException $e) {
  150. \error_reporting($oldLevel);
  151. $this->fail('Unexpected error exception');
  152. }
  153. \error_reporting($oldLevel);
  154. \rewind($stream);
  155. $streamContents = \stream_get_contents($stream);
  156. $this->assertContains('PHP Notice:', $streamContents);
  157. $this->assertContains('wheee', $streamContents);
  158. $this->assertContains('line 13', $streamContents);
  159. }
  160. /**
  161. * @expectedException \Psy\Exception\ErrorException
  162. */
  163. public function testNotHandlingErrors()
  164. {
  165. $shell = new Shell($this->getConfig());
  166. $oldLevel = \error_reporting();
  167. \error_reporting($oldLevel | E_USER_NOTICE);
  168. try {
  169. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  170. } catch (ErrorException $e) {
  171. \error_reporting($oldLevel);
  172. throw $e;
  173. }
  174. }
  175. public function testVersion()
  176. {
  177. $shell = new Shell($this->getConfig());
  178. $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
  179. $this->assertContains(Shell::VERSION, $shell->getVersion());
  180. $this->assertContains(PHP_VERSION, $shell->getVersion());
  181. $this->assertContains(PHP_SAPI, $shell->getVersion());
  182. }
  183. public function testCodeBuffer()
  184. {
  185. $shell = new Shell($this->getConfig());
  186. $shell->addCode('class');
  187. $this->assertNull($shell->flushCode());
  188. $this->assertTrue($shell->hasCode());
  189. $shell->addCode('a');
  190. $this->assertNull($shell->flushCode());
  191. $this->assertTrue($shell->hasCode());
  192. $shell->addCode('{}');
  193. $code = $shell->flushCode();
  194. $this->assertFalse($shell->hasCode());
  195. $code = \preg_replace('/\s+/', ' ', $code);
  196. $this->assertNotNull($code);
  197. $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
  198. }
  199. public function testKeepCodeBufferOpen()
  200. {
  201. $shell = new Shell($this->getConfig());
  202. $shell->addCode('1 \\');
  203. $this->assertNull($shell->flushCode());
  204. $this->assertTrue($shell->hasCode());
  205. $shell->addCode('+ 1 \\');
  206. $this->assertNull($shell->flushCode());
  207. $this->assertTrue($shell->hasCode());
  208. $shell->addCode('+ 1');
  209. $code = $shell->flushCode();
  210. $this->assertFalse($shell->hasCode());
  211. $code = \preg_replace('/\s+/', ' ', $code);
  212. $this->assertNotNull($code);
  213. $this->assertSame('return 1 + 1 + 1;', $code);
  214. }
  215. /**
  216. * @expectedException \Psy\Exception\ParseErrorException
  217. */
  218. public function testCodeBufferThrowsParseExceptions()
  219. {
  220. $shell = new Shell($this->getConfig());
  221. $shell->addCode('this is not valid');
  222. $shell->flushCode();
  223. }
  224. public function testClosuresSupport()
  225. {
  226. $shell = new Shell($this->getConfig());
  227. $code = '$test = function () {}';
  228. $shell->addCode($code);
  229. $shell->flushCode();
  230. $code = '$test()';
  231. $shell->addCode($code);
  232. $this->assertSame($shell->flushCode(), 'return $test();');
  233. }
  234. public function testWriteStdout()
  235. {
  236. $output = $this->getOutput();
  237. $stream = $output->getStream();
  238. $shell = new Shell($this->getConfig());
  239. $shell->setOutput($output);
  240. $shell->writeStdout("{{stdout}}\n");
  241. \rewind($stream);
  242. $streamContents = \stream_get_contents($stream);
  243. $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
  244. }
  245. public function testWriteStdoutWithoutNewline()
  246. {
  247. $output = $this->getOutput();
  248. $stream = $output->getStream();
  249. $shell = new Shell($this->getConfig());
  250. $shell->setOutput($output);
  251. $shell->writeStdout('{{stdout}}');
  252. \rewind($stream);
  253. $streamContents = \stream_get_contents($stream);
  254. $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
  255. }
  256. /**
  257. * @dataProvider getReturnValues
  258. */
  259. public function testWriteReturnValue($input, $expected)
  260. {
  261. $output = $this->getOutput();
  262. $stream = $output->getStream();
  263. $shell = new Shell($this->getConfig());
  264. $shell->setOutput($output);
  265. $shell->writeReturnValue($input);
  266. \rewind($stream);
  267. $this->assertEquals($expected, \stream_get_contents($stream));
  268. }
  269. public function getReturnValues()
  270. {
  271. return [
  272. ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL],
  273. [1, "=> \033[35m1\033[39m" . PHP_EOL],
  274. ];
  275. }
  276. /**
  277. * @dataProvider getRenderedExceptions
  278. */
  279. public function testWriteException($exception, $expected)
  280. {
  281. $output = $this->getOutput();
  282. $stream = $output->getStream();
  283. $shell = new Shell($this->getConfig());
  284. $shell->setOutput($output);
  285. $shell->writeException($exception);
  286. \rewind($stream);
  287. $this->assertSame($expected, \stream_get_contents($stream));
  288. }
  289. public function getRenderedExceptions()
  290. {
  291. return [
  292. [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL],
  293. ];
  294. }
  295. /**
  296. * @dataProvider getExecuteValues
  297. */
  298. public function testShellExecute($input, $expected)
  299. {
  300. $output = $this->getOutput();
  301. $stream = $output->getStream();
  302. $shell = new Shell($this->getConfig());
  303. $shell->setOutput($output);
  304. $this->assertEquals($expected, $shell->execute($input));
  305. \rewind($stream);
  306. $this->assertSame('', \stream_get_contents($stream));
  307. }
  308. public function getExecuteValues()
  309. {
  310. return [
  311. ['return 12', 12],
  312. ['"{{return value}}"', '{{return value}}'],
  313. ['1', '1'],
  314. ];
  315. }
  316. /**
  317. * @dataProvider commandsToHas
  318. */
  319. public function testHasCommand($command, $has)
  320. {
  321. $shell = new Shell($this->getConfig());
  322. // :-/
  323. $refl = new \ReflectionClass('Psy\\Shell');
  324. $method = $refl->getMethod('hasCommand');
  325. $method->setAccessible(true);
  326. $this->assertEquals($method->invokeArgs($shell, [$command]), $has);
  327. }
  328. public function commandsToHas()
  329. {
  330. return [
  331. ['help', true],
  332. ['help help', true],
  333. ['"help"', false],
  334. ['"help help"', false],
  335. ['ls -al ', true],
  336. ['ls "-al" ', true],
  337. ['ls"-al"', false],
  338. [' q', true],
  339. [' q --help', true],
  340. ['"q"', false],
  341. ['"q",', false],
  342. ];
  343. }
  344. private function getOutput()
  345. {
  346. $stream = \fopen('php://memory', 'w+');
  347. $this->streams[] = $stream;
  348. $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
  349. return $output;
  350. }
  351. private function getConfig(array $config = [])
  352. {
  353. // Mebbe there's a better way than this?
  354. $dir = \tempnam(\sys_get_temp_dir(), 'psysh_shell_test_');
  355. \unlink($dir);
  356. $defaults = [
  357. 'configDir' => $dir,
  358. 'dataDir' => $dir,
  359. 'runtimeDir' => $dir,
  360. ];
  361. return new Configuration(\array_merge($defaults, $config));
  362. }
  363. }