Mercurial > hg > isophonics-drupal-site
comparison vendor/psy/psysh/test/ContextTest.php @ 16:c2387f117808
Routine composer update
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 15:07:59 +0100 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
15:e200cb7efeb3 | 16:c2387f117808 |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of Psy Shell. | |
5 * | |
6 * (c) 2012-2018 Justin Hileman | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Psy\Test; | |
13 | |
14 use Psy\Context; | |
15 | |
16 class ContextTest extends \PHPUnit\Framework\TestCase | |
17 { | |
18 public function testGet() | |
19 { | |
20 $this->assertTrue(true); | |
21 } | |
22 | |
23 public function testGetAll() | |
24 { | |
25 $this->assertTrue(true); | |
26 } | |
27 | |
28 public function testGetSpecialVariables() | |
29 { | |
30 $context = new Context(); | |
31 | |
32 $this->assertNull($context->get('_')); | |
33 $this->assertNull($context->getReturnValue()); | |
34 | |
35 $this->assertEquals(['_' => null], $context->getAll()); | |
36 | |
37 $e = new \Exception('eeeeeee'); | |
38 $obj = new \StdClass(); | |
39 $context->setLastException($e); | |
40 $context->setLastStdout('out'); | |
41 $context->setBoundObject($obj); | |
42 | |
43 $context->setCommandScopeVariables([ | |
44 '__function' => 'function', | |
45 '__method' => 'method', | |
46 '__class' => 'class', | |
47 '__namespace' => 'namespace', | |
48 '__file' => 'file', | |
49 '__line' => 'line', | |
50 '__dir' => 'dir', | |
51 ]); | |
52 | |
53 $expected = [ | |
54 '_' => null, | |
55 '_e' => $e, | |
56 '__out' => 'out', | |
57 'this' => $obj, | |
58 '__function' => 'function', | |
59 '__method' => 'method', | |
60 '__class' => 'class', | |
61 '__namespace' => 'namespace', | |
62 '__file' => 'file', | |
63 '__line' => 'line', | |
64 '__dir' => 'dir', | |
65 ]; | |
66 | |
67 $this->assertEquals($expected, $context->getAll()); | |
68 } | |
69 | |
70 public function testSetAll() | |
71 { | |
72 $context = new Context(); | |
73 | |
74 $baz = new \StdClass(); | |
75 $vars = [ | |
76 'foo' => 'Foo', | |
77 'bar' => 123, | |
78 'baz' => $baz, | |
79 | |
80 '_' => 'fail', | |
81 '_e' => 'fail', | |
82 '__out' => 'fail', | |
83 'this' => 'fail', | |
84 '__psysh__' => 'fail', | |
85 | |
86 '__function' => 'fail', | |
87 '__method' => 'fail', | |
88 '__class' => 'fail', | |
89 '__namespace' => 'fail', | |
90 '__file' => 'fail', | |
91 '__line' => 'fail', | |
92 '__dir' => 'fail', | |
93 ]; | |
94 | |
95 $context->setAll($vars); | |
96 | |
97 $this->assertEquals('Foo', $context->get('foo')); | |
98 $this->assertEquals(123, $context->get('bar')); | |
99 $this->assertSame($baz, $context->get('baz')); | |
100 | |
101 $this->assertEquals(['foo' => 'Foo', 'bar' => 123, 'baz' => $baz, '_' => null], $context->getAll()); | |
102 } | |
103 | |
104 /** | |
105 * @dataProvider specialNames | |
106 * @expectedException \InvalidArgumentException | |
107 * @expectedExceptionMessageRegEx /Unknown variable: \$\w+/ | |
108 */ | |
109 public function testSetAllDoesNotSetSpecial($name) | |
110 { | |
111 $context = new Context(); | |
112 $context->setAll([$name => 'fail']); | |
113 $context->get($name); | |
114 } | |
115 | |
116 public function specialNames() | |
117 { | |
118 return [ | |
119 ['_e'], | |
120 ['__out'], | |
121 ['this'], | |
122 ['__psysh__'], | |
123 ['__function'], | |
124 ['__method'], | |
125 ['__class'], | |
126 ['__namespace'], | |
127 ['__file'], | |
128 ['__line'], | |
129 ['__dir'], | |
130 ]; | |
131 } | |
132 | |
133 public function testReturnValue() | |
134 { | |
135 $context = new Context(); | |
136 $this->assertNull($context->getReturnValue()); | |
137 | |
138 $val = 'some string'; | |
139 $context->setReturnValue($val); | |
140 $this->assertEquals($val, $context->getReturnValue()); | |
141 $this->assertEquals($val, $context->get('_')); | |
142 | |
143 $obj = new \StdClass(); | |
144 $context->setReturnValue($obj); | |
145 $this->assertSame($obj, $context->getReturnValue()); | |
146 $this->assertSame($obj, $context->get('_')); | |
147 | |
148 $context->setReturnValue(null); | |
149 $this->assertNull($context->getReturnValue()); | |
150 } | |
151 | |
152 public function testLastException() | |
153 { | |
154 $context = new Context(); | |
155 $e = new \Exception('wat'); | |
156 $context->setLastException($e); | |
157 $this->assertSame($e, $context->getLastException()); | |
158 $this->assertSame($e, $context->get('_e')); | |
159 } | |
160 | |
161 /** | |
162 * @expectedException \InvalidArgumentException | |
163 * @expectedExceptionMessage No most-recent exception | |
164 */ | |
165 public function testLastExceptionThrowsSometimes() | |
166 { | |
167 $context = new Context(); | |
168 $context->getLastException(); | |
169 } | |
170 | |
171 public function testLastStdout() | |
172 { | |
173 $context = new Context(); | |
174 $context->setLastStdout('ouuuuut'); | |
175 $this->assertEquals('ouuuuut', $context->getLastStdout()); | |
176 $this->assertEquals('ouuuuut', $context->get('__out')); | |
177 } | |
178 | |
179 /** | |
180 * @expectedException \InvalidArgumentException | |
181 * @expectedExceptionMessage No most-recent output | |
182 */ | |
183 public function testLastStdoutThrowsSometimes() | |
184 { | |
185 $context = new Context(); | |
186 $context->getLastStdout(); | |
187 } | |
188 | |
189 public function testBoundObject() | |
190 { | |
191 $context = new Context(); | |
192 $this->assertNull($context->getBoundObject()); | |
193 | |
194 $obj = new \StdClass(); | |
195 $context->setBoundObject($obj); | |
196 $this->assertSame($obj, $context->getBoundObject()); | |
197 $this->assertSame($obj, $context->get('this')); | |
198 | |
199 $context->setBoundObject(null); | |
200 $this->assertNull($context->getBoundObject()); | |
201 } | |
202 | |
203 /** | |
204 * @expectedException \InvalidArgumentException | |
205 * @expectedExceptionMessage Unknown variable: $this | |
206 */ | |
207 public function testBoundObjectThrowsSometimes() | |
208 { | |
209 $context = new Context(); | |
210 $context->get('this'); | |
211 } | |
212 | |
213 public function testBoundClass() | |
214 { | |
215 $context = new Context(); | |
216 $this->assertNull($context->getBoundClass()); | |
217 | |
218 $context->setBoundClass(''); | |
219 $this->assertNull($context->getBoundClass()); | |
220 | |
221 $context->setBoundClass('Psy\Shell'); | |
222 $this->assertEquals('Psy\Shell', $context->getBoundClass()); | |
223 | |
224 $context->setBoundObject(new \StdClass()); | |
225 $this->assertNotNull($context->getBoundObject()); | |
226 $this->assertNull($context->getBoundClass()); | |
227 | |
228 $context->setBoundClass('Psy\Shell'); | |
229 $this->assertEquals('Psy\Shell', $context->getBoundClass()); | |
230 $this->assertNull($context->getBoundObject()); | |
231 | |
232 $context->setBoundClass(null); | |
233 $this->assertNull($context->getBoundClass()); | |
234 $this->assertNull($context->getBoundObject()); | |
235 } | |
236 | |
237 public function testCommandScopeVariables() | |
238 { | |
239 $__function = 'donkey'; | |
240 $__method = 'diddy'; | |
241 $__class = 'cranky'; | |
242 $__namespace = 'funky'; | |
243 $__file = 'candy'; | |
244 $__line = 'dixie'; | |
245 $__dir = 'wrinkly'; | |
246 | |
247 $vars = compact('__function', '__method', '__class', '__namespace', '__file', '__line', '__dir'); | |
248 | |
249 $context = new Context(); | |
250 $context->setCommandScopeVariables($vars); | |
251 | |
252 $this->assertEquals($vars, $context->getCommandScopeVariables()); | |
253 | |
254 $this->assertEquals($__function, $context->get('__function')); | |
255 $this->assertEquals($__method, $context->get('__method')); | |
256 $this->assertEquals($__class, $context->get('__class')); | |
257 $this->assertEquals($__namespace, $context->get('__namespace')); | |
258 $this->assertEquals($__file, $context->get('__file')); | |
259 $this->assertEquals($__line, $context->get('__line')); | |
260 $this->assertEquals($__dir, $context->get('__dir')); | |
261 | |
262 $someVars = compact('__function', '__namespace', '__file', '__line', '__dir'); | |
263 $context->setCommandScopeVariables($someVars); | |
264 } | |
265 | |
266 public function testGetUnusedCommandScopeVariableNames() | |
267 { | |
268 $context = new Context(); | |
269 | |
270 $this->assertEquals( | |
271 ['__function', '__method', '__class', '__namespace', '__file', '__line', '__dir'], | |
272 $context->getUnusedCommandScopeVariableNames() | |
273 ); | |
274 | |
275 $context->setCommandScopeVariables([ | |
276 '__function' => 'foo', | |
277 '__namespace' => 'bar', | |
278 '__file' => 'baz', | |
279 '__line' => 123, | |
280 '__dir' => 'qux', | |
281 ]); | |
282 | |
283 $this->assertEquals( | |
284 ['__method', '__class'], | |
285 array_values($context->getUnusedCommandScopeVariableNames()) | |
286 ); | |
287 } | |
288 | |
289 /** | |
290 * @dataProvider specialAndNotSpecialVariableNames | |
291 */ | |
292 public function testIsSpecialVariableName($name, $isSpecial) | |
293 { | |
294 $context = new Context(); | |
295 | |
296 if ($isSpecial) { | |
297 $this->assertTrue($context->isSpecialVariableName($name)); | |
298 } else { | |
299 $this->assertFalse($context->isSpecialVariableName($name)); | |
300 } | |
301 } | |
302 | |
303 public function specialAndNotSpecialVariableNames() | |
304 { | |
305 return [ | |
306 ['foo', false], | |
307 ['psysh', false], | |
308 ['__psysh', false], | |
309 | |
310 ['_', true], | |
311 ['_e', true], | |
312 ['__out', true], | |
313 ['this', true], | |
314 ['__psysh__', true], | |
315 | |
316 ['__function', true], | |
317 ['__method', true], | |
318 ['__class', true], | |
319 ['__namespace', true], | |
320 ['__file', true], | |
321 ['__line', true], | |
322 ['__dir', true], | |
323 ]; | |
324 } | |
325 } |