PageRenderTime 30ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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