From 2c65a28608b54acca7a1d311c804aa8ceb8efc29 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 19 Feb 2014 13:50:19 +0100 Subject: [PATCH] [Validator] Completed test coverage and documentation of the Node classes --- .../Component/Validator/Node/ClassNode.php | 30 +++++++++++--- .../Component/Validator/Node/GenericNode.php | 9 +++- src/Symfony/Component/Validator/Node/Node.php | 41 ++++++++++++++++++- .../Component/Validator/Node/PropertyNode.php | 28 ++++++++++++- .../Validator/Tests/Node/ClassNodeTest.php | 31 ++++++++++++++ 5 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 src/Symfony/Component/Validator/Tests/Node/ClassNodeTest.php diff --git a/src/Symfony/Component/Validator/Node/ClassNode.php b/src/Symfony/Component/Validator/Node/ClassNode.php index d49bf81c77..07554963d7 100644 --- a/src/Symfony/Component/Validator/Node/ClassNode.php +++ b/src/Symfony/Component/Validator/Node/ClassNode.php @@ -11,10 +11,13 @@ namespace Symfony\Component\Validator\Node; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Mapping\ClassMetadataInterface; /** - * @since %%NextVersion%% + * Represents an object and its class metadata in the validation graph. + * + * @since 2.5 * @author Bernhard Schussek */ class ClassNode extends Node @@ -24,19 +27,34 @@ class ClassNode extends Node */ public $metadata; - public function __construct($value, ClassMetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups) + /** + * Creates a new class node. + * + * @param object $object The validated object + * @param ClassMetadataInterface $metadata The class metadata of that + * object + * @param string $propertyPath The property path leading + * to this node + * @param string[] $groups The groups in which this + * node should be validated + * @param string[] $cascadedGroups The groups in which + * cascaded objects should be + * validated + * + * @throws UnexpectedTypeException If the given value is not an object + */ + public function __construct($object, ClassMetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups) { - if (!is_object($value)) { - // error + if (!is_object($object)) { + throw new UnexpectedTypeException($object, 'object'); } parent::__construct( - $value, + $object, $metadata, $propertyPath, $groups, $cascadedGroups ); } - } diff --git a/src/Symfony/Component/Validator/Node/GenericNode.php b/src/Symfony/Component/Validator/Node/GenericNode.php index aab73db64e..82ee9ac7fb 100644 --- a/src/Symfony/Component/Validator/Node/GenericNode.php +++ b/src/Symfony/Component/Validator/Node/GenericNode.php @@ -12,7 +12,14 @@ namespace Symfony\Component\Validator\Node; /** - * @since %%NextVersion%% + * Represents a value that has neither class metadata nor property metadata + * attached to it. + * + * Together with {@link \Symfony\Component\Validator\Mapping\GenericMetadata}, + * this node type can be used to validate a value against some given + * constraints. + * + * @since 2.5 * @author Bernhard Schussek */ class GenericNode extends Node diff --git a/src/Symfony/Component/Validator/Node/Node.php b/src/Symfony/Component/Validator/Node/Node.php index 08b2e4da78..014665abf4 100644 --- a/src/Symfony/Component/Validator/Node/Node.php +++ b/src/Symfony/Component/Validator/Node/Node.php @@ -14,21 +14,60 @@ namespace Symfony\Component\Validator\Node; use Symfony\Component\Validator\Mapping\MetadataInterface; /** - * @since %%NextVersion%% + * A node in the validated graph. + * + * @since 2.5 * @author Bernhard Schussek */ abstract class Node { + /** + * The validated value. + * + * @var mixed + */ public $value; + /** + * The metadata specifying how the value should be validated. + * + * @var MetadataInterface + */ public $metadata; + /** + * The property path leading to this node. + * + * @var string + */ public $propertyPath; + /** + * The groups in which the value should be validated. + * + * @var string[] + */ public $groups; + /** + * The groups in which cascaded values should be validated. + * + * @var string[] + */ public $cascadedGroups; + /** + * Creates a new property node. + * + * @param mixed $value The property value + * @param MetadataInterface $metadata The property's metadata + * @param string $propertyPath The property path leading to + * this node + * @param string[] $groups The groups in which this node + * should be validated + * @param string[] $cascadedGroups The groups in which cascaded + * objects should be validated + */ public function __construct($value, MetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups) { $this->value = $value; diff --git a/src/Symfony/Component/Validator/Node/PropertyNode.php b/src/Symfony/Component/Validator/Node/PropertyNode.php index 76cfcb3531..bcb462b176 100644 --- a/src/Symfony/Component/Validator/Node/PropertyNode.php +++ b/src/Symfony/Component/Validator/Node/PropertyNode.php @@ -14,7 +14,20 @@ namespace Symfony\Component\Validator\Node; use Symfony\Component\Validator\Mapping\PropertyMetadataInterface; /** - * @since %%NextVersion%% + * Represents the value of a property and its associated metadata. + * + * If the property contains an object and should be cascaded, a new + * {@link ClassNode} instance will be created for that object. + * + * Example: + * + * (Article:ClassNode) + * \ + * (author:PropertyNode) + * \ + * (Author:ClassNode) + * + * @since 2.5 * @author Bernhard Schussek */ class PropertyNode extends Node @@ -24,6 +37,19 @@ class PropertyNode extends Node */ public $metadata; + /** + * Creates a new property node. + * + * @param mixed $value The property value + * @param PropertyMetadataInterface $metadata The property's metadata + * @param string $propertyPath The property path leading + * to this node + * @param string[] $groups The groups in which this + * node should be validated + * @param string[] $cascadedGroups The groups in which + * cascaded objects should + * be validated + */ public function __construct($value, PropertyMetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups) { parent::__construct( diff --git a/src/Symfony/Component/Validator/Tests/Node/ClassNodeTest.php b/src/Symfony/Component/Validator/Tests/Node/ClassNodeTest.php new file mode 100644 index 0000000000..1241d1bb5b --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Node/ClassNodeTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Node; + +use Symfony\Component\Validator\Node\ClassNode; + +/** + * @since 2.5 + * @author Bernhard Schussek + */ +class ClassNodeTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testConstructorExpectsObject() + { + $metadata = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataInterface'); + + new ClassNode('foobar', $metadata, '', array(), array()); + } +}