PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/psy/psysh/test/Psy/Test/ShellTest.php

https://gitlab.com/vincetang/mtweb
PHP | 339 lines | 253 code | 62 blank | 24 comment | 0 complexity | f2146455725ac774b6059ab0e83ad160 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell
  4. *
  5. * (c) 2012-2014 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->addCode('code');
  89. $this->assertTrue($shell->hasCode());
  90. $this->assertNotEmpty($shell->getCodeBuffer());
  91. $shell->renderException($e, $output);
  92. $this->assertSame($e, $shell->getScopeVariable('_e'));
  93. $this->assertFalse($shell->hasCode());
  94. $this->assertEmpty($shell->getCodeBuffer());
  95. rewind($stream);
  96. $streamContents = stream_get_contents($stream);
  97. $this->assertContains('PHP Parse error', $streamContents);
  98. $this->assertContains('message', $streamContents);
  99. $this->assertContains('line 13', $streamContents);
  100. }
  101. public function testHandlingErrors()
  102. {
  103. $shell = new Shell($this->getConfig());
  104. $output = $this->getOutput();
  105. $stream = $output->getStream();
  106. $shell->setOutput($output);
  107. $oldLevel = error_reporting();
  108. error_reporting($oldLevel & ~E_USER_NOTICE);
  109. try {
  110. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  111. } catch (ErrorException $e) {
  112. error_reporting($oldLevel);
  113. $this->fail('Unexpected error exception');
  114. }
  115. error_reporting($oldLevel);
  116. rewind($stream);
  117. $streamContents = stream_get_contents($stream);
  118. $this->assertContains('PHP error:', $streamContents);
  119. $this->assertContains('wheee', $streamContents);
  120. $this->assertContains('line 13', $streamContents);
  121. }
  122. /**
  123. * @expectedException Psy\Exception\ErrorException
  124. */
  125. public function testNotHandlingErrors()
  126. {
  127. $shell = new Shell($this->getConfig());
  128. $oldLevel = error_reporting();
  129. error_reporting($oldLevel | E_USER_NOTICE);
  130. try {
  131. $shell->handleError(E_USER_NOTICE, 'wheee', null, 13);
  132. } catch (ErrorException $e) {
  133. error_reporting($oldLevel);
  134. throw $e;
  135. }
  136. }
  137. public function testVersion()
  138. {
  139. $shell = new Shell($this->getConfig());
  140. $this->assertInstanceOf('Symfony\Component\Console\Application', $shell);
  141. $this->assertContains(Shell::VERSION, $shell->getVersion());
  142. $this->assertContains(phpversion(), $shell->getVersion());
  143. $this->assertContains(php_sapi_name(), $shell->getVersion());
  144. }
  145. public function testCodeBuffer()
  146. {
  147. $shell = new Shell($this->getConfig());
  148. $shell->addCode('class');
  149. $this->assertNull($shell->flushCode());
  150. $this->assertTrue($shell->hasCode());
  151. $shell->addCode('a');
  152. $this->assertNull($shell->flushCode());
  153. $this->assertTrue($shell->hasCode());
  154. $shell->addCode('{}');
  155. $code = $shell->flushCode();
  156. $this->assertFalse($shell->hasCode());
  157. $code = preg_replace('/\s+/', ' ', $code);
  158. $this->assertNotNull($code);
  159. $this->assertEquals('class a { }', $code);
  160. }
  161. public function testKeepCodeBufferOpen()
  162. {
  163. $shell = new Shell($this->getConfig());
  164. $shell->addCode('1 \\');
  165. $this->assertNull($shell->flushCode());
  166. $this->assertTrue($shell->hasCode());
  167. $shell->addCode('+ 1 \\');
  168. $this->assertNull($shell->flushCode());
  169. $this->assertTrue($shell->hasCode());
  170. $shell->addCode('+ 1');
  171. $code = $shell->flushCode();
  172. $this->assertFalse($shell->hasCode());
  173. $code = preg_replace('/\s+/', ' ', $code);
  174. $this->assertNotNull($code);
  175. $this->assertEquals('return 1 + 1 + 1;', $code);
  176. }
  177. /**
  178. * @expectedException \Psy\Exception\ParseErrorException
  179. */
  180. public function testCodeBufferThrowsParseExceptions()
  181. {
  182. $shell = new Shell($this->getConfig());
  183. $shell->addCode('this is not valid');
  184. $shell->flushCode();
  185. }
  186. public function testClosuresSupport()
  187. {
  188. $shell = new Shell($this->getConfig());
  189. $code = '$test = function () {}';
  190. $shell->addCode($code);
  191. $shell->flushCode();
  192. $code = '$test()';
  193. $shell->addCode($code);
  194. $shell->flushCode();
  195. }
  196. public function testWriteStdout()
  197. {
  198. $output = $this->getOutput();
  199. $stream = $output->getStream();
  200. $shell = new Shell($this->getConfig());
  201. $shell->setOutput($output);
  202. $shell->writeStdout("{{stdout}}\n");
  203. rewind($stream);
  204. $streamContents = stream_get_contents($stream);
  205. $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents);
  206. }
  207. public function testWriteStdoutWithoutNewline()
  208. {
  209. $output = $this->getOutput();
  210. $stream = $output->getStream();
  211. $shell = new Shell($this->getConfig());
  212. $shell->setOutput($output);
  213. $shell->writeStdout('{{stdout}}');
  214. rewind($stream);
  215. $streamContents = stream_get_contents($stream);
  216. $this->assertEquals('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
  217. }
  218. /**
  219. * @dataProvider getReturnValues
  220. */
  221. public function testWriteReturnValue($input, $expected)
  222. {
  223. $output = $this->getOutput();
  224. $stream = $output->getStream();
  225. $shell = new Shell($this->getConfig());
  226. $shell->setOutput($output);
  227. $shell->writeReturnValue($input);
  228. rewind($stream);
  229. $this->assertEquals($expected, stream_get_contents($stream));
  230. }
  231. public function getReturnValues()
  232. {
  233. return array(
  234. array('{{return value}}', '=> <string>"{{return value}}"</string>' . PHP_EOL),
  235. array(1, '=> <number>1</number>' . PHP_EOL),
  236. );
  237. }
  238. /**
  239. * @dataProvider getRenderedExceptions
  240. */
  241. public function testWriteException($exception, $expected)
  242. {
  243. $output = $this->getOutput();
  244. $stream = $output->getStream();
  245. $shell = new Shell($this->getConfig());
  246. $shell->setOutput($output);
  247. $shell->writeException($exception);
  248. rewind($stream);
  249. $this->assertEquals($expected, stream_get_contents($stream));
  250. }
  251. public function getRenderedExceptions()
  252. {
  253. return array(
  254. array(new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL),
  255. );
  256. }
  257. private function getOutput()
  258. {
  259. $stream = fopen('php://memory', 'w+');
  260. $this->streams[] = $stream;
  261. $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, false);
  262. return $output;
  263. }
  264. private function getConfig(array $config = array())
  265. {
  266. // Mebbe there's a better way than this?
  267. $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_');
  268. unlink($dir);
  269. $defaults = array(
  270. 'configDir' => $dir,
  271. 'dataDir' => $dir,
  272. 'runtimeDir' => $dir,
  273. );
  274. return new Configuration(array_merge($defaults, $config));
  275. }
  276. }