comparison vendor/nikic/php-parser/test/PhpParser/NodeDumperTest.php @ 0:4c8ae668cc8c

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