Add tests for node attributes

Also fix the @inheritDoc declarations and do some whitespace normalization
This commit is contained in:
nikic 2012-04-09 12:37:47 +02:00
parent 510599d8b8
commit a45360ccaf
3 changed files with 45 additions and 16 deletions

View File

@ -48,7 +48,7 @@ interface PHPParser_Node
* Sets an attribute on a node.
*
* @param string $key
* @param mixed $value
* @param mixed $value
*/
public function setAttribute($key, $value);
@ -57,7 +57,7 @@ interface PHPParser_Node
*
* @param string $key
*
* @return Boolean
* @return bool
*/
public function hasAttribute($key);
@ -65,7 +65,7 @@ interface PHPParser_Node
* Returns the value of an attribute.
*
* @param string $key
* @param mixed $default
* @param mixed $default
*
* @return mixed
*/

View File

@ -76,28 +76,28 @@ abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggrega
}
/**
* @inheritDoc
* {@inheritDoc}
*/
public function setAttribute($key, $value) {
$this->attributes[$key] = $value;
}
/**
* @inheritDoc
* {@inheritDoc}
*/
public function hasAttribute($key) {
return array_key_exists($key, $this->attributes);
}
/**
* @inheritDoc
* {@inheritDoc}
*/
public function getAttribute($key, $default = null) {
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : $default;
}
/**
* @inheritDoc
* {@inheritDoc}
*/
public function getAttributes() {
return $this->attributes;

View File

@ -21,6 +21,7 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
$this->assertEquals('/** doc comment */', $node->getDocComment());
$this->assertEquals('value', $node->subNode);
$this->assertTrue(isset($node->subNode));
$this->assertEmpty($node->getAttributes());
return $node;
}
@ -50,4 +51,32 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
unset($node->subNode);
$this->assertFalse(isset($node->subNode));
}
/**
* @depends testConstruct
*/
public function testAttributes(PHPParser_NodeAbstract $node) {
$this->assertEmpty($node->getAttributes());
$node->setAttribute('key', 'value');
$this->assertTrue($node->hasAttribute('key'));
$this->assertEquals('value', $node->getAttribute('key'));
$this->assertFalse($node->hasAttribute('doesNotExist'));
$this->assertNull($node->getAttribute('doesNotExist'));
$this->assertEquals('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->assertEquals(
array(
'key' => 'value',
'null' => null,
),
$node->getAttributes()
);
}
}