Version 1
[yaffs-website] / vendor / symfony / var-dumper / Tests / CliDumperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\VarDumper\Tests;
13
14 use Symfony\Component\VarDumper\Cloner\VarCloner;
15 use Symfony\Component\VarDumper\Dumper\CliDumper;
16 use Symfony\Component\VarDumper\Test\VarDumperTestCase;
17
18 /**
19  * @author Nicolas Grekas <p@tchwork.com>
20  */
21 class CliDumperTest extends VarDumperTestCase
22 {
23     public function testGet()
24     {
25         require __DIR__.'/Fixtures/dumb-var.php';
26
27         $dumper = new CliDumper('php://output');
28         $dumper->setColors(false);
29         $cloner = new VarCloner();
30         $cloner->addCasters(array(
31             ':stream' => function ($res, $a) {
32                 unset($a['uri'], $a['wrapper_data']);
33
34                 return $a;
35             },
36         ));
37         $data = $cloner->cloneVar($var);
38
39         ob_start();
40         $dumper->dump($data);
41         $out = ob_get_clean();
42         $out = preg_replace('/[ \t]+$/m', '', $out);
43         $intMax = PHP_INT_MAX;
44         $res = (int) $var['res'];
45         $closure54 = '';
46         $r = defined('HHVM_VERSION') ? '' : '#%d';
47
48         if (PHP_VERSION_ID >= 50400) {
49             $closure54 = <<<EOTXT
50
51     class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
52     this: Symfony\Component\VarDumper\Tests\CliDumperTest {{$r} …}
53 EOTXT;
54         }
55
56         $this->assertStringMatchesFormat(
57             <<<EOTXT
58 array:24 [
59   "number" => 1
60   0 => &1 null
61   "const" => 1.1
62   1 => true
63   2 => false
64   3 => NAN
65   4 => INF
66   5 => -INF
67   6 => {$intMax}
68   "str" => "déjà\\n"
69   7 => b"é\\x00"
70   "[]" => []
71   "res" => stream resource {@{$res}
72 %A  wrapper_type: "plainfile"
73     stream_type: "STDIO"
74     mode: "r"
75     unread_bytes: 0
76     seekable: true
77 %A  options: []
78   }
79   "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
80     +foo: "foo"
81     +"bar": "bar"
82   }
83   "closure" => Closure {{$r}{$closure54}
84     parameters: {
85       \$a: {}
86       &\$b: {
87         typeHint: "PDO"
88         default: null
89       }
90     }
91     file: "{$var['file']}"
92     line: "{$var['line']} to {$var['line']}"
93   }
94   "line" => {$var['line']}
95   "nobj" => array:1 [
96     0 => &3 {#%d}
97   ]
98   "recurs" => &4 array:1 [
99     0 => &4 array:1 [&4]
100   ]
101   8 => &1 null
102   "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
103   "snobj" => &3 {#%d}
104   "snobj2" => {#%d}
105   "file" => "{$var['file']}"
106   b"bin-key-é" => ""
107 ]
108
109 EOTXT
110             ,
111             $out
112         );
113     }
114
115     /**
116      * @requires extension xml
117      */
118     public function testXmlResource()
119     {
120         $var = xml_parser_create();
121
122         $this->assertDumpMatchesFormat(
123             <<<'EOTXT'
124 xml resource {
125   current_byte_index: %i
126   current_column_number: %i
127   current_line_number: 1
128   error_code: XML_ERROR_NONE
129 }
130 EOTXT
131             ,
132             $var
133         );
134     }
135
136     public function testJsonCast()
137     {
138         $var = (array) json_decode('{"0":{},"1":null}');
139         foreach ($var as &$v) {
140         }
141         $var[] = &$v;
142         $var[''] = 2;
143
144         $this->assertDumpMatchesFormat(
145             <<<'EOTXT'
146 array:4 [
147   "0" => {}
148   "1" => &1 null
149   0 => &1 null
150   "" => 2
151 ]
152 EOTXT
153             ,
154             $var
155         );
156     }
157
158     public function testObjectCast()
159     {
160         $var = (object) array(1 => 1);
161         $var->{1} = 2;
162
163         $this->assertDumpMatchesFormat(
164             <<<'EOTXT'
165 {
166   +1: 1
167   +"1": 2
168 }
169 EOTXT
170             ,
171             $var
172         );
173     }
174
175     public function testClosedResource()
176     {
177         if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
178             $this->markTestSkipped();
179         }
180
181         $var = fopen(__FILE__, 'r');
182         fclose($var);
183
184         $dumper = new CliDumper('php://output');
185         $dumper->setColors(false);
186         $cloner = new VarCloner();
187         $data = $cloner->cloneVar($var);
188
189         ob_start();
190         $dumper->dump($data);
191         $out = ob_get_clean();
192         $res = (int) $var;
193
194         $this->assertStringMatchesFormat(
195             <<<EOTXT
196 Closed resource @{$res}
197
198 EOTXT
199             ,
200             $out
201         );
202     }
203
204     /**
205      * @requires function Twig_Template::getSourceContext
206      */
207     public function testThrowingCaster()
208     {
209         $out = fopen('php://memory', 'r+b');
210
211         require_once __DIR__.'/Fixtures/Twig.php';
212         $twig = new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()));
213
214         $dumper = new CliDumper();
215         $dumper->setColors(false);
216         $cloner = new VarCloner();
217         $cloner->addCasters(array(
218             ':stream' => function ($res, $a) {
219                 unset($a['wrapper_data']);
220
221                 return $a;
222             },
223         ));
224         $cloner->addCasters(array(
225             ':stream' => eval('return function () use ($twig) {
226                 try {
227                     $twig->render(array());
228                 } catch (\Twig_Error_Runtime $e) {
229                     throw $e->getPrevious();
230                 }
231             };'),
232         ));
233         $line = __LINE__ - 2;
234         $ref = (int) $out;
235
236         $data = $cloner->cloneVar($out);
237         $dumper->dump($data, $out);
238         rewind($out);
239         $out = stream_get_contents($out);
240
241         $r = defined('HHVM_VERSION') ? '' : '#%d';
242         $this->assertStringMatchesFormat(
243             <<<EOTXT
244 stream resource {@{$ref}
245 %Awrapper_type: "PHP"
246   stream_type: "MEMORY"
247   mode: "%s+b"
248   unread_bytes: 0
249   seekable: true
250   uri: "php://memory"
251 %Aoptions: []
252   ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
253     #message: "Unexpected Exception thrown from a caster: Foobar"
254     -trace: {
255       %d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
256         src: {
257           %sTwig.php:21: """
258                 // line 2\\n
259                 throw new \Exception('Foobar');\\n
260             }\\n
261             """
262           bar.twig:2: """
263             foo bar\\n
264               twig source\\n
265             \\n
266             """
267         }
268       }
269       %d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
270         src: {
271           %sTemplate.php:%d: """
272             try {\\n
273                 \$this->doDisplay(\$context, \$blocks);\\n
274             } catch (Twig_Error \$e) {\\n
275             """
276         }
277       }
278       %d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
279         src: {
280           %sTemplate.php:%d: """
281             {\\n
282                 \$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
283             }\\n
284             """
285         }
286       }
287       %d. Twig_Template->render() ==> Twig_Template->display(): {
288         src: {
289           %sTemplate.php:%d: """
290             try {\\n
291                 \$this->display(\$context);\\n
292             } catch (%s \$e) {\\n
293             """
294         }
295       }
296       %d. %slosure%s() ==> Twig_Template->render(): {
297         src: {
298           %sCliDumperTest.php:{$line}: """
299                     }\\n
300                 };'),\\n
301             ));\\n
302             """
303         }
304       }
305     }
306   }
307 }
308
309 EOTXT
310             ,
311             $out
312         );
313     }
314
315     public function testRefsInProperties()
316     {
317         $var = (object) array('foo' => 'foo');
318         $var->bar = &$var->foo;
319
320         $dumper = new CliDumper();
321         $dumper->setColors(false);
322         $cloner = new VarCloner();
323
324         $out = fopen('php://memory', 'r+b');
325         $data = $cloner->cloneVar($var);
326         $dumper->dump($data, $out);
327         rewind($out);
328         $out = stream_get_contents($out);
329
330         $r = defined('HHVM_VERSION') ? '' : '#%d';
331         $this->assertStringMatchesFormat(
332             <<<EOTXT
333 {{$r}
334   +"foo": &1 "foo"
335   +"bar": &1 "foo"
336 }
337
338 EOTXT
339             ,
340             $out
341         );
342     }
343
344     /**
345      * @runInSeparateProcess
346      * @preserveGlobalState disabled
347      * @requires PHP 5.6
348      */
349     public function testSpecialVars56()
350     {
351         $var = $this->getSpecialVars();
352
353         $this->assertDumpEquals(
354             <<<'EOTXT'
355 array:3 [
356   0 => array:1 [
357     0 => &1 array:1 [
358       0 => &1 array:1 [&1]
359     ]
360   ]
361   1 => array:1 [
362     "GLOBALS" => &2 array:1 [
363       "GLOBALS" => &2 array:1 [&2]
364     ]
365   ]
366   2 => &2 array:1 [&2]
367 ]
368 EOTXT
369             ,
370             $var
371         );
372     }
373
374     /**
375      * @runInSeparateProcess
376      * @preserveGlobalState disabled
377      */
378     public function testGlobalsNoExt()
379     {
380         $var = $this->getSpecialVars();
381         unset($var[0]);
382         $out = '';
383
384         $dumper = new CliDumper(function ($line, $depth) use (&$out) {
385             if ($depth >= 0) {
386                 $out .= str_repeat('  ', $depth).$line."\n";
387             }
388         });
389         $dumper->setColors(false);
390         $cloner = new VarCloner();
391
392         $refl = new \ReflectionProperty($cloner, 'useExt');
393         $refl->setAccessible(true);
394         $refl->setValue($cloner, false);
395
396         $data = $cloner->cloneVar($var);
397         $dumper->dump($data);
398
399         $this->assertSame(
400             <<<'EOTXT'
401 array:2 [
402   1 => array:1 [
403     "GLOBALS" => &1 array:1 [
404       "GLOBALS" => &1 array:1 [&1]
405     ]
406   ]
407   2 => &1 array:1 [&1]
408 ]
409
410 EOTXT
411             ,
412             $out
413         );
414     }
415
416     /**
417      * @runInSeparateProcess
418      * @preserveGlobalState disabled
419      */
420     public function testBuggyRefs()
421     {
422         if (PHP_VERSION_ID >= 50600) {
423             $this->markTestSkipped('PHP 5.6 fixed refs counting');
424         }
425
426         $var = $this->getSpecialVars();
427         $var = $var[0];
428
429         $dumper = new CliDumper();
430         $dumper->setColors(false);
431         $cloner = new VarCloner();
432
433         $data = $cloner->cloneVar($var)->withMaxDepth(3);
434         $out = '';
435         $dumper->dump($data, function ($line, $depth) use (&$out) {
436             if ($depth >= 0) {
437                 $out .= str_repeat('  ', $depth).$line."\n";
438             }
439         });
440
441         $this->assertSame(
442             <<<'EOTXT'
443 array:1 [
444   0 => array:1 [
445     0 => array:1 [
446       0 => array:1 [ …1]
447     ]
448   ]
449 ]
450
451 EOTXT
452             ,
453             $out
454         );
455     }
456
457     private function getSpecialVars()
458     {
459         foreach (array_keys($GLOBALS) as $var) {
460             if ('GLOBALS' !== $var) {
461                 unset($GLOBALS[$var]);
462             }
463         }
464
465         $var = function &() {
466             $var = array();
467             $var[] = &$var;
468
469             return $var;
470         };
471
472         return array($var(), $GLOBALS, &$GLOBALS);
473     }
474 }