mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-12-02 17:38:19 +01:00
a2d7e8977a
Instead of storing subnodes in a subNodes dictionary, they are now stored as simple properties. This requires declarating the properties, assigning them in the constructor, overriding the getSubNodeNames() method and passing NULL to the first argument of the NodeAbstract constructor. [Deprecated: It's still possible to use the old mode of operation for custom nodes by passing an array of subnodes to the constructor.] The only behavior difference this should cause is that getSubNodeNames() will always return the original subnode names and skip any additional properties that were dynamically added. E.g. this means that the "namespacedName" node added by the NameResolver visitor is not treated as a subnode, but as a dynamic property instead. This change improves performance and memory usage.
162 lines
5.0 KiB
PHP
162 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace PhpParser;
|
|
|
|
class DummyNode extends NodeAbstract {
|
|
public $subNode1;
|
|
public $subNode2;
|
|
|
|
public function __construct($subNode1, $subNode2, $attributes) {
|
|
parent::__construct(null, $attributes);
|
|
$this->subNode1 = $subNode1;
|
|
$this->subNode2 = $subNode2;
|
|
}
|
|
|
|
public function getSubNodeNames() {
|
|
return array('subNode1', 'subNode2');
|
|
}
|
|
|
|
// This method is only overwritten because the node is located in an unusual namespace
|
|
public function getType() {
|
|
return 'Dummy';
|
|
}
|
|
}
|
|
|
|
class NodeAbstractTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
public function provideNodes() {
|
|
$attributes = array(
|
|
'startLine' => 10,
|
|
'comments' => array(
|
|
new Comment('// Comment' . "\n"),
|
|
new Comment\Doc('/** doc comment */'),
|
|
),
|
|
);
|
|
|
|
$node1 = $this->getMockForAbstractClass(
|
|
'PhpParser\NodeAbstract',
|
|
array(
|
|
array(
|
|
'subNode1' => 'value1',
|
|
'subNode2' => 'value2',
|
|
),
|
|
$attributes
|
|
),
|
|
'PhpParser_Node_Dummy'
|
|
);
|
|
$node1->notSubNode = 'value3';
|
|
|
|
$node2 = new DummyNode('value1', 'value2', $attributes);
|
|
$node2->notSubNode = 'value3';
|
|
|
|
return array(
|
|
array($attributes, $node1),
|
|
array($attributes, $node2),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideNodes
|
|
*/
|
|
public function testConstruct(array $attributes, Node $node) {
|
|
$this->assertSame('Dummy', $node->getType());
|
|
$this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames());
|
|
$this->assertSame(10, $node->getLine());
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
|
$this->assertSame('value1', $node->subNode1);
|
|
$this->assertSame('value2', $node->subNode2);
|
|
$this->assertTrue(isset($node->subNode1));
|
|
$this->assertTrue(isset($node->subNode2));
|
|
$this->assertFalse(isset($node->subNode3));
|
|
$this->assertSame($attributes, $node->getAttributes());
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideNodes
|
|
*/
|
|
public function testGetDocComment(array $attributes, Node $node) {
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
|
array_pop($node->getAttribute('comments')); // remove doc comment
|
|
$this->assertNull($node->getDocComment());
|
|
array_pop($node->getAttribute('comments')); // remove comment
|
|
$this->assertNull($node->getDocComment());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideNodes
|
|
*/
|
|
public function testChange(array $attributes, Node $node) {
|
|
// change of line
|
|
$node->setLine(15);
|
|
$this->assertSame(15, $node->getLine());
|
|
|
|
// direct modification
|
|
$node->subNode = 'newValue';
|
|
$this->assertSame('newValue', $node->subNode);
|
|
|
|
// indirect modification
|
|
$subNode =& $node->subNode;
|
|
$subNode = 'newNewValue';
|
|
$this->assertSame('newNewValue', $node->subNode);
|
|
|
|
// removal
|
|
unset($node->subNode);
|
|
$this->assertFalse(isset($node->subNode));
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideNodes
|
|
*/
|
|
public function testIteration(array $attributes, Node $node) {
|
|
// Iteration is simple object iteration over properties,
|
|
// not over subnodes
|
|
$i = 0;
|
|
foreach ($node as $key => $value) {
|
|
if ($i === 0) {
|
|
$this->assertSame('subNode1', $key);
|
|
$this->assertSame('value1', $value);
|
|
} else if ($i === 1) {
|
|
$this->assertSame('subNode2', $key);
|
|
$this->assertSame('value2', $value);
|
|
} else if ($i === 2) {
|
|
$this->assertSame('notSubNode', $key);
|
|
$this->assertSame('value3', $value);
|
|
} else {
|
|
throw new \Exception;
|
|
}
|
|
$i++;
|
|
}
|
|
$this->assertSame(3, $i);
|
|
}
|
|
|
|
public function testAttributes() {
|
|
/** @var $node Node */
|
|
$node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
|
|
|
|
$this->assertEmpty($node->getAttributes());
|
|
|
|
$node->setAttribute('key', 'value');
|
|
$this->assertTrue($node->hasAttribute('key'));
|
|
$this->assertSame('value', $node->getAttribute('key'));
|
|
|
|
$this->assertFalse($node->hasAttribute('doesNotExist'));
|
|
$this->assertNull($node->getAttribute('doesNotExist'));
|
|
$this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
|
|
|
|
$node->setAttribute('null', null);
|
|
$this->assertTrue($node->hasAttribute('null'));
|
|
$this->assertNull($node->getAttribute('null'));
|
|
$this->assertNull($node->getAttribute('null', 'default'));
|
|
|
|
$this->assertSame(
|
|
array(
|
|
'key' => 'value',
|
|
'null' => null,
|
|
),
|
|
$node->getAttributes()
|
|
);
|
|
}
|
|
}
|