5 class DummyNode extends NodeAbstract {
9 public function __construct($subNode1, $subNode2, $attributes) {
10 parent::__construct($attributes);
11 $this->subNode1 = $subNode1;
12 $this->subNode2 = $subNode2;
15 public function getSubNodeNames() {
16 return array('subNode1', 'subNode2');
19 // This method is only overwritten because the node is located in an unusual namespace
20 public function getType() {
25 class NodeAbstractTest extends \PHPUnit_Framework_TestCase
27 public function provideNodes() {
31 new Comment('// Comment' . "\n"),
32 new Comment\Doc('/** doc comment */'),
36 $node = new DummyNode('value1', 'value2', $attributes);
37 $node->notSubNode = 'value3';
40 array($attributes, $node),
45 * @dataProvider provideNodes
47 public function testConstruct(array $attributes, Node $node) {
48 $this->assertSame('Dummy', $node->getType());
49 $this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames());
50 $this->assertSame(10, $node->getLine());
51 $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
52 $this->assertSame('value1', $node->subNode1);
53 $this->assertSame('value2', $node->subNode2);
54 $this->assertTrue(isset($node->subNode1));
55 $this->assertTrue(isset($node->subNode2));
56 $this->assertFalse(isset($node->subNode3));
57 $this->assertSame($attributes, $node->getAttributes());
63 * @dataProvider provideNodes
65 public function testGetDocComment(array $attributes, Node $node) {
66 $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
67 array_pop($node->getAttribute('comments')); // remove doc comment
68 $this->assertNull($node->getDocComment());
69 array_pop($node->getAttribute('comments')); // remove comment
70 $this->assertNull($node->getDocComment());
73 public function testSetDocComment() {
74 $node = new DummyNode(null, null, []);
76 // Add doc comment to node without comments
77 $docComment = new Comment\Doc('/** doc */');
78 $node->setDocComment($docComment);
79 $this->assertSame($docComment, $node->getDocComment());
82 $docComment = new Comment\Doc('/** doc 2 */');
83 $node->setDocComment($docComment);
84 $this->assertSame($docComment, $node->getDocComment());
86 // Add docmment to node with other comments
87 $c1 = new Comment('/* foo */');
88 $c2 = new Comment('/* bar */');
89 $docComment = new Comment\Doc('/** baz */');
90 $node->setAttribute('comments', [$c1, $c2]);
91 $node->setDocComment($docComment);
92 $this->assertSame([$c1, $c2, $docComment], $node->getAttribute('comments'));
96 * @dataProvider provideNodes
98 public function testChange(array $attributes, Node $node) {
101 $this->assertSame(15, $node->getLine());
103 // direct modification
104 $node->subNode = 'newValue';
105 $this->assertSame('newValue', $node->subNode);
107 // indirect modification
108 $subNode =& $node->subNode;
109 $subNode = 'newNewValue';
110 $this->assertSame('newNewValue', $node->subNode);
113 unset($node->subNode);
114 $this->assertFalse(isset($node->subNode));
118 * @dataProvider provideNodes
120 public function testIteration(array $attributes, Node $node) {
121 // Iteration is simple object iteration over properties,
124 foreach ($node as $key => $value) {
126 $this->assertSame('subNode1', $key);
127 $this->assertSame('value1', $value);
128 } else if ($i === 1) {
129 $this->assertSame('subNode2', $key);
130 $this->assertSame('value2', $value);
131 } else if ($i === 2) {
132 $this->assertSame('notSubNode', $key);
133 $this->assertSame('value3', $value);
135 throw new \Exception;
139 $this->assertSame(3, $i);
142 public function testAttributes() {
143 /** @var $node Node */
144 $node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
146 $this->assertEmpty($node->getAttributes());
148 $node->setAttribute('key', 'value');
149 $this->assertTrue($node->hasAttribute('key'));
150 $this->assertSame('value', $node->getAttribute('key'));
152 $this->assertFalse($node->hasAttribute('doesNotExist'));
153 $this->assertNull($node->getAttribute('doesNotExist'));
154 $this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
156 $node->setAttribute('null', null);
157 $this->assertTrue($node->hasAttribute('null'));
158 $this->assertNull($node->getAttribute('null'));
159 $this->assertNull($node->getAttribute('null', 'default'));
166 $node->getAttributes()
170 public function testJsonSerialization() {
175 function functionName(&$a = 0, $b = 1.0) {
179 $expected = <<<'JSON'
182 "nodeType": "Stmt_Function",
184 "name": "functionName",
193 "nodeType": "Scalar_LNumber",
213 "nodeType": "Scalar_DNumber",
229 "nodeType": "Stmt_Echo",
232 "nodeType": "Scalar_String",
251 "nodeType": "Comment",
252 "text": "\/\/ comment\n",
257 "nodeType": "Comment_Doc",
258 "text": "\/** doc comment *\/",
269 $parser = new Parser\Php7(new Lexer());
270 $stmts = $parser->parse(canonicalize($code));
271 $json = json_encode($stmts, JSON_PRETTY_PRINT);
272 $this->assertEquals(canonicalize($expected), canonicalize($json));