mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
Improvements on Each rule and test classes.
This commit is contained in:
parent
d579ce4efc
commit
58461766e4
|
@ -13,9 +13,8 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Validatable;
|
||||
use Traversable;
|
||||
|
||||
class Each extends AbstractRule
|
||||
class Each extends Iterable
|
||||
{
|
||||
public $itemValidator;
|
||||
public $keyValidator;
|
||||
|
@ -30,14 +29,10 @@ class Each extends AbstractRule
|
|||
{
|
||||
$exceptions = [];
|
||||
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
if (!parent::validate($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator)) {
|
||||
try {
|
||||
|
@ -65,11 +60,7 @@ class Each extends AbstractRule
|
|||
|
||||
public function check($input)
|
||||
{
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
if (!parent::validate($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
|
@ -88,14 +79,10 @@ class Each extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
if (!parent::validate($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
|
||||
return false;
|
||||
|
|
|
@ -16,13 +16,52 @@ namespace Respect\Validation\Rules;
|
|||
* @covers Respect\Validation\Rules\Each
|
||||
* @covers Respect\Validation\Exceptions\EachException
|
||||
*/
|
||||
class EachTest extends \PHPUnit_Framework_TestCase
|
||||
class EachTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput()
|
||||
{
|
||||
$ruleNotEmpty = new Each($this->getRuleMock(true));
|
||||
$ruleAlphaItemIntKey = new Each($this->getRuleMock(true), $this->getRuleMock(true));
|
||||
$ruleOnlyKeyValidation = new Each(null, $this->getRuleMock(true));
|
||||
|
||||
$intStack = new \SplStack();
|
||||
$intStack->push(1);
|
||||
$intStack->push(2);
|
||||
$intStack->push(3);
|
||||
$intStack->push(4);
|
||||
$intStack->push(5);
|
||||
|
||||
$stdClass = new \stdClass;
|
||||
$stdClass->name = 'Emmerson';
|
||||
$stdClass->age = 22;
|
||||
|
||||
return [
|
||||
[$ruleNotEmpty, [1, 2, 3, 4, 5]],
|
||||
[$ruleNotEmpty, $intStack],
|
||||
[$ruleNotEmpty, $stdClass],
|
||||
[$ruleAlphaItemIntKey, ['a', 'b', 'c', 'd', 'e']],
|
||||
[$ruleOnlyKeyValidation, ['a', 'b', 'c', 'd', 'e']],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidInput()
|
||||
{
|
||||
$rule = new Each($this->getRuleMock(false));
|
||||
$ruleOnlyKeyValidation = new Each(null, $this->getRuleMock(false));
|
||||
|
||||
return [
|
||||
[$rule, 123],
|
||||
[$rule, ''],
|
||||
[$rule, null],
|
||||
[$rule, false],
|
||||
[$rule, ['', 2, 3, 4, 5]],
|
||||
[$ruleOnlyKeyValidation, ['age' => 22]],
|
||||
];
|
||||
}
|
||||
|
||||
public function testValidatorShouldPassIfEveryArrayItemPass()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->validate([1, 2, 3, 4, 5]);
|
||||
$this->assertTrue($result);
|
||||
$v = new Each($this->getRuleMock(true));
|
||||
$result = $v->check([1, 2, 3, 4, 5]);
|
||||
$this->assertTrue($result);
|
||||
$result = $v->assert([1, 2, 3, 4, 5]);
|
||||
|
@ -31,9 +70,7 @@ class EachTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testValidatorShouldPassIfEveryArrayItemAndKeyPass()
|
||||
{
|
||||
$v = new Each(new Alpha(), new IntVal());
|
||||
$result = $v->validate(['a', 'b', 'c', 'd', 'e']);
|
||||
$this->assertTrue($result);
|
||||
$v = new Each($this->getRuleMock(true), $this->getRuleMock(true));
|
||||
$result = $v->check(['a', 'b', 'c', 'd', 'e']);
|
||||
$this->assertTrue($result);
|
||||
$result = $v->assert(['a', 'b', 'c', 'd', 'e']);
|
||||
|
@ -42,9 +79,7 @@ class EachTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testValidatorShouldPassWithOnlyKeyValidation()
|
||||
{
|
||||
$v = new Each(null, new IntVal());
|
||||
$result = $v->validate(['a', 'b', 'c', 'd', 'e']);
|
||||
$this->assertTrue($result);
|
||||
$v = new Each(null, $this->getRuleMock(true));
|
||||
$result = $v->check(['a', 'b', 'c', 'd', 'e']);
|
||||
$this->assertTrue($result);
|
||||
$result = $v->assert(['a', 'b', 'c', 'd', 'e']);
|
||||
|
@ -56,23 +91,8 @@ class EachTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public function testValidatorShouldNotPassWithOnlyKeyValidation()
|
||||
{
|
||||
$v = new Each(null, new StringType());
|
||||
$result = $v->assert(['a', 'b', 'c', 'd', 'e']);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testNotTraversableValidatorShouldFail()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->validate(null);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testValidatorShouldFailOnInvalidItem()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->validate(['', 2, 3, 4, 5]);
|
||||
$this->assertFalse($result);
|
||||
$v = new Each(null, $this->getRuleMock(false));
|
||||
$v->assert(['a', 'b', 'c', 'd', 'e']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,18 +100,25 @@ class EachTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public function testAssertShouldFailOnInvalidItem()
|
||||
{
|
||||
$v = new Each(new IntVal());
|
||||
$result = $v->assert(['a', 2, 3, 4, 5]);
|
||||
$this->assertFalse($result);
|
||||
$v = new Each($this->getRuleMock(false));
|
||||
$v->assert(['a', 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\EachException
|
||||
*/
|
||||
public function testAssertShouldFailOnNonTraversable()
|
||||
public function testAssertShouldFailWithNonIterableInput()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->assert(123);
|
||||
$this->assertFalse($result);
|
||||
$v = new Each($this->getRuleMock(false));
|
||||
$v->assert('a');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\EachException
|
||||
*/
|
||||
public function testCheckShouldFailWithNonIterableInput()
|
||||
{
|
||||
$v = new Each($this->getRuleMock(false));
|
||||
$v->check(null);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue