Refactor "ArrayVal" rule and add integration tests

This commit is contained in:
Guilherme Siani 2015-10-17 17:52:35 -03:00 committed by Henrique Moody
parent f08a1fa9fc
commit 1c254ab682
9 changed files with 101 additions and 56 deletions

View file

@ -37,6 +37,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
### Changed ### Changed
- Add country code to the message of "PostalCode" exception rule (#413) - 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 "Between" rule inclusive (#445)
- Make "Max" rule inclusive (#445) - Make "Max" rule inclusive (#445)
- Make "Min" rule inclusive (#445) - Make "Min" rule inclusive (#445)

View file

@ -2,15 +2,21 @@
- `v::arrayVal()` - `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 ```php
v::arrayVal()->validate([]); //true v::arrayVal()->validate([]); // true
v::arrayVal()->validate(new ArrayObject); //true v::arrayVal()->validate(new ArrayObject); // true
``` ```
*** ***
See also: See also:
* [ArrayType](ArrayType.md)
* [Countable](Countable.md)
* [Each](Each.md) * [Each](Each.md)
* [Iterable](Iterable.md)
* [Key](Key.md) * [Key](Key.md)
* [KeySet](KeySet.md)
* [KeyValue](KeyValue.md)

View file

@ -15,8 +15,6 @@ class ArrayVal extends AbstractRule
{ {
public function validate($input) public function validate($input)
{ {
return is_array($input) || ($input instanceof \ArrayAccess return is_array($input) || $input instanceof \ArrayAccess;
&& $input instanceof \Traversable
&& $input instanceof \Countable);
} }
} }

View file

@ -0,0 +1,11 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::arrayVal()->assert(['asdf', 'lkjh']);
v::arrayVal()->check(new ArrayObject([2, 3]));
?>
--EXPECTF--

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ArrayValException;
use Respect\Validation\Validator as v;
try {
v::arrayVal()->check('Bla %123');
} catch (ArrayValException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
"Bla %123" must be an array

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::arrayVal()->assert(new stdClass());
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
\-`[object] (stdClass: { })` must be an array

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ArrayValException;
use Respect\Validation\Validator as v;
try {
v::not(v::arrayVal())->check([42]);
} catch (ArrayValException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
{ 42 } must not be an array

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::not(v::arrayVal())->assert(new ArrayObject([2, 3]));
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
\-`[traversable] (ArrayObject: { 2, 3 })` must not be an array

View file

@ -11,69 +11,34 @@
namespace Respect\Validation\Rules; namespace Respect\Validation\Rules;
class TestAccess extends \ArrayObject implements \ArrayAccess, \Countable, \Traversable
{
}
/** /**
* @group rule * @group rule
* @covers Respect\Validation\Rules\ArrayVal * @covers Respect\Validation\Rules\ArrayVal
* @covers Respect\Validation\Exceptions\ArrayValException
*/ */
class ArrayValTest extends \PHPUnit_Framework_TestCase class ArrayValTest extends RuleTestCase
{ {
protected $object; public function providerForValidInput()
protected function setUp()
{ {
$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 [ return [
[[]], [$rule, []],
[[1, 2, 3]], [$rule, [1, 2, 3]],
[new TestAccess()], [$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 [ return [
[''], [$rule, ''],
[null], [$rule, null],
[121], [$rule, 121],
[new \stdClass()], [$rule, new \stdClass()],
[false], [$rule, false],
['aaa'], [$rule, 'aaa'],
]; ];
} }
} }