mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Refactoring on some validators. Still need refactoring on many of them
This commit is contained in:
parent
248fbdcf4f
commit
0dceb11976
|
@ -11,9 +11,9 @@ class AbstractCompositeException extends ValidationException
|
|||
self::INVALID_SOME => '%2$d rules did not passed',
|
||||
);
|
||||
|
||||
public function chooseTemplate($input, $numFailed, $numRules)
|
||||
public function chooseTemplate($input, $numFailed, $numRequired, $numTotal)
|
||||
{
|
||||
if ($numFailed === $numRules)
|
||||
if ($numFailed === $numTotal)
|
||||
return static::INVALID_NONE;
|
||||
else
|
||||
return static::INVALID_SOME;
|
||||
|
|
|
@ -4,23 +4,9 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class BetweenException extends ValidationException
|
||||
{
|
||||
const INVALID_LESS= 'Between_1';
|
||||
const INVALID_MORE= 'Between_2';
|
||||
const INVALID_BOTH= 'Between_3';
|
||||
const INVALID_BETWEEN= 'Between_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_LESS => '"%s" is less than "%2$s"',
|
||||
self::INVALID_MORE => '"%s" is more than "%3$s"',
|
||||
self::INVALID_BOTH => '"%s" is less than "%s" and more than "%s"',
|
||||
self::INVALID_BETWEEN => '"%s" is out of bounds',
|
||||
);
|
||||
|
||||
public function chooseTemplate($input, $min, $max, $isMinValid, $isMaxValid)
|
||||
{
|
||||
if (!$isMinValid && !$isMaxValid)
|
||||
return self::INVALID_BOTH;
|
||||
if (!$isMinValid)
|
||||
return self::INVALID_LESS;
|
||||
if (!$isMaxValid)
|
||||
return self::INVALID_MORE;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class HasOptionalAttributeException extends ValidationException
|
||||
{
|
||||
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasOptionalAttribute_2';
|
||||
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasOptionalAttribute_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_HAS_ATTRIBUTE_RELATED => '"%2$s" is invalid',
|
||||
);
|
||||
|
|
12
library/Respect/Validation/Exceptions/MaxException.php
Normal file
12
library/Respect/Validation/Exceptions/MaxException.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class MaxException extends ValidationException
|
||||
{
|
||||
const INVALID_MAX= 'Max_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_MAX => '"%s" is greater than %s',
|
||||
);
|
||||
|
||||
}
|
12
library/Respect/Validation/Exceptions/MinException.php
Normal file
12
library/Respect/Validation/Exceptions/MinException.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class MinException extends ValidationException
|
||||
{
|
||||
const INVALID_MIN= 'Min_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_MIN => '"%s" is lower than %s',
|
||||
);
|
||||
|
||||
}
|
|
@ -2,12 +2,11 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Exception;
|
||||
use Respect\Validation\Validator;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Exception;
|
||||
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
{
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Exception;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
abstract class AbstractRule implements Validatable
|
||||
{
|
||||
|
@ -20,9 +20,9 @@ abstract class AbstractRule implements Validatable
|
|||
return $this->exception;
|
||||
}
|
||||
|
||||
public function setException()
|
||||
public function setException(Exception $e)
|
||||
{
|
||||
|
||||
$this->exception = $e;
|
||||
}
|
||||
|
||||
}
|
|
@ -10,22 +10,24 @@ class AllOf extends AbstractComposite
|
|||
public function validate($input)
|
||||
{
|
||||
$validators = $this->getRules();
|
||||
return count($validators) === count(array_filter(
|
||||
$validators,
|
||||
function($v) use($input) {
|
||||
return $v->validate($input);
|
||||
}
|
||||
));
|
||||
$passedValidators = array_filter(
|
||||
$validators,
|
||||
function($v) use($input) {
|
||||
return $v->validate($input);
|
||||
}
|
||||
);
|
||||
return count($validators) === count($passedValidators);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
$exceptions = $this->validateRules($input);
|
||||
$numRules = count($this->rules);
|
||||
if (!empty($exceptions))
|
||||
throw $this->getException() ? : AllOfException::create()
|
||||
->setRelated($exceptions)
|
||||
->configure(
|
||||
$input, count($exceptions), count($this->rules)
|
||||
$input, count($exceptions), $numRules, $numRules
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -6,39 +6,18 @@ use Respect\Validation\Rules\AbstractRule;
|
|||
use Respect\Validation\Exceptions\AlnumException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Alnum extends AbstractRule
|
||||
class Alnum extends Alpha
|
||||
{
|
||||
|
||||
protected $additionalChars = '';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
'Invalid list of additional characters to be loaded'
|
||||
);
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return is_string($input) && preg_match(
|
||||
"#^[a-zA-Z0-9]+$#",
|
||||
str_replace(str_split($this->additionalChars), '', $input)
|
||||
);
|
||||
}
|
||||
protected $stringFormat = '#^[a-zA-Z0-9]+$#';
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : AlnumException::create()
|
||||
->configure($input, $this->additionalChars);
|
||||
throw $this->getException() ? : AlnumException::create()
|
||||
->configure($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@ class Alpha extends AbstractRule
|
|||
{
|
||||
|
||||
protected $additionalChars = '';
|
||||
protected $stringFormat = '#^[a-zA-Z]+$#';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
|
@ -23,7 +24,7 @@ class Alpha extends AbstractRule
|
|||
public function validate($input)
|
||||
{
|
||||
return is_string($input) && preg_match(
|
||||
"#^[a-zA-Z]+$#",
|
||||
$this->stringFormat,
|
||||
str_replace(str_split($this->additionalChars), '', $input)
|
||||
);
|
||||
}
|
||||
|
@ -31,8 +32,8 @@ class Alpha extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : AlphaException::create()
|
||||
->configure($input, $this->additionalChars);
|
||||
throw $this->getException() ? : AlphaException::create()
|
||||
->configure($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,10 +19,13 @@ class AtLeast extends AbstractComposite
|
|||
{
|
||||
$validators = $this->getRules();
|
||||
$exceptions = $this->validateRules($input);
|
||||
if ($this->howMany > (count($validators) - count($exceptions)))
|
||||
$numRules = count($validators);
|
||||
$numExceptions = count($exceptions);
|
||||
if ($this->howMany > ($numRules - $numExceptions))
|
||||
throw $this->getException() ? : AtLeastException::create()
|
||||
->configure($input, count($exceptions), $this->howMany)
|
||||
->setRelated($exceptions);
|
||||
->configure(
|
||||
$input, count($exceptions), $this->howMany, $numRules
|
||||
)->setRelated($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,21 +2,16 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validator;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\BetweenException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
class Between extends AbstractRule
|
||||
class Between extends AllOf
|
||||
{
|
||||
|
||||
public function __construct($min=null, $max=null, Validatable $type=null)
|
||||
public function __construct($min=null, $max=null)
|
||||
{
|
||||
$this->min = $min;
|
||||
$this->max = $max;
|
||||
$this->type = $type;
|
||||
if (!is_null($min) && !is_null($max) && $min > $max)
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
|
@ -24,46 +19,21 @@ class Between extends AbstractRule
|
|||
$this->max
|
||||
)
|
||||
);
|
||||
if (is_null($type))
|
||||
return;
|
||||
try {
|
||||
$type->assert($min);
|
||||
$type->assert($max);
|
||||
} catch (ValidationException $e) {
|
||||
throw new ComponentException(
|
||||
$e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function validateMin($input)
|
||||
{
|
||||
return is_null($this->min) || $input >= $this->min;
|
||||
}
|
||||
|
||||
public function validateMax($input)
|
||||
{
|
||||
return is_null($this->max) || $input <= $this->max;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return (is_null($this->type) || $this->type->validate($input))
|
||||
&& $this->validateMin($input)
|
||||
&& $this->validateMax($input);
|
||||
if (!is_null($min))
|
||||
$this->addRule(new Min($min));
|
||||
if (!is_null($max))
|
||||
$this->addRule(new Max($max));
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!is_null($this->type))
|
||||
$this->type->assert($input);
|
||||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
try {
|
||||
parent::assert($input);
|
||||
} catch (ValidationException $e) {
|
||||
throw $this->getException() ? : BetweenException::create()
|
||||
->configure(
|
||||
$input, $this->min, $this->max, $validMin, $validMax
|
||||
);
|
||||
->addRelated($e)
|
||||
->configure($input);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,8 +28,8 @@ class Callback extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : CallbackException::create()
|
||||
->configure($input, $this->callback);
|
||||
throw $this->getException() ? : CallbackException::create()
|
||||
->configure($input, $this->callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,8 +36,8 @@ class Date extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : DateException::create()
|
||||
->configure($input, $this->format);
|
||||
throw $this->getException() ? : DateException::create()
|
||||
->configure($input, $this->format);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ class Float extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : FloatException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : FloatException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ class HasAttribute extends AllOf
|
|||
|
||||
public function __construct($attribute, Validatable $attributeValidator=null)
|
||||
{
|
||||
if (!is_string($attribute))
|
||||
if (!is_string($attribute) || empty($attribute))
|
||||
throw new ComponentException(
|
||||
'Invalid attribute name'
|
||||
);
|
||||
|
|
|
@ -2,14 +2,35 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\HasAttribute;
|
||||
use Respect\Validation\Exceptions\HasOptionalAttributeException;
|
||||
use Respect\Validation\Rules\AllOf;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Validatable;
|
||||
use \ReflectionProperty;
|
||||
use \ReflectionException;
|
||||
|
||||
class HasOptionalAttribute extends HasAttribute
|
||||
class HasOptionalAttribute extends AllOf
|
||||
{
|
||||
|
||||
public function __construct($attribute, Validatable $attributeValidator=null)
|
||||
{
|
||||
if (!is_string($attribute) || empty($attribute))
|
||||
throw new ComponentException(
|
||||
'Invalid attribute name'
|
||||
);
|
||||
$this->attribute = $attribute;
|
||||
if (!is_null($attributeValidator))
|
||||
$this->addRule($attributeValidator);
|
||||
}
|
||||
|
||||
protected function getAttributeValue($input)
|
||||
{
|
||||
$propertyMirror = new ReflectionProperty($input, $this->attribute);
|
||||
$propertyMirror->setAccessible(true);
|
||||
return $propertyMirror->getValue($input);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
try {
|
||||
|
@ -31,9 +52,15 @@ class HasOptionalAttribute extends HasAttribute
|
|||
return true;
|
||||
} catch (ValidationException $e) {
|
||||
throw $this->getException() ? : HasOptionalAttributeException::create()
|
||||
->configure($input, $this->attribute);
|
||||
->addRelated($e)
|
||||
->configure($input, $this->attribute, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
36
library/Respect/Validation/Rules/Max.php
Normal file
36
library/Respect/Validation/Rules/Max.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\MaxException;
|
||||
|
||||
class Max extends AbstractRule
|
||||
{
|
||||
|
||||
protected $max;
|
||||
|
||||
public function __construct($maxValue)
|
||||
{
|
||||
$this->max = $maxValue;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $input <= $this->max;
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : MaxException::create()
|
||||
->configure($input, $this->max);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
36
library/Respect/Validation/Rules/Min.php
Normal file
36
library/Respect/Validation/Rules/Min.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\MinException;
|
||||
|
||||
class Min extends AbstractRule
|
||||
{
|
||||
|
||||
protected $min;
|
||||
|
||||
public function __construct($minValue)
|
||||
{
|
||||
$this->min = $minValue;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $input >= $this->min;
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : MinException::create()
|
||||
->configure($input, $this->min);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -30,7 +30,7 @@ class NoneOf extends AbstractComposite
|
|||
$numExceptions = count($exceptions);
|
||||
if ($numRules !== $numExceptions)
|
||||
throw $this->getException() ? : NoneOfException::create()
|
||||
->configure($input, $numExceptions, $numRules)
|
||||
->configure($input, $numExceptions, 0, $numRules)
|
||||
->setRelated($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -16,9 +16,11 @@ class OneOf extends AbstractComposite
|
|||
{
|
||||
$validators = $this->getRules();
|
||||
$exceptions = $this->validateRules($input);
|
||||
if (count($exceptions) === count($validators))
|
||||
$numRules = count($validators);
|
||||
$numExceptions = count($exceptions);
|
||||
if ($numExceptions === $numRules)
|
||||
throw $this->getException() ? : OneOfException::create()
|
||||
->configure($input, count($exceptions), count($validators))
|
||||
->configure($input, $numExceptions, 1, $numRules)
|
||||
->setRelated($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -10,59 +10,37 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function providerValid()
|
||||
{
|
||||
$n = Validator::numeric();
|
||||
$d = Validator::date();
|
||||
return array(
|
||||
array(0, 1, 0, $n),
|
||||
array(0, 1, 1, $n),
|
||||
array(10, 20, 15, $n),
|
||||
array(10, 20, 20, $n),
|
||||
array(-10, 20, -5, $n),
|
||||
array(-10, 20, 0, $n),
|
||||
array(0, 1, 0),
|
||||
array(0, 1, 1),
|
||||
array(10, 20, 15),
|
||||
array(10, 20, 20),
|
||||
array(-10, 20, -5),
|
||||
array(-10, 20, 0),
|
||||
array(
|
||||
new DateTime('yesterday'),
|
||||
new DateTime('tomorrow'),
|
||||
new DateTime('now'),
|
||||
$d
|
||||
new DateTime('now')
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerInvalid()
|
||||
{
|
||||
$n = Validator::numeric();
|
||||
return array(
|
||||
array(0, 1, 2, $n),
|
||||
array(0, 1, -1, $n),
|
||||
array(10, 20, 999, $n),
|
||||
array(-10, 20, -11, $n),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerInvalidComponentParameters()
|
||||
{
|
||||
$n = Validator::numeric();
|
||||
return array(
|
||||
array('a', 1, 1, $n),
|
||||
array(0, ' ', 1, $n),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerInvalidInput()
|
||||
{
|
||||
$n = Validator::numeric();
|
||||
return array(
|
||||
array(10, 20, 'zas a', $n),
|
||||
array(-10, 20, ' ', $n)
|
||||
array(0, 1, 2),
|
||||
array(0, 1, -1),
|
||||
array(10, 20, 999),
|
||||
array(-10, 20, -11),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerValid
|
||||
*/
|
||||
public function testBetweenBounds($min, $max, $input, $type)
|
||||
public function testBetweenBounds($min, $max, $input)
|
||||
{
|
||||
$o = new Between($min, $max, $type);
|
||||
$o = new Between($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
$o = new Between($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
|
@ -72,29 +50,9 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerInvalid
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testNotBetweenBounds($min, $max, $input, $type)
|
||||
public function testNotBetweenBounds($min, $max, $input)
|
||||
{
|
||||
$o = new Between($min, $max, $type);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidComponentParameters
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testComponentParameters($min, $max, $input, $type)
|
||||
{
|
||||
$o = new Between($min, $max, $type);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidInput
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testInvalidInput($min, $max, $input, $type)
|
||||
{
|
||||
$o = new Between($min, $max, $type);
|
||||
$o = new Between($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
|
|
|
@ -32,11 +32,21 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAtrributeNames
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters()
|
||||
public function testInvalidParameters($attributeName)
|
||||
{
|
||||
$validator = new HasAttribute(array('invalid'));
|
||||
$validator = new HasAttribute($attributeName);
|
||||
}
|
||||
|
||||
public function providerForInvalidAtrributeNames()
|
||||
{
|
||||
return array(
|
||||
array(new \stdClass),
|
||||
array(123),
|
||||
array('')
|
||||
);
|
||||
}
|
||||
|
||||
public function testValidatorAttribute()
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class HasOptionalAttributeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testHasOptionalAttribute()
|
||||
{
|
||||
$validator = new HasOptionalAttribute('bar');
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
||||
public function testNotNull()
|
||||
{
|
||||
$validator = new HasOptionalAttribute('bar');
|
||||
$obj = new \stdClass;
|
||||
$obj->baraaaaa = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters()
|
||||
{
|
||||
$validator = new HasOptionalAttribute(array('invalid'));
|
||||
}
|
||||
|
||||
public function testValidatorAttribute()
|
||||
{
|
||||
$subValidator = new StringLength(1, 3);
|
||||
$validator = new HasOptionalAttribute('bar', $subValidator);
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class HasOptionalAttributeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testValidatorAttribute()
|
||||
{
|
||||
$subValidator = new StringLength(1, 10);
|
||||
$validator = new HasOptionalAttribute('bar', $subValidator);
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
||||
}
|
47
tests/library/Respect/Validation/Rules/MaxTest.php
Normal file
47
tests/library/Respect/Validation/Rules/MaxTest.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class MaxTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidMax
|
||||
*
|
||||
*/
|
||||
public function testMax($maxValue, $input)
|
||||
{
|
||||
$max = new Max($maxValue);
|
||||
$this->assertTrue($max->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidMax
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testNotMax($maxValue, $input)
|
||||
{
|
||||
$max = new Max($maxValue);
|
||||
$this->assertTrue($max->assert($input));
|
||||
}
|
||||
|
||||
public function providerForValidMax()
|
||||
{
|
||||
return array(
|
||||
array(200, 165.0),
|
||||
array(200, -200),
|
||||
array(200, 200),
|
||||
array(200, 0),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidMax()
|
||||
{
|
||||
return array(
|
||||
array(200, 300),
|
||||
array(200, 250),
|
||||
array(200, 1500),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
47
tests/library/Respect/Validation/Rules/MinTest.php
Normal file
47
tests/library/Respect/Validation/Rules/MinTest.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class MinTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidMin
|
||||
*
|
||||
*/
|
||||
public function testMin($minValue, $input)
|
||||
{
|
||||
$min = new Min($minValue);
|
||||
$this->assertTrue($min->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidMin
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testNotMin($minValue, $input)
|
||||
{
|
||||
$min = new Min($minValue);
|
||||
$this->assertTrue($min->assert($input));
|
||||
}
|
||||
|
||||
public function providerForValidMin()
|
||||
{
|
||||
return array(
|
||||
array(100, 165.0),
|
||||
array(-100, 200),
|
||||
array(200, 200),
|
||||
array(200, 300),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidMin()
|
||||
{
|
||||
return array(
|
||||
array(500, 300),
|
||||
array(0, -250),
|
||||
array(0, -50),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue