1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 04:14:44 +01:00
PHP-Parser/test/PhpParser/NodeAbstractTest.php

148 lines
4.6 KiB
PHP
Raw Normal View History

2011-11-27 21:43:27 +01:00
<?php
namespace PhpParser;
class DummyNode extends NodeAbstract {
public $subNode1;
public $subNode2;
public function __construct($subNode1, $subNode2, $attributes) {
2015-05-02 22:17:34 +02:00
parent::__construct($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
2011-11-27 21:43:27 +01:00
{
public function provideNodes() {
$attributes = array(
'startLine' => 10,
'comments' => array(
new Comment('// Comment' . "\n"),
new Comment\Doc('/** doc comment */'),
),
);
2015-05-02 22:17:34 +02:00
$node = new DummyNode('value1', 'value2', $attributes);
$node->notSubNode = 'value3';
return array(
2015-05-02 22:17:34 +02:00
array($attributes, $node),
);
}
2011-11-27 21:43:27 +01:00
/**
* @dataProvider provideNodes
*/
public function testConstruct(array $attributes, Node $node) {
2014-09-30 20:38:09 +02:00
$this->assertSame('Dummy', $node->getType());
$this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames());
2014-09-30 20:38:09 +02:00
$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));
2014-09-30 20:38:09 +02:00
$this->assertSame($attributes, $node->getAttributes());
2011-11-27 21:43:27 +01:00
return $node;
}
/**
* @dataProvider provideNodes
*/
public function testGetDocComment(array $attributes, Node $node) {
2014-09-30 20:38:09 +02:00
$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());
}
2011-11-27 21:43:27 +01:00
/**
* @dataProvider provideNodes
2011-11-27 21:43:27 +01:00
*/
public function testChange(array $attributes, Node $node) {
// 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
// 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
// indirect modification
$subNode =& $node->subNode;
$subNode = 'newNewValue';
2014-09-30 20:38:09 +02:00
$this->assertSame('newNewValue', $node->subNode);
// removal
2011-11-27 21:43:27 +01:00
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'));
2014-09-30 20:38:09 +02:00
$this->assertSame('value', $node->getAttribute('key'));
$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'));
$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(
array(
'key' => 'value',
'null' => null,
),
$node->getAttributes()
);
}
}