Removed modules/contrib/media module to allow update to the core media module
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / NodeDumperTest.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser;
4
5 use PHPUnit\Framework\TestCase;
6
7 class NodeDumperTest extends TestCase
8 {
9     private function canonicalize($string) {
10         return str_replace("\r\n", "\n", $string);
11     }
12
13     /**
14      * @dataProvider provideTestDump
15      */
16     public function testDump($node, $dump) {
17         $dumper = new NodeDumper;
18
19         $this->assertSame($this->canonicalize($dump), $this->canonicalize($dumper->dump($node)));
20     }
21
22     public function provideTestDump() {
23         return [
24             [
25                 [],
26 'array(
27 )'
28             ],
29             [
30                 ['Foo', 'Bar', 'Key' => 'FooBar'],
31 'array(
32     0: Foo
33     1: Bar
34     Key: FooBar
35 )'
36             ],
37             [
38                 new Node\Name(['Hallo', 'World']),
39 'Name(
40     parts: array(
41         0: Hallo
42         1: World
43     )
44 )'
45             ],
46             [
47                 new Node\Expr\Array_([
48                     new Node\Expr\ArrayItem(new Node\Scalar\String_('Foo'))
49                 ]),
50 'Expr_Array(
51     items: array(
52         0: Expr_ArrayItem(
53             key: null
54             value: Scalar_String(
55                 value: Foo
56             )
57             byRef: false
58         )
59     )
60 )'
61             ],
62         ];
63     }
64
65     public function testDumpWithPositions() {
66         $parser = (new ParserFactory)->create(
67             ParserFactory::ONLY_PHP7,
68             new Lexer(['usedAttributes' => ['startLine', 'endLine', 'startFilePos', 'endFilePos']])
69         );
70         $dumper = new NodeDumper(['dumpPositions' => true]);
71
72         $code = "<?php\n\$a = 1;\necho \$a;";
73         $expected = <<<'OUT'
74 array(
75     0: Stmt_Expression[2:1 - 2:7](
76         expr: Expr_Assign[2:1 - 2:6](
77             var: Expr_Variable[2:1 - 2:2](
78                 name: a
79             )
80             expr: Scalar_LNumber[2:6 - 2:6](
81                 value: 1
82             )
83         )
84     )
85     1: Stmt_Echo[3:1 - 3:8](
86         exprs: array(
87             0: Expr_Variable[3:6 - 3:7](
88                 name: a
89             )
90         )
91     )
92 )
93 OUT;
94
95         $stmts = $parser->parse($code);
96         $dump = $dumper->dump($stmts, $code);
97
98         $this->assertSame($this->canonicalize($expected), $this->canonicalize($dump));
99     }
100
101     /**
102      * @expectedException        \InvalidArgumentException
103      * @expectedExceptionMessage Can only dump nodes and arrays.
104      */
105     public function testError() {
106         $dumper = new NodeDumper;
107         $dumper->dump(new \stdClass);
108     }
109 }