Skip to content

Loading…

[Validator] Improve support for optional/required fields in Collection constraint #3118

Merged
merged 4 commits into from

5 participants

@webmozart
Symfony member

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: none
Todo: none

Travis Build Status

Improves the Collection constraint to test on a more granular level if entries of the collection are optional or required. Before this could only be set using the "allowExtraFields" and "allowMissingFields" options, but these are very general and limited.

The former syntax - without Optional or Required - is still supported.

Usage:

$array = array(
    'name' => 'Bernhard',
    'birthdate' => '1970-01-01',
);
$validator->validate($array, null, new Collection(array(
    'name' => new Required(),
    'birthdate' => new Optional(),
));

// you can also pass additional constraints for the fields
$validator->validate($array, null, new Collection(array(
    'name' => new Required(array(
        new Type('string'),
        new MinLength(3),
    )),
    'birthdate' => new Optional(new Date()),
));
@stof stof commented on the diff
...mponent/Validator/Constraints/CollectionValidator.php
@@ -62,7 +68,7 @@ public function isValid($value, Constraint $constraint)
}
unset($extraFields[$field]);
- } else {
+ } else if (!$fieldConstraint instanceof Optional) {
@stof Symfony member
stof added a note

should be elseif without space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff
...ent/Validator/Constraints/CollectionValidatorTest.php
((23 lines not shown))
+ }
+
+ 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]'));
@stof Symfony member
stof added a note

wrong indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff
...ent/Validator/Constraints/CollectionValidatorTest.php
((47 lines not shown))
+ $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]'));
@stof Symfony member
stof added a note

same here

@webmozart Symfony member

Oh man, thanks for the heads up (striding too much in non-PHP land..) :) Fixing ASAP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff
...ent/Validator/Constraints/CollectionValidatorTest.php
((89 lines not shown))
+ }
+
+ 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]'));
@stof Symfony member
stof added a note

and here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff
...ent/Validator/Constraints/CollectionValidatorTest.php
((113 lines not shown))
+ $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]'));
@stof Symfony member
stof added a note

and here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@canni

@bschussek I've rewritten a lot of test code for Collection validator in 2.0 branch and also had modified validator itself, as it had a bug #3078, consider waiting with this PR till fabpot will merge 2.0 back into master, as there will be code conflicts :)

@Koc

Does it helps to #2615 ?

@fabpot
Symfony member

@canni: I've just merged 2.0 into master.

@webmozart
Symfony member

@fabpot: Rebased. I also fixed the CS issues mentioned by @stof.

@fabpot fabpot added a commit that referenced this pull request
@fabpot fabpot merged branch bschussek/collection-validator (PR #3118)
Commits
-------

e6e3da5 [Validator] Improved test coverage of CollectionValidator and reduced test code duplication
509c7bf [Validator] Moved Optional and Required constraints to dedicated sub namespace.
bf59018 [Validator] Removed @api-tag from Optional and Required constraint, since these two are new.
6641f3e [Validator] Added constraints Optional and Required for the CollectionValidator

Discussion
----------

[Validator] Improve support for optional/required fields in Collection constraint

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: none
Todo: none

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=collection-validator)

Improves the `Collection` constraint to test on a more granular level if entries of the collection are optional or required. Before this could only be set using the "allowExtraFields" and "allowMissingFields" options, but these are very general and limited.

The former syntax - without Optional or Required - is still supported.

Usage:

    $array = array(
        'name' => 'Bernhard',
        'birthdate' => '1970-01-01',
    );
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(),
        'birthdate' => new Optional(),
    ));

    // you can also pass additional constraints for the fields
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(array(
            new Type('string'),
            new MinLength(3),
        )),
        'birthdate' => new Optional(new Date()),
    ));

---------------------------------------------------------------------------

by canni at 2012-01-15T20:22:17Z

@bschussek I've rewritten a lot of test code for Collection validator in 2.0 branch and also had modified validator itself, as it had a bug #3078, consider waiting with this PR till fabpot will merge 2.0 back into master, as there will be code conflicts :)

---------------------------------------------------------------------------

by Koc at 2012-01-15T23:13:04Z

Does it helps to #2615 ?

---------------------------------------------------------------------------

by fabpot at 2012-01-16T06:44:53Z

@canni: I've just merged 2.0 into master.

---------------------------------------------------------------------------

by bschussek at 2012-01-16T12:05:19Z

@fabpot: Rebased. I also fixed the CS issues mentioned by @stof.
e056480
@fabpot fabpot merged commit e6e3da5 into symfony:master
@mmucklo mmucklo pushed a commit that referenced this pull request
@fabpot fabpot merged branch bschussek/collection-validator (PR #3118)
Commits
-------

e6e3da5 [Validator] Improved test coverage of CollectionValidator and reduced test code duplication
509c7bf [Validator] Moved Optional and Required constraints to dedicated sub namespace.
bf59018 [Validator] Removed @api-tag from Optional and Required constraint, since these two are new.
6641f3e [Validator] Added constraints Optional and Required for the CollectionValidator

Discussion
----------

[Validator] Improve support for optional/required fields in Collection constraint

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: none
Todo: none

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=collection-validator)

Improves the `Collection` constraint to test on a more granular level if entries of the collection are optional or required. Before this could only be set using the "allowExtraFields" and "allowMissingFields" options, but these are very general and limited.

The former syntax - without Optional or Required - is still supported.

Usage:

    $array = array(
        'name' => 'Bernhard',
        'birthdate' => '1970-01-01',
    );
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(),
        'birthdate' => new Optional(),
    ));

    // you can also pass additional constraints for the fields
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(array(
            new Type('string'),
            new MinLength(3),
        )),
        'birthdate' => new Optional(new Date()),
    ));

---------------------------------------------------------------------------

by canni at 2012-01-15T20:22:17Z

@bschussek I've rewritten a lot of test code for Collection validator in 2.0 branch and also had modified validator itself, as it had a bug #3078, consider waiting with this PR till fabpot will merge 2.0 back into master, as there will be code conflicts :)

---------------------------------------------------------------------------

by Koc at 2012-01-15T23:13:04Z

Does it helps to #2615 ?

---------------------------------------------------------------------------

by fabpot at 2012-01-16T06:44:53Z

@canni: I've just merged 2.0 into master.

---------------------------------------------------------------------------

by bschussek at 2012-01-16T12:05:19Z

@fabpot: Rebased. I also fixed the CS issues mentioned by @stof.
7a5e02f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
27 src/Symfony/Component/Validator/Constraints/Collection/Optional.php
@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Constraints\Collection;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class Optional extends Constraint
+{
+ public $constraints = array();
+
+ public function getDefaultOption()
+ {
+ return 'constraints';
+ }
+}
View
27 src/Symfony/Component/Validator/Constraints/Collection/Required.php
@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Constraints\Collection;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class Required extends Constraint
+{
+ public $constraints = array();
+
+ public function getDefaultOption()
+ {
+ return 'constraints';
+ }
+}
View
12 src/Symfony/Component/Validator/Constraints/CollectionValidator.php
@@ -14,6 +14,8 @@
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
@@ -51,12 +53,18 @@ public function isValid($value, Constraint $constraint)
$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 +74,7 @@ public function isValid($value, Constraint $constraint)
}
unset($extraFields[$field]);
- } else {
+ } else if (!$fieldConstraint instanceof Optional) {
@stof Symfony member
stof added a note

should be elseif without space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
$missingFields[] = $field;
}
}
View
20 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php
@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * 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);
+ }
+}
View
20 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php
@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * 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;
+ }
+}
View
80 ...ymfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php
@@ -0,0 +1,80 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * 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);
+ }
+}
View
359 tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php
@@ -15,70 +15,12 @@
use Symfony\Component\Validator\Constraints\Min;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
+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;
-/**
- * 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;
@@ -102,6 +44,8 @@ protected function tearDown()
$this->context = null;
}
+ abstract protected function prepareTestData(array $contents);
+
public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
@@ -111,13 +55,9 @@ public function testNullIsValid()
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),
))));
}
@@ -132,61 +72,66 @@ public function testThrowsExceptionIfNotTraversable()
))));
}
- /**
- * @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),
),
@@ -196,26 +141,24 @@ public function testExtraFieldsDisallowed($array)
// 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),
@@ -223,24 +166,14 @@ public function testExtraFieldsAllowed()
'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),
),
@@ -249,64 +182,152 @@ public function testMissingFieldsDisallowed()
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,
+ }
+
+ public function testOptionalFieldPresent()
+ {
+ $data = $this->prepareTestData(array(
+ 'foo' => null,
+ ));
+
+ $this->assertTrue($this->validator->isValid($data, new Collection(array(
+ 'foo' => new Optional(),
))));
- $this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
- 'fields' => array(
- 'foo' => new Min(4),
- ),
- 'allowMissingFields' => true,
+ }
+
+ public function testOptionalFieldNotPresent()
+ {
+ $data = $this->prepareTestData(array());
+
+ $this->assertTrue($this->validator->isValid($data, new Collection(array(
+ 'foo' => new Optional(),
))));
}
- public function testArrayAccessObject() {
- $value = new TestArrayObject();
- $value['foo'] = 12;
- $value['asdf'] = 'asdfaf';
+ public function testOptionalFieldSingleConstraint()
+ {
+ $this->context->setGroup('MyGroup');
+ $this->context->setPropertyPath('bar');
- $this->assertTrue(isset($value['asdf']));
- $this->assertTrue(isset($value['foo']));
- $this->assertFalse(empty($value['asdf']));
- $this->assertFalse(empty($value['foo']));
+ $array = array(
+ 'foo' => 5,
+ );
- $result = $this->validator->isValid($value, new Collection(array(
- 'fields' => array(
- 'foo' => new NotBlank(),
- 'asdf' => new NotBlank()
- )
- )));
+ $constraint = new Min(4);
- $this->assertTrue($result);
+ $this->walker->expects($this->once())
+ ->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($data, new Collection(array(
+ 'foo' => new Optional($constraint),
+ ))));
}
- public function testArrayObject() {
- $value = new \ArrayObject(array());
- $value['foo'] = 12;
- $value['asdf'] = 'asdfaf';
+ public function testOptionalFieldMultipleConstraints()
+ {
+ $this->context->setGroup('MyGroup');
+ $this->context->setPropertyPath('bar');
+
+ $array = array(
+ 'foo' => 5,
+ );
- $this->assertTrue(isset($value['asdf']));
- $this->assertTrue(isset($value['foo']));
- $this->assertFalse(empty($value['asdf']));
- $this->assertFalse(empty($value['foo']));
+ $constraints = array(
+ new NotNull(),
+ new Min(4),
+ );
- $result = $this->validator->isValid($value, new Collection(array(
- 'fields' => array(
- 'foo' => new NotBlank(),
- 'asdf' => new NotBlank()
- )
- )));
+ 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($result);
+ $data = $this->prepareTestData($array);
+
+ $this->assertTrue($this->validator->isValid($data, new Collection(array(
+ 'foo' => new Optional($constraints),
+ ))));
+ }
+
+ public function testRequiredFieldPresent()
+ {
+ $data = $this->prepareTestData(array(
+ 'foo' => null,
+ ));
+
+ $this->assertTrue($this->validator->isValid($data, new Collection(array(
+ 'foo' => new Required(),
+ ))));
+ }
+
+ public function testRequiredFieldNotPresent()
+ {
+ $data = $this->prepareTestData(array());
+
+ $this->assertFalse($this->validator->isValid($data, 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]'));
+
+ $data = $this->prepareTestData($array);
+
+ $this->assertTrue($this->validator->isValid($data, 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]'));
+ }
+
+ $data = $this->prepareTestData($array);
+
+ $this->assertTrue($this->validator->isValid($array, new Collection(array(
+ 'foo' => new Required($constraints),
+ ))));
}
public function testObjectShouldBeLeftUnchanged()
@@ -324,34 +345,4 @@ public function testObjectShouldBeLeftUnchanged()
'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,
- )))
- );
- }
}
-
Something went wrong with that request. Please try again.