mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 15:18:17 +01:00
Add tests for node attributes
Also fix the @inheritDoc declarations and do some whitespace normalization
This commit is contained in:
parent
510599d8b8
commit
a45360ccaf
@ -43,34 +43,34 @@ interface PHPParser_Node
|
||||
* @param null|string $docComment Nearest doc comment or null
|
||||
*/
|
||||
public function setDocComment($docComment);
|
||||
|
||||
|
||||
/**
|
||||
* Sets an attribute on a node.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setAttribute($key, $value);
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether an attribute exists.
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return Boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAttribute($key);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of an attribute.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAttribute($key, $default = null);
|
||||
|
||||
|
||||
/**
|
||||
* Returns all attributes for the given node.
|
||||
*
|
||||
|
@ -74,30 +74,30 @@ abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggrega
|
||||
public function setDocComment($docComment) {
|
||||
$this->docComment = $docComment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user