2011-11-27 21:43:27 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
class NodeAbstractTest extends \PHPUnit_Framework_TestCase
|
2011-11-27 21:43:27 +01:00
|
|
|
{
|
|
|
|
public function testConstruct() {
|
2012-04-29 23:32:09 +02:00
|
|
|
$attributes = array(
|
2012-05-06 18:24:26 +02:00
|
|
|
'startLine' => 10,
|
|
|
|
'comments' => array(
|
2014-02-06 14:44:16 +01:00
|
|
|
new Comment('// Comment' . "\n"),
|
|
|
|
new Comment\Doc('/** doc comment */'),
|
2012-05-06 18:24:26 +02:00
|
|
|
),
|
2012-04-29 23:32:09 +02:00
|
|
|
);
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
/** @var $node NodeAbstract */
|
2011-11-27 21:43:27 +01:00
|
|
|
$node = $this->getMockForAbstractClass(
|
2014-02-06 14:44:16 +01:00
|
|
|
'PhpParser\NodeAbstract',
|
2011-11-27 21:43:27 +01:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'subNode' => 'value'
|
|
|
|
),
|
2012-04-29 23:32:09 +02:00
|
|
|
$attributes
|
2011-11-27 21:43:27 +01:00
|
|
|
),
|
2014-02-06 14:44:16 +01:00
|
|
|
'PhpParser_Node_Dummy'
|
2011-11-27 21:43:27 +01:00
|
|
|
);
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Dummy', $node->getType());
|
|
|
|
$this->assertSame(array('subNode'), $node->getSubNodeNames());
|
|
|
|
$this->assertSame(10, $node->getLine());
|
|
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
|
|
|
$this->assertSame('value', $node->subNode);
|
2011-11-27 21:43:27 +01:00
|
|
|
$this->assertTrue(isset($node->subNode));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($attributes, $node->getAttributes());
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2012-05-06 18:24:26 +02:00
|
|
|
/**
|
|
|
|
* @depends testConstruct
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
public function testGetDocComment(Node $node) {
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
2012-05-06 18:24:26 +02:00
|
|
|
array_pop($node->getAttribute('comments')); // remove doc comment
|
|
|
|
$this->assertNull($node->getDocComment());
|
|
|
|
array_pop($node->getAttribute('comments')); // remove comment
|
|
|
|
$this->assertNull($node->getDocComment());
|
|
|
|
}
|
|
|
|
|
2011-11-27 21:43:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testConstruct
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
public function testChange(Node $node) {
|
2011-12-02 17:52:03 +01:00
|
|
|
// change of line
|
2011-11-27 21:43:27 +01:00
|
|
|
$node->setLine(15);
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(15, $node->getLine());
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2011-12-02 17:52:03 +01:00
|
|
|
// direct modification
|
2011-11-27 21:43:27 +01:00
|
|
|
$node->subNode = 'newValue';
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('newValue', $node->subNode);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2011-12-02 17:52:03 +01:00
|
|
|
// indirect modification
|
|
|
|
$subNode =& $node->subNode;
|
|
|
|
$subNode = 'newNewValue';
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('newNewValue', $node->subNode);
|
2011-12-02 17:52:03 +01:00
|
|
|
|
|
|
|
// removal
|
2011-11-27 21:43:27 +01:00
|
|
|
unset($node->subNode);
|
|
|
|
$this->assertFalse(isset($node->subNode));
|
|
|
|
}
|
2012-04-09 12:37:47 +02:00
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
public function testAttributes() {
|
2014-02-06 14:44:16 +01:00
|
|
|
/** @var $node Node */
|
|
|
|
$node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
|
2012-04-29 23:32:09 +02:00
|
|
|
|
2012-04-09 12:37:47 +02:00
|
|
|
$this->assertEmpty($node->getAttributes());
|
|
|
|
|
|
|
|
$node->setAttribute('key', 'value');
|
|
|
|
$this->assertTrue($node->hasAttribute('key'));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('value', $node->getAttribute('key'));
|
2012-04-09 12:37:47 +02:00
|
|
|
|
|
|
|
$this->assertFalse($node->hasAttribute('doesNotExist'));
|
|
|
|
$this->assertNull($node->getAttribute('doesNotExist'));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
|
2012-04-09 12:37:47 +02:00
|
|
|
|
|
|
|
$node->setAttribute('null', null);
|
|
|
|
$this->assertTrue($node->hasAttribute('null'));
|
|
|
|
$this->assertNull($node->getAttribute('null'));
|
|
|
|
$this->assertNull($node->getAttribute('null', 'default'));
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(
|
2012-04-09 12:37:47 +02:00
|
|
|
array(
|
2012-04-29 23:32:09 +02:00
|
|
|
'key' => 'value',
|
2012-04-09 12:37:47 +02:00
|
|
|
'null' => null,
|
|
|
|
),
|
|
|
|
$node->getAttributes()
|
|
|
|
);
|
|
|
|
}
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|