mirror of
https://github.com/Respect/Validation.git
synced 2024-06-15 03:55:07 +02:00
Refactor "ArrayVal" rule and add integration tests
This commit is contained in:
parent
f08a1fa9fc
commit
1c254ab682
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
11
tests/integration/arrayVal_1.phpt
Normal file
11
tests/integration/arrayVal_1.phpt
Normal 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--
|
16
tests/integration/arrayVal_2.phpt
Normal file
16
tests/integration/arrayVal_2.phpt
Normal 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
|
16
tests/integration/arrayVal_3.phpt
Normal file
16
tests/integration/arrayVal_3.phpt
Normal 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
|
16
tests/integration/arrayVal_4.phpt
Normal file
16
tests/integration/arrayVal_4.phpt
Normal 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
|
16
tests/integration/arrayVal_5.phpt
Normal file
16
tests/integration/arrayVal_5.phpt
Normal 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
|
|
@ -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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue