From 6641f3e2311b494f080205fe2a957633842b34d3 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 13 Dec 2011 23:40:32 +0100 Subject: [PATCH 1/4] [Validator] Added constraints Optional and Required for the CollectionValidator --- .../Constraints/CollectionValidator.php | 10 +- .../Validator/Constraints/Optional.php | 29 ++++ .../Validator/Constraints/Required.php | 29 ++++ .../Constraints/CollectionValidatorTest.php | 134 ++++++++++++++++++ 4 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Validator/Constraints/Optional.php create mode 100644 src/Symfony/Component/Validator/Constraints/Required.php diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index e703b538e0..832a9b2b6d 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -51,12 +51,18 @@ class CollectionValidator extends ConstraintValidator $extraFields[$field] = $fieldValue; } - foreach ($constraint->fields as $field => $constraints) { + foreach ($constraint->fields as $field => $fieldConstraint) { if ( // bug fix issue #2779 (is_array($value) && array_key_exists($field, $value)) || ($value instanceof \ArrayAccess && $value->offsetExists($field)) ) { + if ($fieldConstraint instanceof Required || $fieldConstraint instanceof Optional) { + $constraints = $fieldConstraint->constraints; + } else { + $constraints = $fieldConstraint; + } + // cannot simply cast to array, because then the object is converted to an // array instead of wrapped inside $constraints = is_array($constraints) ? $constraints : array($constraints); @@ -66,7 +72,7 @@ class CollectionValidator extends ConstraintValidator } unset($extraFields[$field]); - } else { + } else if (!$fieldConstraint instanceof Optional) { $missingFields[] = $field; } } diff --git a/src/Symfony/Component/Validator/Constraints/Optional.php b/src/Symfony/Component/Validator/Constraints/Optional.php new file mode 100644 index 0000000000..9d2c847f60 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Optional.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @api + */ +class Optional extends Constraint +{ + public $constraints = array(); + + public function getDefaultOption() + { + return 'constraints'; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/Required.php b/src/Symfony/Component/Validator/Constraints/Required.php new file mode 100644 index 0000000000..4dfbd48919 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Required.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @api + */ +class Required extends Constraint +{ + public $constraints = array(); + + public function getDefaultOption() + { + return 'constraints'; + } +} diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php index a2f17c3c44..efed5ac65e 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php @@ -15,6 +15,8 @@ use Symfony\Component\Validator\ExecutionContext; use Symfony\Component\Validator\Constraints\Min; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; +use Symfony\Component\Validator\Constraints\Required; +use Symfony\Component\Validator\Constraints\Optional; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\CollectionValidator; @@ -309,6 +311,138 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->assertTrue($result); } + public function testOptionalFieldPresent() + { + $array = array( + 'foo' => null, + ); + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Optional(), + )))); + } + + public function testOptionalFieldNotPresent() + { + $array = array( + ); + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Optional(), + )))); + } + + public function testOptionalFieldSingleConstraint() + { + $this->context->setGroup('MyGroup'); + $this->context->setPropertyPath('bar'); + + $array = array( + 'foo' => 5, + ); + + $constraint = new Min(4); + + $this->walker->expects($this->once()) + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Optional($constraint), + )))); + } + + public function testOptionalFieldMultipleConstraints() + { + $this->context->setGroup('MyGroup'); + $this->context->setPropertyPath('bar'); + + $array = array( + 'foo' => 5, + ); + + $constraints = array( + new NotNull(), + new Min(4), + ); + + foreach ($constraints as $i => $constraint) { + $this->walker->expects($this->at($i)) + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + } + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Optional($constraints), + )))); + } + + public function testRequiredFieldPresent() + { + $array = array( + 'foo' => null, + ); + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Required(), + )))); + } + + public function testRequiredFieldNotPresent() + { + $array = array( + ); + + $this->assertFalse($this->validator->isValid($array, new Collection(array( + 'foo' => new Required(), + )))); + } + + public function testRequiredFieldSingleConstraint() + { + $this->context->setGroup('MyGroup'); + $this->context->setPropertyPath('bar'); + + $array = array( + 'foo' => 5, + ); + + $constraint = new Min(4); + + $this->walker->expects($this->once()) + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Required($constraint), + )))); + } + + public function testRequiredFieldMultipleConstraints() + { + $this->context->setGroup('MyGroup'); + $this->context->setPropertyPath('bar'); + + $array = array( + 'foo' => 5, + ); + + $constraints = array( + new NotNull(), + new Min(4), + ); + + foreach ($constraints as $i => $constraint) { + $this->walker->expects($this->at($i)) + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + } + + $this->assertTrue($this->validator->isValid($array, new Collection(array( + 'foo' => new Required($constraints), + )))); + } + public function testObjectShouldBeLeftUnchanged() { $value = new \ArrayObject(array( From bf5901850a86751e4482caa9b33e2c8d20ca6727 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 15 Jan 2012 19:45:14 +0100 Subject: [PATCH 2/4] [Validator] Removed @api-tag from Optional and Required constraint, since these two are new. --- src/Symfony/Component/Validator/Constraints/Optional.php | 2 -- src/Symfony/Component/Validator/Constraints/Required.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Optional.php b/src/Symfony/Component/Validator/Constraints/Optional.php index 9d2c847f60..b8f258183f 100644 --- a/src/Symfony/Component/Validator/Constraints/Optional.php +++ b/src/Symfony/Component/Validator/Constraints/Optional.php @@ -15,8 +15,6 @@ use Symfony\Component\Validator\Constraint; /** * @Annotation - * - * @api */ class Optional extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/Required.php b/src/Symfony/Component/Validator/Constraints/Required.php index 4dfbd48919..33a86fe4fd 100644 --- a/src/Symfony/Component/Validator/Constraints/Required.php +++ b/src/Symfony/Component/Validator/Constraints/Required.php @@ -15,8 +15,6 @@ use Symfony\Component\Validator\Constraint; /** * @Annotation - * - * @api */ class Required extends Constraint { From 509c7bfb5b8d2f05c561a3fff57476e521939fea Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 15 Jan 2012 20:31:13 +0100 Subject: [PATCH 3/4] [Validator] Moved Optional and Required constraints to dedicated sub namespace. --- .../Validator/Constraints/{ => Collection}/Optional.php | 2 +- .../Validator/Constraints/{ => Collection}/Required.php | 2 +- .../Component/Validator/Constraints/CollectionValidator.php | 2 ++ .../Validator/Constraints/CollectionValidatorTest.php | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) rename src/Symfony/Component/Validator/Constraints/{ => Collection}/Optional.php (88%) rename src/Symfony/Component/Validator/Constraints/{ => Collection}/Required.php (88%) diff --git a/src/Symfony/Component/Validator/Constraints/Optional.php b/src/Symfony/Component/Validator/Constraints/Collection/Optional.php similarity index 88% rename from src/Symfony/Component/Validator/Constraints/Optional.php rename to src/Symfony/Component/Validator/Constraints/Collection/Optional.php index b8f258183f..8d4c7746db 100644 --- a/src/Symfony/Component/Validator/Constraints/Optional.php +++ b/src/Symfony/Component/Validator/Constraints/Collection/Optional.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Validator\Constraints; +namespace Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraint; diff --git a/src/Symfony/Component/Validator/Constraints/Required.php b/src/Symfony/Component/Validator/Constraints/Collection/Required.php similarity index 88% rename from src/Symfony/Component/Validator/Constraints/Required.php rename to src/Symfony/Component/Validator/Constraints/Collection/Required.php index 33a86fe4fd..5dda7434dc 100644 --- a/src/Symfony/Component/Validator/Constraints/Required.php +++ b/src/Symfony/Component/Validator/Constraints/Collection/Required.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Validator\Constraints; +namespace Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraint; diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index 832a9b2b6d..600dbc088a 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -14,6 +14,8 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Constraints\Collection\Optional; +use Symfony\Component\Validator\Constraints\Collection\Required; /** * @api diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php index efed5ac65e..1dd37efa84 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php @@ -15,8 +15,8 @@ use Symfony\Component\Validator\ExecutionContext; use Symfony\Component\Validator\Constraints\Min; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; -use Symfony\Component\Validator\Constraints\Required; -use Symfony\Component\Validator\Constraints\Optional; +use Symfony\Component\Validator\Constraints\Collection\Required; +use Symfony\Component\Validator\Constraints\Collection\Optional; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\CollectionValidator; From e6e3da50633ff6e0c5df5d12a3c1a8f7a21dfd44 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 16 Jan 2012 13:03:38 +0100 Subject: [PATCH 4/4] [Validator] Improved test coverage of CollectionValidator and reduced test code duplication --- .../CollectionValidatorArrayObjectTest.php | 20 ++ .../CollectionValidatorArrayTest.php | 20 ++ ...llectionValidatorCustomArrayObjectTest.php | 80 +++++ .../Constraints/CollectionValidatorTest.php | 287 +++++------------- 4 files changed, 192 insertions(+), 215 deletions(-) create mode 100644 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php create mode 100644 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php create mode 100644 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php new file mode 100644 index 0000000000..834138de9a --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Validator\Constraints; + +class CollectionValidatorArrayObjectTest extends CollectionValidatorTest +{ + public function prepareTestData(array $contents) + { + return new \ArrayObject($contents); + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php new file mode 100644 index 0000000000..deba6d893d --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Validator\Constraints; + +class CollectionValidatorArrayTest extends CollectionValidatorTest +{ + public function prepareTestData(array $contents) + { + return $contents; + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php new file mode 100644 index 0000000000..bd7363d437 --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Validator\Constraints; + +/** +* This class is a hand written simplified version of PHP native `ArrayObject` +* class, to show that it behaves different than PHP native implementation. +*/ +class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable +{ + private $array; + + public function __construct(array $array = null) + { + $this->array = (array) ($array ?: array()); + } + + public function offsetExists($offset) + { + return array_key_exists($offset, $this->array); + } + + public function offsetGet($offset) + { + return $this->array[$offset]; + } + + public function offsetSet($offset, $value) + { + if (null === $offset) { + $this->array[] = $value; + } else { + $this->array[$offset] = $value; + } + } + + public function offsetUnset($offset) + { + if (array_key_exists($offset, $this->array)) { + unset($this->array[$offset]); + } + } + + public function getIterator() + { + return new \ArrayIterator($this->array); + } + + public function count() + { + return count($this->array); + } + + public function serialize() + { + return serialize($this->array); + } + + public function unserialize($serialized) + { + $this->array = (array) unserialize((string) $serialized); + } +} + +class CollectionValidatorCustomArrayObjectTest extends CollectionValidatorTest +{ + public function prepareTestData(array $contents) + { + return new CustomArrayObject($contents); + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php index 1dd37efa84..a1cfc31980 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php @@ -20,67 +20,7 @@ use Symfony\Component\Validator\Constraints\Collection\Optional; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\CollectionValidator; -/** - * This class is a hand written simplified version of PHP native `ArrayObject` - * class, to show that it behaves different than PHP native implementation. - */ -class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable -{ - private $array; - - public function __construct(array $array = null) - { - $this->array = (array) ($array ?: array()); - } - - public function offsetExists($offset) - { - return array_key_exists($offset, $this->array); - } - - public function offsetGet($offset) - { - return $this->array[$offset]; - } - - public function offsetSet($offset, $value) - { - if (null === $offset) { - $this->array[] = $value; - } else { - $this->array[$offset] = $value; - } - } - - public function offsetUnset($offset) - { - if (array_key_exists($offset, $this->array)) { - unset($this->array[$offset]); - } - } - - public function getIterator() - { - return new \ArrayIterator($this->array); - } - - public function count() - { - return count($this->array); - } - - public function serialize() - { - return serialize($this->array); - } - - public function unserialize($serialized) - { - $this->array = (array) unserialize((string) $serialized); - } -} - -class CollectionValidatorTest extends \PHPUnit_Framework_TestCase +abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase { protected $validator; protected $walker; @@ -104,6 +44,8 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->context = null; } + abstract protected function prepareTestData(array $contents); + public function testNullIsValid() { $this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array( @@ -113,13 +55,9 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testFieldsAsDefaultOption() { - $this->assertTrue($this->validator->isValid(array('foo' => 'foobar'), new Collection(array( - 'foo' => new Min(4), - )))); - $this->assertTrue($this->validator->isValid(new \ArrayObject(array('foo' => 'foobar')), new Collection(array( - 'foo' => new Min(4), - )))); - $this->assertTrue($this->validator->isValid(new TestArrayObject(array('foo' => 'foobar')), new Collection(array( + $data = $this->prepareTestData(array('foo' => 'foobar')); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Min(4), )))); } @@ -134,61 +72,66 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase )))); } - /** - * @dataProvider getValidArguments - */ - public function testWalkSingleConstraint($array) + public function testWalkSingleConstraint() { $this->context->setGroup('MyGroup'); $this->context->setPropertyPath('foo'); $constraint = new Min(4); + $array = array('foo' => 3); + foreach ($array as $key => $value) { $this->walker->expects($this->once()) - ->method('walkConstraint') - ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']')); + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']')); } - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData($array); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'fields' => array( 'foo' => $constraint, ), )))); } - /** - * @dataProvider getValidArguments - */ - public function testWalkMultipleConstraints($array) + public function testWalkMultipleConstraints() { $this->context->setGroup('MyGroup'); $this->context->setPropertyPath('foo'); - $constraint = new Min(4); - // can only test for twice the same constraint because PHPUnits mocking - // can't test method calls with different arguments - $constraints = array($constraint, $constraint); + $constraints = array( + new Min(4), + new NotNull(), + ); + $array = array('foo' => 3); foreach ($array as $key => $value) { - $this->walker->expects($this->exactly(2)) - ->method('walkConstraint') - ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']')); + foreach ($constraints as $i => $constraint) { + $this->walker->expects($this->at($i)) + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']')); + } } - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData($array); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'fields' => array( 'foo' => $constraints, ) )))); } - /** - * @dataProvider getArgumentsWithExtraFields - */ - public function testExtraFieldsDisallowed($array) + public function testExtraFieldsDisallowed() { - $this->assertFalse($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData(array( + 'foo' => 5, + 'bar' => 6, + )); + + $this->assertFalse($this->validator->isValid($data, new Collection(array( 'fields' => array( 'foo' => new Min(4), ), @@ -198,26 +141,24 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase // bug fix public function testNullNotConsideredExtraField() { - $array = array( + $data = $this->prepareTestData(array( 'foo' => null, - ); + )); $collection = new Collection(array( 'fields' => array( 'foo' => new Min(4), ), )); - $this->assertTrue($this->validator->isValid($array, $collection)); - $this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection)); - $this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection)); + $this->assertTrue($this->validator->isValid($data, $collection)); } public function testExtraFieldsAllowed() { - $array = array( + $data = $this->prepareTestData(array( 'foo' => 5, 'bar' => 6, - ); + )); $collection = new Collection(array( 'fields' => array( 'foo' => new Min(4), @@ -225,24 +166,14 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase 'allowExtraFields' => true, )); - $this->assertTrue($this->validator->isValid($array, $collection)); - $this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection)); - $this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection)); + $this->assertTrue($this->validator->isValid($data, $collection)); } public function testMissingFieldsDisallowed() { - $this->assertFalse($this->validator->isValid(array(), new Collection(array( - 'fields' => array( - 'foo' => new Min(4), - ), - )))); - $this->assertFalse($this->validator->isValid(new \ArrayObject(array()), new Collection(array( - 'fields' => array( - 'foo' => new Min(4), - ), - )))); - $this->assertFalse($this->validator->isValid(new TestArrayObject(array()), new Collection(array( + $data = $this->prepareTestData(array()); + + $this->assertFalse($this->validator->isValid($data, new Collection(array( 'fields' => array( 'foo' => new Min(4), ), @@ -251,83 +182,32 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testMissingFieldsAllowed() { - $this->assertTrue($this->validator->isValid(array(), new Collection(array( + $data = $this->prepareTestData(array()); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'fields' => array( 'foo' => new Min(4), ), 'allowMissingFields' => true, )))); - $this->assertTrue($this->validator->isValid(new \ArrayObject(array()), new Collection(array( - 'fields' => array( - 'foo' => new Min(4), - ), - 'allowMissingFields' => true, - )))); - $this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array( - 'fields' => array( - 'foo' => new Min(4), - ), - 'allowMissingFields' => true, - )))); - } - - public function testArrayAccessObject() { - $value = new TestArrayObject(); - $value['foo'] = 12; - $value['asdf'] = 'asdfaf'; - - $this->assertTrue(isset($value['asdf'])); - $this->assertTrue(isset($value['foo'])); - $this->assertFalse(empty($value['asdf'])); - $this->assertFalse(empty($value['foo'])); - - $result = $this->validator->isValid($value, new Collection(array( - 'fields' => array( - 'foo' => new NotBlank(), - 'asdf' => new NotBlank() - ) - ))); - - $this->assertTrue($result); - } - - public function testArrayObject() { - $value = new \ArrayObject(array()); - $value['foo'] = 12; - $value['asdf'] = 'asdfaf'; - - $this->assertTrue(isset($value['asdf'])); - $this->assertTrue(isset($value['foo'])); - $this->assertFalse(empty($value['asdf'])); - $this->assertFalse(empty($value['foo'])); - - $result = $this->validator->isValid($value, new Collection(array( - 'fields' => array( - 'foo' => new NotBlank(), - 'asdf' => new NotBlank() - ) - ))); - - $this->assertTrue($result); } public function testOptionalFieldPresent() { - $array = array( + $data = $this->prepareTestData(array( 'foo' => null, - ); + )); - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Optional(), )))); } public function testOptionalFieldNotPresent() { - $array = array( - ); + $data = $this->prepareTestData(array()); - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Optional(), )))); } @@ -347,7 +227,9 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ->method('walkConstraint') ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData($array); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Optional($constraint), )))); } @@ -372,28 +254,29 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); } - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData($array); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Optional($constraints), )))); } public function testRequiredFieldPresent() { - $array = array( + $data = $this->prepareTestData(array( 'foo' => null, - ); + )); - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Required(), )))); } public function testRequiredFieldNotPresent() { - $array = array( - ); + $data = $this->prepareTestData(array()); - $this->assertFalse($this->validator->isValid($array, new Collection(array( + $this->assertFalse($this->validator->isValid($data, new Collection(array( 'foo' => new Required(), )))); } @@ -410,10 +293,12 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $constraint = new Min(4); $this->walker->expects($this->once()) - ->method('walkConstraint') - ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); - $this->assertTrue($this->validator->isValid($array, new Collection(array( + $data = $this->prepareTestData($array); + + $this->assertTrue($this->validator->isValid($data, new Collection(array( 'foo' => new Required($constraint), )))); } @@ -434,10 +319,12 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase foreach ($constraints as $i => $constraint) { $this->walker->expects($this->at($i)) - ->method('walkConstraint') - ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); + ->method('walkConstraint') + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); } + $data = $this->prepareTestData($array); + $this->assertTrue($this->validator->isValid($array, new Collection(array( 'foo' => new Required($constraints), )))); @@ -458,34 +345,4 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase 'foo' => 3 ), (array) $value); } - - public function getValidArguments() - { - return array( - // can only test for one entry, because PHPUnits mocking does not allow - // to expect multiple method calls with different arguments - array(array('foo' => 3)), - array(new \ArrayObject(array('foo' => 3))), - array(new TestArrayObject(array('foo' => 3))), - ); - } - - public function getArgumentsWithExtraFields() - { - return array( - array(array( - 'foo' => 5, - 'bar' => 6, - )), - array(new \ArrayObject(array( - 'foo' => 5, - 'bar' => 6, - ))), - array(new TestArrayObject(array( - 'foo' => 5, - 'bar' => 6, - ))) - ); - } } -