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