A lot of refactoring, tests on ValidationException, improvements on AllOf::validate

This commit is contained in:
Alexandre 2011-02-06 20:21:15 -02:00
parent e252c404ba
commit 8098321bcd
34 changed files with 330 additions and 200 deletions

View file

@ -26,7 +26,7 @@ class AbstractCompositeException extends AbstractRelatedException
public function getRelated($full=false)
{
if (!$full && 1 === count($this->related))
return $this->related[0]->getRelated();
return $this->related[0]->getRelated(false);
else
return parent::getRelated($full);
}

View file

@ -5,5 +5,12 @@ namespace Respect\Validation\Exceptions;
abstract class AbstractRelatedException extends ValidationException
{
public function getRelated($full=false)
{
if (!$full && 1 === count($this->related))
return $this->related[0]->getRelated(true);
else
return parent::getRelated($full);
}
}

View file

@ -6,8 +6,8 @@ class AttributeException extends AbstractRelatedException
{
public static $defaultTemplates = array(
'"%2$s" is not present',
'"%2$s" is invalid',
'%1$s is not present',
'%1$s is invalid',
);
public function chooseTemplate($input, $attributeName, $hasTheAttribute)

View file

@ -4,5 +4,13 @@ namespace Respect\Validation\Exceptions;
class CallException extends AbstractRelatedException
{
public function getMainMessage()
{
if (1 === count($this->related))
return $this->related[0]->getMainMessage();
else
return parent::getMainMessage();
}
}

View file

@ -7,6 +7,18 @@ class LengthException extends ValidationException
public static $defaultTemplates = array(
'"%s" length is not between %d and %d',
'"%s" length is lower than %2$d',
'"%s" length is greater than %3$d',
);
public function chooseTemplate($input, $min, $max)
{
if (is_null($min))
return 2;
elseif (is_null($max))
return 1;
else
return 0;
}
}

View file

@ -18,39 +18,53 @@ class ValidationException extends InvalidArgumentException
);
protected $related = array();
protected $params = array();
protected $name = '';
protected $id = '';
protected $template = '';
public static function create()
public function getParams()
{
$instance = new static;
return func_num_args() > 0 ? $instance : $instance->configure(func_get_args());
return $this->params;
}
public function configure()
public function setTemplate($template)
{
$this->template = $template;
}
public static function create($input=null)
{
$i = new static;
if (func_get_args() > 0)
return call_user_func_array(array($i, 'configure'), func_get_args());
else
return $i;
}
public function configure($input=null)
{
$this->message = $this->getMainMessage();
$this->params = func_get_args();
$this->stringifyParams();
$this->guessName();
$this->stringifyInput();
$this->guessId();
return $this;
}
protected function guessName()
protected function guessId()
{
if (!empty($this->name))
if (!empty($this->id))
return;
$name = end(explode('\\', get_called_class()));
$name = lcfirst(str_replace('Exception', '', $name));
$this->setName($name);
$id = end(explode('\\', get_called_class()));
$id = lcfirst(str_replace('Exception', '', $id));
$this->setId($id);
}
protected function stringifyParams()
protected function stringifyInput()
{
foreach ($this->params as &$param)
if (!is_object($param) || method_exists($param, '__toString'))
$param = (string) $param;
else
$param = get_class($param);
$param = &$this->params[0];
if (!is_object($param) || method_exists($param, '__toString'))
$param = (string) $param;
else
$param = get_class($param);
}
public function chooseTemplate()
@ -61,30 +75,31 @@ class ValidationException extends InvalidArgumentException
public function getFullMessage()
{
$message = array();
foreach ($this->iterate(false, self::ITERATE_TREE) as $m)
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function setName($name)
public function setId($id)
{
$this->name = $name;
$this->id = $id;
return $this;
}
public function getName()
public function getId()
{
return $this->name;
return $this->id;
}
public function getRelatedByName($name)
{
foreach ($this->iterate(true) as $e)
if ($e->getName() === $name)
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name)
return $e;
return false;
}
public function iterate($full=false, $mode=self::ITERATE_ALL)
public function getIterator($full=false, $mode=self::ITERATE_ALL)
{
$exceptionIterator = new ExceptionIterator($this, $full);
if ($mode == self::ITERATE_ALL)
@ -131,10 +146,12 @@ class ValidationException extends InvalidArgumentException
public function getTemplate()
{
if (!empty($this->template))
return $this->template;
$templateKey = call_user_func_array(
array($this, 'chooseTemplate'), $this->params
);
return static::$defaultTemplates[$templateKey];
return $this->template = static::$defaultTemplates[$templateKey];
}
public function __toString()

View file

@ -18,6 +18,13 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
$this->addRules(func_get_args());
}
public function setName($name)
{
foreach ($this->getRules() as $r)
$r->setName($name);
return parent::setName($name);
}
protected function appendRule(Validatable $validator)
{
$this->rules[spl_object_hash($validator)] = $validator;

View file

@ -26,17 +26,10 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
abstract protected function getReferenceValue($input);
protected function reportError($input, ValidationException $related=null)
public function reportError($input, array $relatedExceptions=array())
{
$e = $this->getException();
if ($e)
return $e;
$e = $this->createException();
if (!is_null($related))
$e->addRelated($related);
$e->configure($input, $this->reference, !is_null($related));
$e->setName($this->reference);
return $e;
return parent::reportError($input, $relatedExceptions, $this->reference,
!is_null($relatedExceptions))->setId($this->reference);
}
public function validate($input)
@ -59,7 +52,7 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
$this->getReferenceValue($input)
);
} catch (ValidationException $e) {
throw $this->reportError($input, $e);
throw $this->reportError($input, array($e));
} catch (ReflectionException $e) {
throw $this->reportError($input);
}

View file

@ -10,13 +10,24 @@ abstract class AbstractRule implements Validatable
{
protected $exception;
protected $id;
protected $name;
public function __construct()
{
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function __invoke($input)
{
return $this->validate($input);
@ -35,6 +46,11 @@ abstract class AbstractRule implements Validatable
return $this->exception;
}
public function hasException()
{
return!empty($this->exception);
}
public function setException(ValidationException $e)
{
$this->exception = $e;
@ -44,7 +60,7 @@ abstract class AbstractRule implements Validatable
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()->configure($input);
throw $this->reportError($input);
return true;
}
@ -53,4 +69,18 @@ abstract class AbstractRule implements Validatable
return $this->assert($input);
}
public function reportError($input, array $relatedExceptions=array())
{
if ($this->hasException())
return $this->getException();
$exception = $this->createException()->setRelated($relatedExceptions);
$parameters = array();
if (func_num_args() > 2)
$parameters = array_slice(func_get_args(), 2);
array_unshift($parameters, $this->getName() ? : $input);
call_user_func_array(array($exception, 'configure'), $parameters);
return $exception;
}
}

View file

@ -7,14 +7,10 @@ class AllOf extends AbstractComposite
public function validate($input)
{
$validators = $this->getRules();
$passedValidators = array_filter(
$validators,
function($v) use($input) {
return $v->validate($input);
}
);
return count($validators) === count($passedValidators);
foreach ($this->getRules() as $rule)
if (!$rule->validate())
return false;
return true;
}
public function assert($input)
@ -22,11 +18,8 @@ class AllOf extends AbstractComposite
$exceptions = $this->validateRules($input);
$numRules = count($this->rules);
if (!empty($exceptions))
throw $this->getException() ? : $this->createException()
->setRelated($exceptions)
->configure(
$input, count($exceptions), $numRules, $numRules
);
throw $this->reportError($input, $exceptions, count($exceptions),
$numRules, $numRules);
return true;
}

View file

@ -8,12 +8,4 @@ class Alnum extends Alpha
protected $additionalChars = '';
protected $stringFormat = '#^([a-zA-Z0-9]|\s)+$#';
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->additionalChars);
return true;
}
}

View file

@ -30,8 +30,7 @@ class Alpha extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->additionalChars);
throw $this->reportError($input, array(), $this->additionalChars);
return true;
}

View file

@ -20,10 +20,8 @@ class AtLeast extends AbstractComposite
$numRules = count($validators);
$numExceptions = count($exceptions);
if ($this->howMany > ($numRules - $numExceptions))
throw $this->getException() ? : $this->createException()
->configure(
$input, $numExceptions, $this->howMany, $numRules
)->setRelated($exceptions);
throw $this->reportError($input, $exceptions, $numExceptions,
$this->howMany, $numRules);
return true;
}
@ -60,9 +58,8 @@ class AtLeast extends AbstractComposite
if ($pass >= $this->howMany)
return true;
if (count($exceptions) > (count($validators) - $this->howMany))
throw $this->getException() ? : $this->createException()
->setRelated($exceptions)
->configure($input, count($exceptions), $this->howMany);
throw $this->reportError($input, $exceptions,
count($exceptions), $this->howMany);
}
return false;
}

View file

@ -17,6 +17,7 @@ class Attribute extends AbstractRelated
'Invalid attribute/property name'
);
parent::__construct($reference, $referenceValidator, $mandatory);
$this->setName($reference);
}
protected function hasReference($input)

View file

@ -23,9 +23,4 @@ class Callback extends AbstractRule
return call_user_func($this->callback, $input);
}
public function getId()
{
return spl_object_hash($this);
}
}

View file

@ -34,8 +34,7 @@ class Date extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->format);
throw $this->reportError($input, array(), $this->format);
return true;
}

View file

@ -33,18 +33,6 @@ class Each extends AbstractRule
return true;
}
protected function reportError($input, $item=null, array $keys=array(),
array $related = array())
{
$e = $this->getException();
if ($e)
return $e;
$e = $this->createException();
$e->setRelated($related);
$e->configure($input, $item, implode(', ', $keys), count($related));
return $e;
}
public function assert($input)
{
if (empty($input))
@ -72,7 +60,8 @@ class Each extends AbstractRule
}
}
if (!empty($exceptions))
throw $this->reportError($input, $item, $keys, $exceptions);
throw $this->reportError($input, array(), $item,
implode(', ', $keys), count($exceptions));
return true;
}

View file

@ -25,8 +25,8 @@ class Equals extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->param, $this->identical);
throw $this->reportError($input, array(), $this->param,
$this->identical);
}
}

View file

@ -30,10 +30,8 @@ class In extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure(
$input, print_r($this->options, true), $this->strict
);
throw $this->reportError($input, array(),
print_r($this->options, true), $this->strict);
return true;
}

View file

@ -20,8 +20,7 @@ class Instance extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->instance);
throw $this->reportError($input, array(), $this->instance);
return true;
}

View file

@ -16,6 +16,7 @@ class Key extends AbstractRelated
'Invalid array key name'
);
parent::__construct($reference, $referenceValidator, $mandatory);
$this->setName($reference);
}
protected function hasReference($input)

View file

@ -80,10 +80,7 @@ class Length extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure(
$input, $this->min, $this->max
);
throw $this->reportError($input, array(), $this->min, $this->max);
return true;
}

View file

@ -25,8 +25,8 @@ class Max extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->max, $this->inclusive);
throw $this->reportError($input, array(), $this->max,
$this->inclusive);
return true;
}

View file

@ -25,8 +25,8 @@ class Min extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->min, $this->inclusive);
throw $this->reportError($input, array(), $this->min,
$this->inclusive);
return true;
}

View file

@ -22,9 +22,8 @@ class NoneOf extends AbstractComposite
$numRules = count($this->getRules());
$numExceptions = count($exceptions);
if ($numRules !== $numExceptions)
throw $this->getException() ? : $this->createException()
->configure($input, $numExceptions, 0, $numRules)
->setRelated($exceptions);
throw $this->reportError($input, $exceptions, $numExceptions, 0,
$numRules);
return true;
}

View file

@ -12,9 +12,8 @@ class OneOf extends AbstractComposite
$numRules = count($validators);
$numExceptions = count($exceptions);
if ($numExceptions === $numRules)
throw $this->getException() ? : $this->createException()
->configure($input, $numExceptions, 1, $numRules)
->setRelated($exceptions);
throw $this->reportError($input, $exceptions, $numExceptions, 1,
$numRules);
return true;
}

View file

@ -20,8 +20,7 @@ class Regex extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->regex);
throw $this->reportError($input, array(), $this->regex);
return true;
}

View file

@ -43,8 +43,7 @@ class Sf extends AbstractRule
'',
$input
);
throw $this->getException() ? : $this->createException()
->configure($violation->getMessage());
throw $this->reportError($violation->getMessage());
}
return true;
}

View file

@ -34,8 +34,7 @@ class Zend extends AbstractRule
foreach ($this->zendValidator->getMessages() as $m) {
$exceptions[] = $this->createException()->configure($m);
}
throw $this->getException() ? : $this->createException()
->configure($exceptions);
throw $this->reportError($input, $exceptions);
}
return true;
}

View file

@ -18,4 +18,9 @@ interface Validatable
public function getException();
public function setException(ValidationException $e);
}
public function setName($name);
public function getName();
}

View file

@ -10,8 +10,8 @@
<email>alexandre@gaigalas.net</email>
<active>yes</active>
</lead>
<date>2011-02-05</date>
<time>15:32:14</time>
<date>2011-02-06</date>
<time>20:21:06</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -26,17 +26,17 @@ First Version
</notes>
<contents>
<dir baseinstalldir="Respect/Validation" name="/">
<file baseinstalldir="Respect/Validation" md5sum="c88274d92f326857d99a5d9909ed4553" name="Exceptions/AbstractCompositeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a800215b4b2db24cac121b96ac25cbb4" name="Exceptions/AbstractRelatedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="138f3a19c04c238bc1d041036991b50e" name="Exceptions/AbstractCompositeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0224a04fb381c031061f45f181210d2a" name="Exceptions/AbstractRelatedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="565d57a097cf70774e4ea33fc99393f0" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8f570018d80559edbca74fd0e77507cc" name="Exceptions/AlnumException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e034e7e5948bb2391e9ba07c6d2bf1fe" name="Exceptions/AlphaException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="87e039b3a5b7bee95571cff6163616db" name="Exceptions/ArrException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="de256f654655018225790cf7417a49b8" name="Exceptions/AtLeastException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a376b118a2d1a27bb762016036a9ea78" name="Exceptions/AttributeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a4897e8b1b989d22bcda92369fc92a48" name="Exceptions/AttributeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b33e57b6b4eca4c81145d97f7f736423" name="Exceptions/BetweenException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="44b39dc2ff0077e205661e7939745cb4" name="Exceptions/CallbackException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0d1b8a28e719230c2084a1f69a6beec4" name="Exceptions/CallException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="85cfc4028d19470a3ea4a6c2d619c1ad" name="Exceptions/CallException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="dccb83d6730328467bd0935caea42b04" name="Exceptions/ComponentException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="07a9f2f27a26a73925aa373b8c04a3f8" name="Exceptions/DateException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7119dcecdceb9ed9ac3bdf334516c4a9" name="Exceptions/DigitsException.php" role="php" />
@ -49,7 +49,7 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="99910e278b930ca3e555ebc9623883ad" name="Exceptions/IntException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9eec15dce42dc32808f128240866ae6b" name="Exceptions/IpException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ed0fe314dd1feced68414b2f3c9636d1" name="Exceptions/KeyException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b249795559549714b11bb52bdb62196b" name="Exceptions/LengthException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="74275cc97638b53c9a25d4facbc293e8" name="Exceptions/LengthException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4dad9457e194a797e9232933eaab1490" name="Exceptions/MaxException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="edffffaed758b8eed5b611ed1720cb9e" name="Exceptions/MinException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9372ddee38174cd9213f19c27f1a6d3a" name="Exceptions/MostOfException.php" role="php" />
@ -65,50 +65,50 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="5f88f980867180b6895accf2bbf56304" name="Exceptions/RegexException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9a62c0bb8723bed9b9f7cb9f5b10d1d2" name="Exceptions/SfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="022f47b77de6317e6fff032a75c4cf46" name="Exceptions/StringException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1c3bb4fc09d7be6bf1b56974defef6f7" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="138055204024bcdea0f54a28177f01d3" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ea6baf29fd0d22ae11ffc24fc8b63426" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c27317c9e88b11f1b68eabe978575ec4" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ca00e7439e1d2cddda1188c446c9827c" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cfb62c0837f2109310a38142bf0261d3" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="77a90010ca6ea9c687426b1b870770ec" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="698c5305baff4a40d732a0f2f9f9066c" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="dc721027b68ebd939a50147ddf4e7ee0" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4d1cdd1c36712cbb63d389609ab871ff" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="abaca0c4f1e41e7a9a75b62bb68664b4" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="595116795bb82d76dfb248af823d33a9" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9aec5cb0ecbd63e2df4d3c6fc1544f14" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a431d0ef3c154cb2c5fe7a07bbd56acf" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="53e41c07b79bb2183b64fb30818c8ec5" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ea3e92cc6b0f2cc18eb66bf2506ecd88" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="46b15803d4f90d1d7b1462b1aa2c9951" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="90a42cc5da8ea72e1ca54137482c432c" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1f6761b039050df857634cdf72794789" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="908141afa67173d6df644cf484ca1c19" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0e5a580ff3090d106df1e02c5882ef57" name="Rules/Between.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d51ab5f4b302720d988d39f84e145851" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8683db92a7d9f93296ee07451527d02b" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ac564489c5972033525c5994cea1c241" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b6837ae1fd632cedab0a460fd0ed8ea2" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8485d68277dc87c19dfc61327782fbd9" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f6cee7a9c23415c07318af3dbcdba94b" name="Rules/Digits.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fccc7892171485270ca31e50c2cf98ed" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f6d3f7db9ed0898460e1e330ffb4e965" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f9e382e06b82ef2db9ad6d086e8be650" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9045e8059dffe685c1f4876203990e6f" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3374c2cf568efc7326468193878792bd" name="Rules/Float.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cf8c4770ebb3021bd1737b677c7270c9" name="Rules/Hexa.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0ed55a1ab9c8929c2dbe0cc8b35d2dab" name="Rules/In.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e4fa200037c4bf6880bf0f5766218171" name="Rules/Instance.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2c93ab42a84ed9a1bec7d256567e8a1b" name="Rules/In.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e64f06f3b0c2078c87655dc740846293" name="Rules/Instance.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fbeec7c032013e067e72c9704ba8537f" name="Rules/Int.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3a083becb83c264973b097207d6e4c7a" name="Rules/Ip.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9ab0c98ff2b5c373ab408f72a168bb0e" name="Rules/Key.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="da3b9154eff6a4d01df566d9fdcfc837" name="Rules/Length.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="72e9a94595f32bf43ea19a53609f4d3a" name="Rules/Max.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2b92961085bcea290ed15dab67e0064b" name="Rules/Min.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="28a39db3f5c31b649a840bc2889cc9ff" name="Rules/Key.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="627c89ec77e5611833b1da875d5b669e" name="Rules/Length.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="00c17c8f4d3562a3202674b61a4f9a91" name="Rules/Max.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="82b066b058fcf54b48cd9ad363f60e03" name="Rules/Min.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1e7ba06c790b75a168a98e3972d38aa6" name="Rules/MostOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ecf7ebf9af5c2c963dbde65efc5cfad2" name="Rules/Negative.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4a301f41432c8f6c15074733082aff66" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3475bf1fa01eaf9244eaaacb1004bd1e" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0aaf71cbd1de8bcd583e560fc80042f5" name="Rules/NotEmpty.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a8a1d7a823cd22f3446b8db96f30f2fc" name="Rules/NoWhitespace.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c5d8a9ee171e6acf3bdab26ef08350c2" name="Rules/NullValue.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="34ed73b15119f9fa8e54d61009fcf0a4" name="Rules/Numeric.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8a6462204bffbb3eead42a1b92b7d17e" name="Rules/Object.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1e26108f3e6bfde46239aa1726d4775b" name="Rules/OneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="05dd2d2108851f2ba1237ffb9be51ec7" name="Rules/OneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2b9e4d588eefda35f48b2360dcf5b36d" name="Rules/Positive.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d3f2e2142375c8e944a3abe9b2e22674" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="668cb6704593d7a9170b71d5715b609d" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="38c92ea81d8bce37761026dcb0876032" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="15a7336192a82ef5ee5e4ddcdeafefc1" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d3cf890b95664760156f902207242250" name="Rules/String.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d4b83a1935ed3deea7484f6acba38a2c" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f95e0018ccc2ea97ece90c41f7da90d3" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="28957781dcb20a937fc8643672e848c6" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b66f9cef94652f5600bb009acf8f15cb" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="34b7d721ae2edac1fbc10c02bc936028" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2d08eae5e1809dda0b843ee4d8c37828" name="Validator.php" role="php" />
</dir>
</contents>
@ -133,7 +133,7 @@ First Version
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2011-02-05</date>
<date>2011-02-06</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
<notes>
First Version

View file

@ -0,0 +1,62 @@
<?php
namespace Respect\Validation\Exceptions;
class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$e = ValidationException::create(new \stdClass, 'bar', array(),
new \stdClass);
$this->assertEquals(array('stdClass', 'bar', array(), new \stdClass),
$e->getParams());
$this->assertEquals('validation', $e->getId());
}
public function testConfigure()
{
$e = new ValidationException;
$e->configure(new \stdClass, 'bar', array(), new \stdClass);
$this->assertEquals(
array('stdClass', 'bar', array(), new \stdClass), $e->getParams()
);
$this->assertEquals('validation', $e->getId());
}
public function testGetRelatedByName()
{
$a = new ValidationException;
$a1 = new ValidationException;
$a2 = new ValidationException;
$a11 = new ValidationException;
$a21 = new ValidationException;
$a->setId('foo');
$a1->setId('bar1');
$a2->setId('bar2');
$a11->setId('baz1');
$a21->setId('baz2');
$a1->addRelated($a11);
$a2->addRelated($a21);
$a->addRelated($a1);
$a->addRelated($a2);
$this->assertEquals($a1, $a->findRelated('bar1'));
$this->assertEquals($a11, $a->findRelated('bar1', 'baz1'));
$this->assertEquals($a2, $a->findRelated('bar2'));
$this->assertEquals($a21, $a->findRelated('bar2', 'baz2'));
$this->assertEquals(false, $a->findRelated('zzz', 'xxx'));
$this->assertEquals(false, $a->findRelated('bar1', 'xx'));
}
public function testGetMainMessage()
{
$e = ValidationException::create('foo');
$e->setTemplate('Validation baz %s');
$this->assertEquals('Validation baz foo', $e->getMainMessage());
$this->assertEquals($e->getMainMessage(), (string) $e);
}
}

View file

@ -2,170 +2,171 @@
namespace Respect\Validation;
use Respect\Validation\Validator as v;
class ValidatorTest extends \PHPUnit_Framework_TestCase
{
public function testAlnum()
{
Validator::alnum()->assert('abc 123');
Validator::alnum('_')->assert('a_bc _123');
v::alnum()->assert('abc 123');
v::alnum('_')->assert('a_bc _123');
}
public function testAlpha()
{
Validator::alpha()->assert('ab c');
Validator::alpha('.')->assert('a. b.c');
v::alpha()->assert('ab c');
v::alpha('.')->assert('a. b.c');
}
public function testArr()
{
Validator::arr()->assert(array());
v::arr()->assert(array());
}
public function testAttribute()
{
Validator::attribute("foo", Validator::string())->assert((object) array("foo" => "bar"));
v::attribute("foo", v::string())->assert((object) array("foo" => "bar"));
}
public function testBetween()
{
Validator::between(5, 15)->assert(10);
Validator::between('a', 'f')->assert('b');
v::between(5, 15)->assert(10);
v::between('a', 'f')->assert('b');
}
public function testCall()
{
Validator::call('implode', Validator::int())->assert(array(1, 2, 3, 4));
v::call('implode', v::int())->assert(array(1, 2, 3, 4));
}
public function testCallback()
{
Validator::callback('is_string')->assert('something');
v::callback('is_string')->assert('something');
}
public function testDate()
{
Validator::date('Y-m-d')->assert('2010-10-10');
Validator::date()->assert('Jan 10 2008');
v::date('Y-m-d')->assert('2010-10-10');
v::date()->assert('Jan 10 2008');
}
public function testDigits()
{
Validator::digits()->assert('02384');
v::digits()->assert('02384');
}
public function testEach()
{
Validator::each(Validator::hexa())->assert(array('AF', 'D1', '09'));
v::each(v::hexa())->assert(array('AF', 'D1', '09'));
}
public function testEquals()
{
Validator::equals('foobar')->assert('foobar');
v::equals('foobar')->assert('foobar');
}
public function testFloat()
{
Validator::float()->assert(1.5);
v::float()->assert(1.5);
}
public function testHexa()
{
Validator::hexa()->assert('FAFAF');
v::hexa()->assert('FAFAF');
}
public function testIn()
{
Validator::in(array(1, 1, 2, 3, 5, 8))->assert(5);
v::in(array(1, 1, 2, 3, 5, 8))->assert(5);
}
public function testInstance()
{
Validator::instance('\stdClass')->assert(new \stdClass);
v::instance('\stdClass')->assert(new \stdClass);
}
public function testInt()
{
Validator::int()->assert(1548);
v::int()->assert(1548);
}
public function testIp()
{
Validator::ip()->assert('200.226.220.222');
v::ip()->assert('200.226.220.222');
}
public function testLength()
{
Validator::length(5, 10)->assert('foobar');
Validator::length(5, 10)->assert(array(1, 2, 3, 4, 5));
v::length(5, 10)->assert('foobar');
v::length(5, 10)->assert(array(1, 2, 3, 4, 5));
}
public function testMax()
{
Validator::max(5)->assert(3);
v::max(5)->assert(3);
}
public function testMin()
{
Validator::min(5)->assert(7);
v::min(5)->assert(7);
}
public function testNegative()
{
Validator::negative()->assert(-5);
v::negative()->assert(-5);
}
public function testPositive()
{
Validator::positive()->assert(3);
v::positive()->assert(3);
}
public function testNoWhitespace()
{
Validator::noWhitespace()->assert('abc');
v::noWhitespace()->assert('abc');
}
public function testNotEmpty()
{
Validator::notEmpty()->assert('aaa');
v::notEmpty()->assert('aaa');
}
public function testNullValue()
{
Validator::nullValue()->assert(null);
v::nullValue()->assert(null);
}
public function testNumeric()
{
Validator::numeric()->assert(1.56e-5);
v::numeric()->assert(1.56e-5);
}
public function testObject()
{
Validator::object()->assert(new \DateTime());
v::object()->assert(new \DateTime());
}
public function testRegex()
{
Validator::regex('[a-f]+')->assert('abcdef');
v::regex('^[a-f]+$')->assert('abcdef');
}
public function testString()
{
Validator::string()->assert('Hello World');
v::string()->assert('Hello World');
}
public function testAllOf()
{
Validator::allOf(
Validator::string(), //any string
Validator::length(5, 20), //between 5 and 20 chars
Validator::noWhitespace() //no whitespace allowed
v::allOf(
v::string(), //any string v::length(5, 20), //between 5 and 20 chars
v::noWhitespace() //no whitespace allowed
)->assert('alganet');
//same as
Validator::string()
v::string()
->length(5, 20)
->noWhitespace()
->assert('alganet');
@ -173,14 +174,47 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
public function testOneOf()
{
$v = Validator::oneOf(
Validator::int()->positive(), //positive integer or;
Validator::float()->negative(), //negative float or;
Validator::nullValue() //null
$v = v::oneOf(
v::int()->positive(), //positive integer or;
v::float()->negative(), //negative float or;
v::nullValue() //null
);
$v->assert(null);
$v->assert(12);
$v->assert(-1.1);
}
public function testGmailSignInValidation()
{
$stringMax256 = v::string()->length(1, 256);
$alnumDot = v::alnum('.');
$stringMin8 = v::string()->length(8, null);
$v = v::allOf(
v::attribute('first_name', $stringMax256)->setName('First Name'),
v::attribute('last_name', $stringMax256)->setName('Last Name'),
v::attribute('desired_login', $alnumDot)->setName('Desired Login'),
v::attribute('password', $stringMin8)->setName('Password'),
v::attribute('password_confirmation', $stringMin8)->setName('Password Confirmation'),
v::attribute('stay_signedin', v::notEmpty())->setName('Stay signed in'),
v::attribute('enable_webhistory', v::notEmpty())->setName('Enabled Web History'),
v::attribute('security_question', $stringMax256)->setName('Security Question')
);
try {
$v->assert(
(object) array(
'first_name' => null,
'last_name' => null,
'desired_login' => null,
'password' => null,
'password_confirmation' => null,
'stay_signedin' => null,
'enable_webhistory' => null,
'security_question' => null,
)
);
} catch (Exceptions\ValidationException $e) {
$e->getFullMessage();
}
}
}

View file

@ -1,6 +1,6 @@
<phpunit backupGlobals="false"
backupStaticAttributes="true"
backupStaticAttributes="false"
bootstrap="bootstrap.php"
colors="false"
convertErrorsToExceptions="true"