mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Name refactorings, interface for simple validator calls, date improvements
This commit is contained in:
parent
348cb019fe
commit
9aee2ea16d
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Respect\Validation;
|
namespace Respect\Validation;
|
||||||
|
|
||||||
abstract class AbstractValidator
|
abstract class AbstractNode
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $messages = array();
|
protected $messages = array();
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation;
|
||||||
|
|
||||||
use LogicException;
|
use LogicException;
|
||||||
|
|
||||||
class Chain extends Validator
|
class Chain extends CompositeValidator
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $input;
|
protected $input;
|
||||||
|
|
119
library/Respect/Validation/CompositeValidator.php
Normal file
119
library/Respect/Validation/CompositeValidator.php
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Respect\Validation;
|
||||||
|
|
||||||
|
class CompositeValidator extends AbstractNode implements Validatable
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $validators = array();
|
||||||
|
|
||||||
|
protected function appendValidator(Validatable $validator)
|
||||||
|
{
|
||||||
|
$this->validators[spl_object_hash($validator)] = $validator;
|
||||||
|
$this->messages = array_merge(
|
||||||
|
$this->messages, $validator->getMessages()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValidator($validator, $arguments=array())
|
||||||
|
{
|
||||||
|
$this->appendValidator(Validator::buildValidator($validator, $arguments));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasValidator($validator)
|
||||||
|
{
|
||||||
|
if (empty($this->validators))
|
||||||
|
return false;
|
||||||
|
if ($validator instanceof Valitatable)
|
||||||
|
return isset($this->validators[spl_object_hash($validator)]);
|
||||||
|
else
|
||||||
|
return (boolean) array_filter(
|
||||||
|
$this->validators,
|
||||||
|
function($v) use ($validator) {
|
||||||
|
return (integer) ($v instanceof $validator);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValidators(array $validators, $prefix='')
|
||||||
|
{
|
||||||
|
foreach ($validators as $k => $v) {
|
||||||
|
if (is_object($v)) {
|
||||||
|
$this->addValidator($v);
|
||||||
|
continue;
|
||||||
|
} elseif (is_numeric($k)) {
|
||||||
|
$validatorName = $v;
|
||||||
|
$validatorArgs = array();
|
||||||
|
} else {
|
||||||
|
$validatorName = $k;
|
||||||
|
if (!empty($v) && !is_array($v))
|
||||||
|
throw new ComponentException(
|
||||||
|
sprintf(
|
||||||
|
'Arguments for array-specified validators must be an array, you provided %s',
|
||||||
|
$v
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$validatorArgs = empty($v) ? array() : $v;
|
||||||
|
}
|
||||||
|
if (!empty($prefix))
|
||||||
|
$validatorName = $prefix . '\\' . $validatorName;
|
||||||
|
$this->addValidator($validatorName, $validatorArgs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidators()
|
||||||
|
{
|
||||||
|
return $this->validators;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function iterateValidation($input)
|
||||||
|
{
|
||||||
|
$validators = $this->getValidators();
|
||||||
|
$exceptions = array();
|
||||||
|
foreach ($validators as $v)
|
||||||
|
try {
|
||||||
|
$v->validate($input);
|
||||||
|
} catch (InvalidException $e) {
|
||||||
|
$exceptions[] = $e;
|
||||||
|
}
|
||||||
|
return $exceptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate($input)
|
||||||
|
{
|
||||||
|
$exceptions = $this->iterateValidation($input);
|
||||||
|
if (!empty($exceptions))
|
||||||
|
throw new InvalidException($exceptions);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validateOne($input)
|
||||||
|
{
|
||||||
|
$validators = $this->getValidators();
|
||||||
|
$exceptions = $this->iterateValidation($input);
|
||||||
|
if (count($exceptions) === count($validators))
|
||||||
|
throw new InvalidException($exceptions);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isValid($input)
|
||||||
|
{
|
||||||
|
$validators = $this->getValidators();
|
||||||
|
return count($validators) === count(array_filter(
|
||||||
|
$validators,
|
||||||
|
function($v) use($input) {
|
||||||
|
return $v->isValid($input);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isOneValid($input)
|
||||||
|
{
|
||||||
|
return (boolean) array_filter(
|
||||||
|
$this->getValidators(),
|
||||||
|
function($v) use($input) {
|
||||||
|
return $v->isValid($input);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
namespace Respect\Validation\Date;
|
namespace Respect\Validation\Date;
|
||||||
|
|
||||||
use Respect\Validation\AbstractValidator;
|
use Respect\Validation\AbstractNode;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
abstract class AbstractDateValidator extends AbstractValidator
|
abstract class AbstractDateValidator extends AbstractNode
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $format = DateTime::RFC1036;
|
protected $format = DateTime::RFC1036;
|
||||||
|
|
|
@ -15,6 +15,8 @@ class Between extends AbstractDateValidator implements Validatable
|
||||||
self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.'
|
self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct($min, $max, $format=null)
|
public function __construct($min, $max, $format=null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -4,25 +4,47 @@ namespace Respect\Validation;
|
||||||
|
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
class Validator extends AbstractValidator implements Validatable
|
abstract class Validator
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $validators = array();
|
public static function validate($input, $validatorName)
|
||||||
protected $messages = array();
|
|
||||||
|
|
||||||
protected function appendValidator(Validatable $validator)
|
|
||||||
{
|
{
|
||||||
$this->validators[spl_object_hash($validator)] = $validator;
|
$arguments = func_get_args();
|
||||||
$this->messages = array_merge(
|
$arguments = array_slice($arguments, 2);
|
||||||
$this->messages, $validator->getMessages()
|
return static::buildValidator($validatorName, $arguments)->validate($input);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addValidator($validator, $arguments=array())
|
public static function is($input, $validatorName)
|
||||||
|
{
|
||||||
|
$arguments = func_get_args();
|
||||||
|
$arguments = array_slice($arguments, 2);
|
||||||
|
return static::buildValidator($validatorName, $arguments)->isValid($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function __callStatic($method, $arguments)
|
||||||
|
{
|
||||||
|
if (2 > count($arguments))
|
||||||
|
return false;
|
||||||
|
if (0 === strpos($method, 'valid')) {
|
||||||
|
$arguments[1] = substr($method, 5) . '\\' . $arguments[1];
|
||||||
|
$arguments[1] = trim($arguments[1], '\\');
|
||||||
|
return call_user_func_array(
|
||||||
|
array(get_called_class(), 'validate'), $arguments
|
||||||
|
);
|
||||||
|
} elseif (0 === strpos($method, 'is')) {
|
||||||
|
$arguments[1] = substr($method, 2) . '\\' . $arguments[1];
|
||||||
|
$arguments[1] = trim($arguments[1], '\\');
|
||||||
|
return call_user_func_array(
|
||||||
|
array(get_called_class(), 'is'), $arguments
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildValidator($validator, $arguments=array())
|
||||||
{
|
{
|
||||||
if ($validator instanceof Validatable) {
|
if ($validator instanceof Validatable) {
|
||||||
$this->appendValidator($validator);
|
return $validator;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (is_object($validator))
|
if (is_object($validator))
|
||||||
throw new ComponentException(
|
throw new ComponentException(
|
||||||
|
@ -32,6 +54,7 @@ class Validator extends AbstractValidator implements Validatable
|
||||||
$validatorFqn = explode('\\', get_called_class());
|
$validatorFqn = explode('\\', get_called_class());
|
||||||
array_pop($validatorFqn);
|
array_pop($validatorFqn);
|
||||||
$validatorFqn = array_merge($validatorFqn, explode('\\', $validator));
|
$validatorFqn = array_merge($validatorFqn, explode('\\', $validator));
|
||||||
|
$validatorFqn = array_map('ucfirst', $validatorFqn);
|
||||||
$validatorFqn = implode('\\', $validatorFqn);
|
$validatorFqn = implode('\\', $validatorFqn);
|
||||||
$validatorClass = new ReflectionClass($validatorFqn);
|
$validatorClass = new ReflectionClass($validatorFqn);
|
||||||
$implementedInterface = $validatorClass->implementsInterface(
|
$implementedInterface = $validatorClass->implementsInterface(
|
||||||
|
@ -49,117 +72,7 @@ class Validator extends AbstractValidator implements Validatable
|
||||||
} else {
|
} else {
|
||||||
$validatorInstance = new $validatorFqn;
|
$validatorInstance = new $validatorFqn;
|
||||||
}
|
}
|
||||||
$this->appendValidator($validatorInstance);
|
return $validatorInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasValidator($validator)
|
|
||||||
{
|
|
||||||
if (empty($this->validators))
|
|
||||||
return false;
|
|
||||||
if ($validator instanceof Valitatable)
|
|
||||||
return isset($this->validators[spl_object_hash($validator)]);
|
|
||||||
else
|
|
||||||
return (boolean) array_filter(
|
|
||||||
$this->validators,
|
|
||||||
function($v) use ($validator) {
|
|
||||||
return (integer) ($v instanceof $validator);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addValidators(array $validators, $prefix='')
|
|
||||||
{
|
|
||||||
foreach ($validators as $k => $v) {
|
|
||||||
if (is_object($v)) {
|
|
||||||
$this->addValidator($v);
|
|
||||||
continue;
|
|
||||||
} elseif (is_numeric($k)) {
|
|
||||||
$validatorName = $v;
|
|
||||||
$validatorArgs = array();
|
|
||||||
} else {
|
|
||||||
$validatorName = $k;
|
|
||||||
if (!empty($v) && !is_array($v))
|
|
||||||
throw new ComponentException(
|
|
||||||
sprintf(
|
|
||||||
'Arguments for array-specified validators must be an array, you provided %s',
|
|
||||||
$v
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$validatorArgs = empty($v) ? array() : $v;
|
|
||||||
}
|
|
||||||
if (!empty($prefix))
|
|
||||||
$validatorName = $prefix . '\\' . $validatorName;
|
|
||||||
$this->addValidator($validatorName, $validatorArgs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getValidators()
|
|
||||||
{
|
|
||||||
return $this->validators;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function iterateValidation($input)
|
|
||||||
{
|
|
||||||
$validators = $this->getValidators();
|
|
||||||
$exceptions = array();
|
|
||||||
foreach ($validators as $v)
|
|
||||||
try {
|
|
||||||
$v->validate($input);
|
|
||||||
} catch (InvalidException $e) {
|
|
||||||
$exceptions[] = $e;
|
|
||||||
}
|
|
||||||
return $exceptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validate($input)
|
|
||||||
{
|
|
||||||
$exceptions = $this->iterateValidation($input);
|
|
||||||
if (!empty($exceptions))
|
|
||||||
throw new InvalidException($exceptions);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validateOne($input)
|
|
||||||
{
|
|
||||||
$validators = $this->getValidators();
|
|
||||||
$exceptions = $this->iterateValidation($input);
|
|
||||||
if (count($exceptions) === count($validators))
|
|
||||||
throw new InvalidException($exceptions);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isValid($input)
|
|
||||||
{
|
|
||||||
$validators = $this->getValidators();
|
|
||||||
return count($validators) === count(array_filter(
|
|
||||||
$validators,
|
|
||||||
function($v) use($input) {
|
|
||||||
return $v->isValid($input);
|
|
||||||
}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isOneValid($input)
|
|
||||||
{
|
|
||||||
return (boolean) array_filter(
|
|
||||||
$this->getValidators(),
|
|
||||||
function($v) use($input) {
|
|
||||||
return $v->isValid($input);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*public function getMessages()
|
|
||||||
{
|
|
||||||
return $this->messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setMessages(array $messages)
|
|
||||||
{
|
|
||||||
if (count($this->messages) != count($messages))
|
|
||||||
throw new ComponentException(
|
|
||||||
'You must set exactly the same amount of messages currently present in the validator'
|
|
||||||
);
|
|
||||||
$this->messages = $messages;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
}
|
204
tests/library/Respect/Validation/CompositeValidatorTest.php
Normal file
204
tests/library/Respect/Validation/CompositeValidatorTest.php
Normal file
|
@ -0,0 +1,204 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Respect\Validation;
|
||||||
|
|
||||||
|
class CompositeValidatorTest extends ValidatorTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->object = new CompositeValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
unset($this->object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testAddExistentValidator($validator)
|
||||||
|
{
|
||||||
|
$this->object->addValidator($validator);
|
||||||
|
$this->assertContains($validator, $this->object->getValidators());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Respect\Validation\ComponentException
|
||||||
|
*/
|
||||||
|
public function testAddNonValidator()
|
||||||
|
{
|
||||||
|
$this->object->addValidator(new \stdClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Respect\Validation\ComponentException
|
||||||
|
*/
|
||||||
|
public function testAddNonValidator2()
|
||||||
|
{
|
||||||
|
if (!class_exists('Respect\Validation\Foo\Freak', false)) {
|
||||||
|
eval("
|
||||||
|
namespace Respect\Validation\Foo;
|
||||||
|
class Freak{}
|
||||||
|
");
|
||||||
|
}
|
||||||
|
$this->object->addValidator('Foo\Freak');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testAddValidatorsMessages($a, $b)
|
||||||
|
{
|
||||||
|
$messagesA = $a->getMessages();
|
||||||
|
$messagesB = $b->getMessages();
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$messagesObject = $this->object->getMessages();
|
||||||
|
foreach ($messagesA as $m) {
|
||||||
|
$this->assertContains($m, $messagesObject);
|
||||||
|
}
|
||||||
|
foreach ($messagesB as $m) {
|
||||||
|
$this->assertContains($m, $messagesObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBuildValidators()
|
||||||
|
{
|
||||||
|
$this->providerForMockImpossibleValidators();
|
||||||
|
$this->object->addValidators(array(
|
||||||
|
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => array(1, 2, 3)
|
||||||
|
));
|
||||||
|
$this->assertValidatorPresence($this->object, 'Bar', 'Baz', 'Bat');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Respect\Validation\ComponentException
|
||||||
|
*/
|
||||||
|
public function testBuildValidatorsInvalid()
|
||||||
|
{
|
||||||
|
$this->providerForMockImpossibleValidators();
|
||||||
|
$this->object->addValidators(array(
|
||||||
|
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => 'balkbal'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockValidators
|
||||||
|
*/
|
||||||
|
public function testValidateManyValid($a, $b, $c)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->assertTrue($this->object->validate('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockValidators
|
||||||
|
*/
|
||||||
|
public function testManyIsValid($a, $b, $c)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->assertTrue($this->object->isValid('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testManyIsInvalid($a, $b, $c)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->assertFalse($this->object->isValid('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testManyIsInvalid2($a, $b, $c)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->object->addValidator(
|
||||||
|
$this->buildMockValidator('Aids', array('Aids_1' => 'aesfg'))
|
||||||
|
);
|
||||||
|
$this->assertFalse($this->object->isValid('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testValidateAllInvalid($a, $b, $c)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
try {
|
||||||
|
$this->object->validate('any');
|
||||||
|
} catch (InvalidException $e) {
|
||||||
|
$this->assertEquals(3, count($e->getExceptions()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testValidateOneValid($invalidA, $invalidB, $invalidC)
|
||||||
|
{
|
||||||
|
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->object->addValidator($valid);
|
||||||
|
$this->assertTrue($this->object->validateOne('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testValidateOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
try {
|
||||||
|
$this->object->validateOne('any');
|
||||||
|
} catch (InvalidException $e) {
|
||||||
|
$this->assertEquals(3, count($e->getExceptions()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testIsValidOneValid($invalidA, $invalidB, $invalidC)
|
||||||
|
{
|
||||||
|
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->object->addValidator($valid);
|
||||||
|
$this->assertTrue($this->object->isOneValid('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testIsValidOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||||
|
{
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->assertFalse($this->object->isOneValid('any'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerForMockImpossibleValidators
|
||||||
|
*/
|
||||||
|
public function testSetMessages($a, $b)
|
||||||
|
{
|
||||||
|
$messagesA = $a->getMessages();
|
||||||
|
$messagesB = $b->getMessages();
|
||||||
|
$this->object->addValidators(func_get_args());
|
||||||
|
$this->object->setMessages(
|
||||||
|
array_map('strrev', $this->object->getMessages())
|
||||||
|
);
|
||||||
|
$messagesObject = $this->object->getMessages();
|
||||||
|
foreach ($messagesA as $m) {
|
||||||
|
$this->assertContains(strrev($m), $messagesObject);
|
||||||
|
}
|
||||||
|
foreach ($messagesB as $m) {
|
||||||
|
$this->assertContains(strrev($m), $messagesObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,200 +5,40 @@ namespace Respect\Validation;
|
||||||
class ValidatorTest extends ValidatorTestCase
|
class ValidatorTest extends ValidatorTestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $object;
|
public function testSimpleValidateCall()
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->object = new Validator;
|
$valid = Validator::valid('now', 'Date\Between', 'yesterday', 'tomorrow');
|
||||||
|
$this->assertTrue($valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown()
|
public function testDynamicValidateCall()
|
||||||
{
|
{
|
||||||
unset($this->object);
|
$valid = Validator::validDate('now', 'Between', 'yesterday', 'tomorrow');
|
||||||
|
$this->assertTrue($valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testSimpleIsValidCall()
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testAddExistentValidator($validator)
|
|
||||||
{
|
{
|
||||||
$this->object->addValidator($validator);
|
$valid = Validator::is('now', 'Date\Between', 'yesterday', 'tomorrow');
|
||||||
$this->assertContains($validator, $this->object->getValidators());
|
$this->assertTrue($valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testDynamicIsValidCall()
|
||||||
* @expectedException Respect\Validation\ComponentException
|
|
||||||
*/
|
|
||||||
public function testAddNonValidator()
|
|
||||||
{
|
{
|
||||||
$this->object->addValidator(new \stdClass);
|
$valid = Validator::isDate('now', 'Between', 'yesterday', 'tomorrow');
|
||||||
|
$this->assertTrue($valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNodeName()
|
||||||
* @expectedException Respect\Validation\ComponentException
|
|
||||||
*/
|
|
||||||
public function testAddNonValidator2()
|
|
||||||
{
|
{
|
||||||
if (!class_exists('Respect\Validation\Foo\Freak', false)) {
|
$valid = Validator::isDate('now', 'between', 'yesterday', 'tomorrow');
|
||||||
eval("
|
$this->assertTrue($valid);
|
||||||
namespace Respect\Validation\Foo;
|
|
||||||
class Freak{}
|
|
||||||
");
|
|
||||||
}
|
|
||||||
$this->object->addValidator('Foo\Freak');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNodeName2()
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testAddValidatorsMessages($a, $b)
|
|
||||||
{
|
{
|
||||||
$messagesA = $a->getMessages();
|
$valid = Validator::is('now', 'date\between', 'yesterday', 'tomorrow');
|
||||||
$messagesB = $b->getMessages();
|
$this->assertTrue($valid);
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$messagesObject = $this->object->getMessages();
|
|
||||||
foreach ($messagesA as $m) {
|
|
||||||
$this->assertContains($m, $messagesObject);
|
|
||||||
}
|
|
||||||
foreach ($messagesB as $m) {
|
|
||||||
$this->assertContains($m, $messagesObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBuildValidators()
|
|
||||||
{
|
|
||||||
$this->providerForMockImpossibleValidators();
|
|
||||||
$this->object->addValidators(array(
|
|
||||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => array(1, 2, 3)
|
|
||||||
));
|
|
||||||
$this->assertValidatorPresence($this->object, 'Bar', 'Baz', 'Bat');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException Respect\Validation\ComponentException
|
|
||||||
*/
|
|
||||||
public function testBuildValidatorsInvalid()
|
|
||||||
{
|
|
||||||
$this->providerForMockImpossibleValidators();
|
|
||||||
$this->object->addValidators(array(
|
|
||||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => 'balkbal'
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockValidators
|
|
||||||
*/
|
|
||||||
public function testValidateManyValid($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->assertTrue($this->object->validate('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockValidators
|
|
||||||
*/
|
|
||||||
public function testManyIsValid($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->assertTrue($this->object->isValid('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testManyIsInvalid($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->assertFalse($this->object->isValid('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testManyIsInvalid2($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->object->addValidator(
|
|
||||||
$this->buildMockValidator('Aids', array('Aids_1' => 'aesfg'))
|
|
||||||
);
|
|
||||||
$this->assertFalse($this->object->isValid('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testValidateAllInvalid($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
try {
|
|
||||||
$this->object->validate('any');
|
|
||||||
} catch (InvalidException $e) {
|
|
||||||
$this->assertEquals(3, count($e->getExceptions()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testValidateOneValid($invalidA, $invalidB, $invalidC)
|
|
||||||
{
|
|
||||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->object->addValidator($valid);
|
|
||||||
$this->assertTrue($this->object->validateOne('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testValidateOneAllRotten($invalidA, $invalidB, $invalidC)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
try {
|
|
||||||
$this->object->validateOne('any');
|
|
||||||
} catch (InvalidException $e) {
|
|
||||||
$this->assertEquals(3, count($e->getExceptions()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testIsValidOneValid($invalidA, $invalidB, $invalidC)
|
|
||||||
{
|
|
||||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->object->addValidator($valid);
|
|
||||||
$this->assertTrue($this->object->isOneValid('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testIsValidOneAllRotten($invalidA, $invalidB, $invalidC)
|
|
||||||
{
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->assertFalse($this->object->isOneValid('any'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForMockImpossibleValidators
|
|
||||||
*/
|
|
||||||
public function testSetMessages($a, $b)
|
|
||||||
{
|
|
||||||
$messagesA = $a->getMessages();
|
|
||||||
$messagesB = $b->getMessages();
|
|
||||||
$this->object->addValidators(func_get_args());
|
|
||||||
$this->object->setMessages(
|
|
||||||
array_map('strrev', $this->object->getMessages())
|
|
||||||
);
|
|
||||||
$messagesObject = $this->object->getMessages();
|
|
||||||
foreach ($messagesA as $m) {
|
|
||||||
$this->assertContains(strrev($m), $messagesObject);
|
|
||||||
}
|
|
||||||
foreach ($messagesB as $m) {
|
|
||||||
$this->assertContains(strrev($m), $messagesObject);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -73,13 +73,13 @@ abstract class ValidatorTestCase extends \PHPUnit_Framework_TestCase
|
||||||
public function providerForMockValidators()
|
public function providerForMockValidators()
|
||||||
{
|
{
|
||||||
$firstValidator = $this->buildMockValidator(
|
$firstValidator = $this->buildMockValidator(
|
||||||
'Bar', array('Bar_1' => 'fga', 'Bar_2' => 'dfgb'), false
|
'Bara', array('Bara_1' => 'fga', 'Bara_2' => 'dfgb'), false
|
||||||
);
|
);
|
||||||
$secondValidator = $this->buildMockValidator(
|
$secondValidator = $this->buildMockValidator(
|
||||||
'Baz', array('Baz_1' => 'gedg', 'Baz_2' => 'rihg49'), false
|
'Baza', array('Baza_1' => 'gedg', 'Baza_2' => 'rihg49'), false
|
||||||
);
|
);
|
||||||
$thirdValidator = $this->buildMockValidator(
|
$thirdValidator = $this->buildMockValidator(
|
||||||
'Bat', array('Bat_1' => 'dfdsgdgfgb'), false
|
'Bata', array('Bata_1' => 'dfdsgdgfgb'), false
|
||||||
);
|
);
|
||||||
return array(
|
return array(
|
||||||
array($firstValidator, $secondValidator, $thirdValidator),
|
array($firstValidator, $secondValidator, $thirdValidator),
|
||||||
|
|
Loading…
Reference in a new issue