diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cbc9fae..9b3df246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ All notable changes of the Respect\Validation releases are documented in this fi ### Changed - Add country code to the message of "PostalCode" exception rule (#413) +- Make "ArrayVal" validate only if the input can be used as an array (#574) - Make "Between" rule inclusive (#445) - Make "Max" rule inclusive (#445) - Make "Min" rule inclusive (#445) diff --git a/docs/ArrayVal.md b/docs/ArrayVal.md index ef8deaf9..8beec4b4 100644 --- a/docs/ArrayVal.md +++ b/docs/ArrayVal.md @@ -2,15 +2,21 @@ - `v::arrayVal()` -Validates if the input is an array or traversable object. +Validates if the input is an array or if the input be used as an array +(instance of `ArrayAccess`). ```php -v::arrayVal()->validate([]); //true -v::arrayVal()->validate(new ArrayObject); //true +v::arrayVal()->validate([]); // true +v::arrayVal()->validate(new ArrayObject); // true ``` *** See also: + * [ArrayType](ArrayType.md) + * [Countable](Countable.md) * [Each](Each.md) + * [Iterable](Iterable.md) * [Key](Key.md) + * [KeySet](KeySet.md) + * [KeyValue](KeyValue.md) diff --git a/library/Rules/ArrayVal.php b/library/Rules/ArrayVal.php index ea38cf5e..4b0a091a 100644 --- a/library/Rules/ArrayVal.php +++ b/library/Rules/ArrayVal.php @@ -15,8 +15,6 @@ class ArrayVal extends AbstractRule { public function validate($input) { - return is_array($input) || ($input instanceof \ArrayAccess - && $input instanceof \Traversable - && $input instanceof \Countable); + return is_array($input) || $input instanceof \ArrayAccess; } } diff --git a/tests/integration/arrayVal_1.phpt b/tests/integration/arrayVal_1.phpt new file mode 100644 index 00000000..a76ca0a5 --- /dev/null +++ b/tests/integration/arrayVal_1.phpt @@ -0,0 +1,11 @@ +--FILE-- +assert(['asdf', 'lkjh']); +v::arrayVal()->check(new ArrayObject([2, 3])); +?> +--EXPECTF-- diff --git a/tests/integration/arrayVal_2.phpt b/tests/integration/arrayVal_2.phpt new file mode 100644 index 00000000..bcf9fa92 --- /dev/null +++ b/tests/integration/arrayVal_2.phpt @@ -0,0 +1,16 @@ +--FILE-- +check('Bla %123'); +} catch (ArrayValException $exception) { + echo $exception->getMainMessage(); +} +?> +--EXPECTF-- +"Bla %123" must be an array diff --git a/tests/integration/arrayVal_3.phpt b/tests/integration/arrayVal_3.phpt new file mode 100644 index 00000000..8f28b810 --- /dev/null +++ b/tests/integration/arrayVal_3.phpt @@ -0,0 +1,16 @@ +--FILE-- +assert(new stdClass()); +} catch (AllOfException $exception) { + echo $exception->getFullMessage(); +} +?> +--EXPECTF-- +\-`[object] (stdClass: { })` must be an array diff --git a/tests/integration/arrayVal_4.phpt b/tests/integration/arrayVal_4.phpt new file mode 100644 index 00000000..bacf34a2 --- /dev/null +++ b/tests/integration/arrayVal_4.phpt @@ -0,0 +1,16 @@ +--FILE-- +check([42]); +} catch (ArrayValException $exception) { + echo $exception->getMainMessage(); +} +?> +--EXPECTF-- +{ 42 } must not be an array diff --git a/tests/integration/arrayVal_5.phpt b/tests/integration/arrayVal_5.phpt new file mode 100644 index 00000000..241d5f26 --- /dev/null +++ b/tests/integration/arrayVal_5.phpt @@ -0,0 +1,16 @@ +--FILE-- +assert(new ArrayObject([2, 3])); +} catch (AllOfException $exception) { + echo $exception->getFullMessage(); +} +?> +--EXPECTF-- +\-`[traversable] (ArrayObject: { 2, 3 })` must not be an array diff --git a/tests/unit/Rules/ArrayValTest.php b/tests/unit/Rules/ArrayValTest.php index f35481cf..13cafc4b 100644 --- a/tests/unit/Rules/ArrayValTest.php +++ b/tests/unit/Rules/ArrayValTest.php @@ -11,69 +11,34 @@ namespace Respect\Validation\Rules; -class TestAccess extends \ArrayObject implements \ArrayAccess, \Countable, \Traversable -{ -} - /** * @group rule * @covers Respect\Validation\Rules\ArrayVal - * @covers Respect\Validation\Exceptions\ArrayValException */ -class ArrayValTest extends \PHPUnit_Framework_TestCase +class ArrayValTest extends RuleTestCase { - protected $object; - - protected function setUp() + public function providerForValidInput() { - $this->object = new ArrayVal(); - } + $rule = new ArrayVal(); - /** - * @dataProvider providerForArray - */ - public function testValidArrayOrArrayObjectShouldReturnTrue($input) - { - $this->assertTrue($this->object->__invoke($input)); - $this->assertTrue($this->object->assert($input)); - $this->assertTrue($this->object->check($input)); - } - - /** - * @dataProvider providerForNotArray - * @expectedException Respect\Validation\Exceptions\ArrayValException - */ - public function testNotArraysShouldThrowArrException($input) - { - $this->assertFalse($this->object->__invoke($input)); - $this->assertFalse($this->object->assert($input)); - } - - public function providerForArray() - { return [ - [[]], - [[1, 2, 3]], - [new TestAccess()], + [$rule, []], + [$rule, [1, 2, 3]], + [$rule, new \ArrayObject()], ]; - - $validator = v::alnum()->length(1, 10); - - $validator = new \Respect\Validation\Rules\AllOf( - new Respect\Validation\Rules\Alnum(), - new Respect\Validation\Rules\Length(1, 10) - ); } - public function providerForNotArray() + public function providerForInvalidInput() { + $rule = new ArrayVal(); + return [ - [''], - [null], - [121], - [new \stdClass()], - [false], - ['aaa'], + [$rule, ''], + [$rule, null], + [$rule, 121], + [$rule, new \stdClass()], + [$rule, false], + [$rule, 'aaa'], ]; } }