Nessuna descrizione

ContextTest.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\Context;
  12. class ContextTest extends \PHPUnit\Framework\TestCase
  13. {
  14. public function testGet()
  15. {
  16. $this->assertTrue(true);
  17. }
  18. public function testGetAll()
  19. {
  20. $this->assertTrue(true);
  21. }
  22. public function testGetSpecialVariables()
  23. {
  24. $context = new Context();
  25. $this->assertNull($context->get('_'));
  26. $this->assertNull($context->getReturnValue());
  27. $this->assertEquals(['_' => null], $context->getAll());
  28. $e = new \Exception('eeeeeee');
  29. $obj = new \StdClass();
  30. $context->setLastException($e);
  31. $context->setLastStdout('out');
  32. $context->setBoundObject($obj);
  33. $context->setCommandScopeVariables([
  34. '__function' => 'function',
  35. '__method' => 'method',
  36. '__class' => 'class',
  37. '__namespace' => 'namespace',
  38. '__file' => 'file',
  39. '__line' => 'line',
  40. '__dir' => 'dir',
  41. ]);
  42. $expected = [
  43. '_' => null,
  44. '_e' => $e,
  45. '__out' => 'out',
  46. 'this' => $obj,
  47. '__function' => 'function',
  48. '__method' => 'method',
  49. '__class' => 'class',
  50. '__namespace' => 'namespace',
  51. '__file' => 'file',
  52. '__line' => 'line',
  53. '__dir' => 'dir',
  54. ];
  55. $this->assertEquals($expected, $context->getAll());
  56. }
  57. public function testSetAll()
  58. {
  59. $context = new Context();
  60. $baz = new \StdClass();
  61. $vars = [
  62. 'foo' => 'Foo',
  63. 'bar' => 123,
  64. 'baz' => $baz,
  65. '_' => 'fail',
  66. '_e' => 'fail',
  67. '__out' => 'fail',
  68. 'this' => 'fail',
  69. '__psysh__' => 'fail',
  70. '__function' => 'fail',
  71. '__method' => 'fail',
  72. '__class' => 'fail',
  73. '__namespace' => 'fail',
  74. '__file' => 'fail',
  75. '__line' => 'fail',
  76. '__dir' => 'fail',
  77. ];
  78. $context->setAll($vars);
  79. $this->assertEquals('Foo', $context->get('foo'));
  80. $this->assertEquals(123, $context->get('bar'));
  81. $this->assertSame($baz, $context->get('baz'));
  82. $this->assertEquals(['foo' => 'Foo', 'bar' => 123, 'baz' => $baz, '_' => null], $context->getAll());
  83. }
  84. /**
  85. * @dataProvider specialNames
  86. * @expectedException \InvalidArgumentException
  87. * @expectedExceptionMessageRegEx /Unknown variable: \$\w+/
  88. */
  89. public function testSetAllDoesNotSetSpecial($name)
  90. {
  91. $context = new Context();
  92. $context->setAll([$name => 'fail']);
  93. $context->get($name);
  94. }
  95. public function specialNames()
  96. {
  97. return [
  98. ['_e'],
  99. ['__out'],
  100. ['this'],
  101. ['__psysh__'],
  102. ['__function'],
  103. ['__method'],
  104. ['__class'],
  105. ['__namespace'],
  106. ['__file'],
  107. ['__line'],
  108. ['__dir'],
  109. ];
  110. }
  111. public function testReturnValue()
  112. {
  113. $context = new Context();
  114. $this->assertNull($context->getReturnValue());
  115. $val = 'some string';
  116. $context->setReturnValue($val);
  117. $this->assertEquals($val, $context->getReturnValue());
  118. $this->assertEquals($val, $context->get('_'));
  119. $obj = new \StdClass();
  120. $context->setReturnValue($obj);
  121. $this->assertSame($obj, $context->getReturnValue());
  122. $this->assertSame($obj, $context->get('_'));
  123. $context->setReturnValue(null);
  124. $this->assertNull($context->getReturnValue());
  125. }
  126. public function testLastException()
  127. {
  128. $context = new Context();
  129. $e = new \Exception('wat');
  130. $context->setLastException($e);
  131. $this->assertSame($e, $context->getLastException());
  132. $this->assertSame($e, $context->get('_e'));
  133. }
  134. /**
  135. * @expectedException \InvalidArgumentException
  136. * @expectedExceptionMessage No most-recent exception
  137. */
  138. public function testLastExceptionThrowsSometimes()
  139. {
  140. $context = new Context();
  141. $context->getLastException();
  142. }
  143. public function testLastStdout()
  144. {
  145. $context = new Context();
  146. $context->setLastStdout('ouuuuut');
  147. $this->assertEquals('ouuuuut', $context->getLastStdout());
  148. $this->assertEquals('ouuuuut', $context->get('__out'));
  149. }
  150. /**
  151. * @expectedException \InvalidArgumentException
  152. * @expectedExceptionMessage No most-recent output
  153. */
  154. public function testLastStdoutThrowsSometimes()
  155. {
  156. $context = new Context();
  157. $context->getLastStdout();
  158. }
  159. public function testBoundObject()
  160. {
  161. $context = new Context();
  162. $this->assertNull($context->getBoundObject());
  163. $obj = new \StdClass();
  164. $context->setBoundObject($obj);
  165. $this->assertSame($obj, $context->getBoundObject());
  166. $this->assertSame($obj, $context->get('this'));
  167. $context->setBoundObject(null);
  168. $this->assertNull($context->getBoundObject());
  169. }
  170. /**
  171. * @expectedException \InvalidArgumentException
  172. * @expectedExceptionMessage Unknown variable: $this
  173. */
  174. public function testBoundObjectThrowsSometimes()
  175. {
  176. $context = new Context();
  177. $context->get('this');
  178. }
  179. public function testBoundClass()
  180. {
  181. $context = new Context();
  182. $this->assertNull($context->getBoundClass());
  183. $context->setBoundClass('');
  184. $this->assertNull($context->getBoundClass());
  185. $context->setBoundClass('Psy\Shell');
  186. $this->assertEquals('Psy\Shell', $context->getBoundClass());
  187. $context->setBoundObject(new \StdClass());
  188. $this->assertNotNull($context->getBoundObject());
  189. $this->assertNull($context->getBoundClass());
  190. $context->setBoundClass('Psy\Shell');
  191. $this->assertEquals('Psy\Shell', $context->getBoundClass());
  192. $this->assertNull($context->getBoundObject());
  193. $context->setBoundClass(null);
  194. $this->assertNull($context->getBoundClass());
  195. $this->assertNull($context->getBoundObject());
  196. }
  197. public function testCommandScopeVariables()
  198. {
  199. $__function = 'donkey';
  200. $__method = 'diddy';
  201. $__class = 'cranky';
  202. $__namespace = 'funky';
  203. $__file = 'candy';
  204. $__line = 'dixie';
  205. $__dir = 'wrinkly';
  206. $vars = \compact('__function', '__method', '__class', '__namespace', '__file', '__line', '__dir');
  207. $context = new Context();
  208. $context->setCommandScopeVariables($vars);
  209. $this->assertEquals($vars, $context->getCommandScopeVariables());
  210. $this->assertEquals($__function, $context->get('__function'));
  211. $this->assertEquals($__method, $context->get('__method'));
  212. $this->assertEquals($__class, $context->get('__class'));
  213. $this->assertEquals($__namespace, $context->get('__namespace'));
  214. $this->assertEquals($__file, $context->get('__file'));
  215. $this->assertEquals($__line, $context->get('__line'));
  216. $this->assertEquals($__dir, $context->get('__dir'));
  217. $someVars = \compact('__function', '__namespace', '__file', '__line', '__dir');
  218. $context->setCommandScopeVariables($someVars);
  219. }
  220. public function testGetUnusedCommandScopeVariableNames()
  221. {
  222. $context = new Context();
  223. $this->assertEquals(
  224. ['__function', '__method', '__class', '__namespace', '__file', '__line', '__dir'],
  225. $context->getUnusedCommandScopeVariableNames()
  226. );
  227. $context->setCommandScopeVariables([
  228. '__function' => 'foo',
  229. '__namespace' => 'bar',
  230. '__file' => 'baz',
  231. '__line' => 123,
  232. '__dir' => 'qux',
  233. ]);
  234. $this->assertEquals(
  235. ['__method', '__class'],
  236. \array_values($context->getUnusedCommandScopeVariableNames())
  237. );
  238. }
  239. /**
  240. * @dataProvider specialAndNotSpecialVariableNames
  241. */
  242. public function testIsSpecialVariableName($name, $isSpecial)
  243. {
  244. $context = new Context();
  245. if ($isSpecial) {
  246. $this->assertTrue($context->isSpecialVariableName($name));
  247. } else {
  248. $this->assertFalse($context->isSpecialVariableName($name));
  249. }
  250. }
  251. public function specialAndNotSpecialVariableNames()
  252. {
  253. return [
  254. ['foo', false],
  255. ['psysh', false],
  256. ['__psysh', false],
  257. ['_', true],
  258. ['_e', true],
  259. ['__out', true],
  260. ['this', true],
  261. ['__psysh__', true],
  262. ['__function', true],
  263. ['__method', true],
  264. ['__class', true],
  265. ['__namespace', true],
  266. ['__file', true],
  267. ['__line', true],
  268. ['__dir', true],
  269. ];
  270. }
  271. }