Improved message compositing and iteration. Cleanup.

This commit is contained in:
Alexandre 2011-02-18 01:08:46 -02:00
parent 895ce93ed1
commit 28603d74a3
19 changed files with 154 additions and 334 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ library/Symfony/
.buildpath
.project
.settings/
nbproject

View file

@ -19,8 +19,10 @@ class ExceptionIterator extends RecursiveArrayIterator
public function hasChildren()
{
if (!$this->current() instanceof AbstractCompositeException) return false;
return (bool) $this->current()->getRelated($this->fullRelated);
if (!$this->current() instanceof AbstractCompositeException)
return false;
else
return (boolean) $this->current()->getRelated($this->fullRelated);
}
public function getChildren()

View file

@ -1,16 +1,18 @@
<?php
namespace Respect\Validation\Exceptions;
use RecursiveIteratorIterator;
use RecursiveTreeIterator;
use Respect\Validation\ExceptionIterator;
class AbstractCompositeException extends ValidationException
{
const ITERATE_TREE = 1;
const ITERATE_ALL = 2;
const NONE = 0;
const SOME = 1;
protected $related = array();
public static $defaultTemplates = array(
self::NONE => 'All of the required rules must pass for {{name}}',
self::SOME => 'These rules must pass for {{name}}',
@ -23,12 +25,21 @@ class AbstractCompositeException extends ValidationException
return $numRules === $numFailed ? static::NONE : static::SOME;
}
public function getFullMessage()
{
$message = array();
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function setContext($context)
{
parent::setContext($context);
foreach ($this->related as $r)
$r->setContext($context);
}
public function getIterator($full=false, $mode=self::ITERATE_ALL)
{
$exceptionIterator = new ExceptionIterator($this, $full);
@ -38,8 +49,6 @@ class AbstractCompositeException extends ValidationException
return new RecursiveTreeIterator($exceptionIterator);
}
public function findRelated()
{
$target = $this;
@ -49,29 +58,31 @@ class AbstractCompositeException extends ValidationException
return $target;
}
public function getRelatedByName($name)
{
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name)
foreach ($this->getIterator(true) as $e) {
if ($e->getId() === $name || $e->getName() === $name)
return $e;
}
return false;
}
public function addRelated(ValidationException $related)
{
if ($related instanceof static)
$related->setName($this->getName());
$this->related[] = $related;
return $this;
}
public function getMainMessage()
public function setName($name)
{
if (1 === count($this->related))
return $this->related[0]
->setName($this->getName())
->getMainMessage();
else
return parent::getMainMessage();
foreach ($this->related as $r)
if ($r instanceof static)
$r->setName($name);
return parent::setName($name);
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related)
@ -81,13 +92,22 @@ class AbstractCompositeException extends ValidationException
public function getRelated($full=false)
{
if (!$full && 1 === count($this->related))
if ($this->related[0] instanceof AbstractCompositeException)
return $this->related[0]->getRelated(false);
else
return array();
else
if ($full || 1 !== count($this->related))
return $this->related;
elseif ($this->related[0] instanceof self)
return $this->related[0]->getRelated();
else
return array();
}
public function getMainMessage()
{
if (1 === count($this->related) &&
($this->related[0] instanceof static
|| !$this->related[0] instanceof self ))
return $this->related[0]->getMainMessage();
else
return parent::getMainMessage();
}
}

View file

@ -4,6 +4,12 @@ namespace Respect\Validation\Exceptions;
class AbstractRelatedException extends AbstractCompositeException
{
public function chooseTemplate()
{
return 0;
}
public function getMainMessage()
{
$vars = $this->getParams();
@ -11,14 +17,9 @@ class AbstractRelatedException extends AbstractCompositeException
return static::format($this->getTemplate(), $vars);
}
public function getRelated($full=false)
public function getRelated()
{
return $this->related;
}
public function chooseTemplate()
{
return 0;
}
}

View file

@ -10,16 +10,13 @@ use Respect\Validation\Validatable;
class ValidationException extends InvalidArgumentException
{
const STANDARD = 0;
const ITERATE_TREE = 1;
const ITERATE_ALL = 2;
public static $defaultTemplates = array(
self::STANDARD => 'Data validation failed for %s'
);
protected $context = null;
protected $id = '';
protected $name = 'validation';
protected $id = 'validation';
protected $name = '';
protected $template = '';
protected $validator = null;
public static function format($template, array $vars=array())
{
@ -36,16 +33,21 @@ class ValidationException extends InvalidArgumentException
if (is_string($value))
return $value;
elseif (is_object($value))
if (method_exists($value, '__toString'))
return (string) $value;
elseif ($value instanceof DateTime)
return $value->format('Y-m-d H:i:s');
else
return "Object of class " . get_class($value);
return static::stringifyObject($value);
else
return (string) $value;
}
public static function stringifyObject($value)
{
if (method_exists($value, '__toString'))
return (string) $value;
elseif ($value instanceof DateTime)
return $value->format('Y-m-d H:i:s');
else
return "Object of class " . get_class($value);
}
public function __toString()
{
return $this->getMainMessage();
@ -65,15 +67,6 @@ class ValidationException extends InvalidArgumentException
return $this;
}
public function getFullMessage()
{
$message = array();
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function getName()
{
return $this->name;
@ -83,6 +76,7 @@ class ValidationException extends InvalidArgumentException
{
return $this->id;
}
public function getMainMessage()
{
$vars = $this->getParams();
@ -105,7 +99,6 @@ class ValidationException extends InvalidArgumentException
return $this->params;
}
public function getTemplate()
{
if (!empty($this->template))
@ -138,10 +131,9 @@ class ValidationException extends InvalidArgumentException
public function setParams(array $params)
{
$this->params = array_map(array(get_called_class(), 'stringify'),
$params);
$params);
}
public function setTemplate($template)
{
$this->template = $template;

View file

@ -3,6 +3,7 @@
namespace Respect\Validation\Rules;
use Exception;
use Respect\Validation\Exceptions\AbstractCompositeException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
@ -21,24 +22,24 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
public function addRule($validator, $arguments=array())
{
if (!$validator instanceof Validatable)
$validator = Validator::buildRule($validator, $arguments);
$this->appendRule($validator);
$this->appendRule(Validator::buildRule($validator, $arguments));
else
$this->appendRule($validator);
}
public function addRules(array $validators)
{
foreach ($validators as $key => $spec) {
foreach ($validators as $key => $spec)
if ($spec instanceof Validatable)
$this->appendRule($spec);
if (is_numeric($key) && is_array($spec))
elseif (is_numeric($key) && is_array($spec))
$this->addRules($spec);
elseif (is_array($spec))
$this->addRule($key, $spec);
else
$this->addRule($spec);
}
}
public function getRules()
{
return $this->rules;
@ -48,14 +49,13 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
{
if (empty($this->rules))
return false;
if ($validator instanceof Valitatable)
elseif ($validator instanceof Valitatable)
return isset($this->rules[spl_object_hash($validator)]);
else
return (boolean) array_filter(
$this->rules,
function($v) use ($validator) {
return (integer) ($v instanceof $validator);
});
foreach ($this->rules as $rule)
if ($rule instanceof $validator)
return true;
return false;
}
protected function appendRule(Validatable $validator)

View file

@ -2,9 +2,8 @@
namespace Respect\Validation\Rules;
use ReflectionProperty;
use ReflectionException;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\AbstractCompositeException;
use Respect\Validation\Exceptions\ValidationException;
abstract class AbstractRelated extends AbstractRule implements Validatable
@ -12,63 +11,55 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
public $mandatory = true;
public $reference = '';
public $referenceValidator;
public $validator;
abstract public function hasReference($input);
abstract public function getReferenceValue($input);
public function __construct($reference, Validatable $validator=null,
$mandatory=true)
public function __construct($reference, Validatable $validator=null, $mandatory=true)
{
$this->reference = $reference;
$this->referenceValidator = $validator;
$this->validator = $validator;
$this->mandatory = $mandatory;
}
public function assert($input)
{
if ($this->mandatory && !$this->hasReference($input))
throw $this->reportError($input);
try {
if (!is_null($this->referenceValidator))
$this->referenceValidator->assert(
$this->getReferenceValue($input)
);
} catch (ValidationException $e) {
throw $this->reportError(
$input, array('hasReference' => true)
)->addRelated($e);
} catch (ReflectionException $e) {
if ($this->mandatory)
throw $this->reportError(
$input, array('hasReference' => false)
);
}
return true;
if (!$this->verifyMandatory($input))
throw $this->reportError($input, array('hasReference' => false));
elseif (!$this->validator)
return true;
else
try {
return $this->validator->assert($this->getReferenceValue($input));
} catch (ValidationException $e) {
throw $this
->reportError($this->reference, array('hasReference' => true))
->setName($this->reference)
->addRelated($e);
}
}
public function check($input)
{
if ($this->mandatory && !$this->hasReference($input))
throw $this->reportError(
$input, array('hasReference' => false)
);
if (!is_null($this->referenceValidator))
$this->referenceValidator->check(
$this->getReferenceValue($input)
);
return true;
if (!$this->verifyMandatory($input))
throw $this->reportError($input, array('hasReference' => false));
else
return $this->validator->check($this->getReferenceValue($input));
}
public function validate($input)
{
if ($this->mandatory && !$this->hasReference($input))
if (!$this->verifyMandatory($input))
return false;
if (!is_null($this->referenceValidator))
return $this->referenceValidator
->validate($this->getReferenceValue($input));
return true;
else
return $this->validator->validate($this->getReferenceValue($input));
}
protected function verifyMandatory($input)
{
return!$this->mandatory || $this->hasReference($input);
}
}

View file

@ -24,9 +24,10 @@ abstract class AbstractRule implements Validatable
public function assert($input)
{
if (!$this->validate($input))
if ($this->validate($input))
return true;
else
throw $this->reportError($input);
return true;
}
public function check($input)
@ -34,35 +35,17 @@ abstract class AbstractRule implements Validatable
return $this->assert($input);
}
public function createException()
{
$currentFQN = get_called_class();
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
$exceptionFQN .= 'Exception';
return new $exceptionFQN;
}
public function getException()
{
return $this->exception;
}
public function getName()
{
return $this->name;
}
public function hasException()
{
return!empty($this->exception);
}
public function reportError($input, array $extraParams=array())
{
if ($this->hasException())
return $this->getException();
$exception = $this->createException();
$currentFQN = get_called_class();
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
$exceptionFQN .= 'Exception';
$exception = new $exceptionFQN;
$input = ValidationException::stringify($input);
$name = $this->getName() ? : "\"$input\"";
$params = array_merge($extraParams, get_object_vars($this));
@ -70,12 +53,6 @@ abstract class AbstractRule implements Validatable
return $exception;
}
public function setException(ValidationException $e)
{
$this->exception = $e;
return $this;
}
public function setName($name)
{
$this->name = $name;

View file

@ -13,7 +13,7 @@ class AllOf extends AbstractComposite
$summary = array(
'total' => $numRules,
'failed' => $numExceptions,
'passed' => $numRules-$numExceptions
'passed' => $numRules - $numExceptions
);
if (!empty($exceptions))
throw $this->reportError($input, $summary)->setRelated($exceptions);

View file

@ -9,14 +9,13 @@ use Respect\Validation\Validatable;
class Attribute extends AbstractRelated
{
public function __construct($reference,
Validatable $referenceValidator=null, $mandatory=true)
public function __construct($reference, Validatable $validator=null, $mandatory=true)
{
if (!is_string($reference) || empty($reference))
throw new ComponentException(
'Invalid attribute/property name'
);
parent::__construct($reference, $referenceValidator, $mandatory);
parent::__construct($reference, $validator, $mandatory);
}
public function getReferenceValue($input)

View file

@ -12,8 +12,7 @@ class Each extends AbstractRule
public $itemValidator;
public $keyValidator;
public function __construct(Validatable $itemValidator = null,
Validatable $keyValidator=null)
public function __construct(Validatable $itemValidator = null, Validatable $keyValidator=null)
{
$this->itemValidator = $itemValidator;
$this->keyValidator = $keyValidator;

View file

@ -29,7 +29,7 @@ class Zend extends AbstractRule
if (!$this->validate($input)) {
$exceptions = array();
foreach ($this->zendValidator->getMessages() as $m) {
$exceptions[] = $this->createException()->configure($m, get_object_vars($this));
$exceptions[] = $this->reportError($m, get_object_vars($this));
}
throw $this->reportError($input)->setRelated($exceptions);
}

View file

@ -11,16 +11,10 @@ interface Validatable
public function check($input);
public function createException();
public function getException();
public function getName();
public function reportError($input, array $relatedExceptions=array());
public function setException(ValidationException $e);
public function setName($name);
public function validate($input);

View file

@ -59,9 +59,14 @@ class Validator extends AllOf
return $this;
}
public function createException()
public function reportError($input, array $extraParams=array())
{
return new AllOfException;
$exception = new AllOfException;
$input = AllOfException::stringify($input);
$name = $this->getName() ? : "\"$input\"";
$params = array_merge($extraParams, get_object_vars($this));
$exception->configure($name, $params);
return $exception;
}
protected static function getRuleClassname($ruleName)

View file

@ -10,8 +10,8 @@
<email>alexandre@gaigalas.net</email>
<active>yes</active>
</lead>
<date>2011-02-10</date>
<time>18:49:40</time>
<date>2011-02-18</date>
<time>01:08:35</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -28,8 +28,8 @@ First Version
<dir baseinstalldir="Respect/Validation" name="/">
<file baseinstalldir="Respect/Validation" md5sum="4ef5c6ff21a7382ac26d74354a9c2ece" name="Contexts/AbstractContext.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a99e9fcc3f89d82a4b6dfd0fbe487014" name="Contexts/Form.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="571c9ba5e526d69bb85c721a07894b6d" name="Exceptions/AbstractCompositeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="529c6da1fb1b18ec8c95d571362898d7" name="Exceptions/AbstractRelatedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="965fbd28ef79a223b6974c812c46d44b" name="Exceptions/AbstractCompositeException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ccf9b13540fc210ca8dbe3c2656c8684" name="Exceptions/AbstractRelatedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3ca12be55a230b4a1096b680714b3222" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ae818fe09029e856ed02ce70273da0f5" name="Exceptions/AlnumException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a4e7f3830a04d92bf48ae2e88bb8d488" name="Exceptions/AlphaException.php" role="php" />
@ -67,23 +67,23 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="cb0971a4379482977ddf22a675581255" name="Exceptions/RegexException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d0498462a420d57ca74fa47e3c8c6194" name="Exceptions/SfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a9d0825a244e0b7d6412777c01367650" name="Exceptions/StringException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cf5979cee838a1267557e41aaae6e084" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="87d7f2f8ff53aa98b0a06f4623994033" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="250fe508ea6c6fa437d8cd37ac1dc697" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3677faf1ead7b088e4572cc9c2260bd2" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="afcc674a1b283927c4703dd346ca4528" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="5eb9b01455189808ae5a9928abda2b76" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="40d19b8a9f0a0bc60ba6786df0056cea" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f2dcb5631134935d3d981313cf296e54" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="478b4e96e27bceb9409c3e2bd2a66ca6" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="46719f6b47886cf026271f0ab6736453" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="38749eea751bc65585b121cdf2452b9c" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7b002c1fab94b46f85c5cb79c50a3c2d" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1f552b16151f61c07ef84a58d7b00754" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="25f54c2c1f3d756afb759261c164f8b6" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9eba4fdf3110d9302b2a80271a3c1f21" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="320aac2002443d72b2a5fc9e494bdaa7" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="39a085f3e4292b002e8bc2f73dc3e926" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ca4e19a40d5b248a915198c10cde2630" name="Rules/Between.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="952c2d9d0f61c1613d43554a0fd17e42" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fc648db054e8001969516e07bd331a26" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f460a19d4b469051add54992877ef23e" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9f72c097ae08a41b0b612799b01d7465" name="Rules/Digits.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="32044ec5ec01ffe2ffa3316a8ecfba21" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="08975a3debe889e0188e2d71816ec04b" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="01d224d7f90af4df2ff2eacb6b1c64b4" 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" />
@ -108,10 +108,10 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="c6cc0755ba8b36aebaa6ad7670fc5124" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fad11bb57927795ef56cd97e57b5bb50" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d3cf890b95664760156f902207242250" name="Rules/String.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="78e821eafd2d8ea59b5f038a488d0b2c" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0cb1b7919e6276beb1e82aa27133ef30" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e2df3f7c6104d57ff0cea651c6cc80c3" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4a90179028b0787f531982548f7861ee" name="Validator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c7e1a15f54d98c301bef8ef504cd3989" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="24e5e73d13d0e855059f33b441af66f5" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="13a221cd7ccc263afb1605371c8b7a50" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8e152b2358c2cabce815a28bd69baccc" name="Validator.php" role="php" />
</dir>
</contents>
<dependencies>
@ -135,7 +135,7 @@ First Version
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2011-02-10</date>
<date>2011-02-18</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
<notes>
First Version

View file

@ -1,74 +0,0 @@
<?php
namespace Respect\Validation;
class AbstractCompositeTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = $this->getMockForAbstractClass(
'Respect\Validation\Rules\AbstractComposite'
);
}
protected function tearDown()
{
unset($this->object);
}
public function testAddExistentValidator()
{
$validator = new Rules\Callback(function() {
return true;
});
$this->object->addRule($validator);
$this->assertContains($validator, $this->object->getRules());
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testAddNonValidator()
{
$this->object->addRule(new \stdClass);
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testAddNonValidator2()
{
$this->object->addRule('CompletelyNonExistant');
}
public function testBuildValidators()
{
$this->object->addRules(array(
'noWhitespace', 'NotEmpty', 'Alnum' => array('__')
));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testBuildValidatorsArrayParams()
{
$this->object->addRules(array(
'noWhitespace', 'Alnum' => 'aiods'
));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testBuildValidatorsNonExistant()
{
$this->object->addRules(array(
'noWhitespace', 'FooBaz'
));
}
}

View file

@ -1,88 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class AbstractRelatedTest extends \PHPUnit_Framework_TestCase
{
protected function createMock($referenceName, $validator, $mandatory,
$hasReference)
{
$mock = $this->getMockForAbstractClass(
'Respect\Validation\Rules\AbstractRelated',
array($referenceName, $validator, $mandatory)
);
$mock
->expects($this->any())
->method('hasReference')
->will($this->returnValue($hasReference));
$mock
->expects($this->any())
->method('getReferenceValue')
->will($this->returnValue($referenceName));
return $mock;
}
public function testValidateMandatoryTrue()
{
$mock = $this->createMock('whatever', new NotEmpty, true, false);
$this->assertFalse($mock->validate('whatever'));
}
public function testValidateMandatoryFalse()
{
$mock = $this->createMock('whatever', new NotEmpty, false, false);
$this->assertTrue($mock->validate('whatever'));
}
public function testValidateHasReference()
{
$mock = $this->createMock(
'', new NotEmpty, false, false
);
$this->assertFalse($mock->validate('bla'));
$mock = $this->createMock(
'this is not empty', new NotEmpty, false, false
);
$this->assertTrue($mock->validate('bla'));
}
/**
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testAssert()
{
$mock = $this->createMock(
'', new NotEmpty, false, false
);
//overriding exception cause mocks cant handle createException properly
$mock->setException(new \Respect\Validation\Exceptions\AbstractRelatedException(''));
$this->assertFalse($mock->assert('bla'));
}
/**
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testAssertHasReference()
{
$mock = $this->createMock(
'', null, true, false
);
//overriding exception cause mocks cant handle createException properly
$mock->setException(new \Respect\Validation\Exceptions\AbstractRelatedException(''));
$this->assertFalse($mock->assert('bla'));
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/
public function testCheck()
{
$mock = $this->createMock(
'', new NotEmpty, true, true
);
$this->assertFalse($mock->check('bla'));
}
}

View file

@ -60,7 +60,6 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
$valid3 = new Callback(function() {
return true;
});
$theInvalidOne->setException(new \Respect\Validation\Exceptions\ValidationException(''));
$o = new AllOf($valid1, $theInvalidOne, $valid3);
$this->assertFalse($o->check('any'));
$o = new AllOf($theInvalidOne, $valid3, $valid1);

View file

@ -223,12 +223,13 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
v::numeric()->validate($number); //true
//From 1 to 15 non-whitespace alphanumeric characters
$validUsername = v::alnum()
->noWhitespace()
->length(1, 15);
->noWhitespace()
->length(1, 15);
$validUsername->validate('alganet'); //true
$validUser = v::attribute('username', $validUsername) //reusing!
->attribute('birthdate', v::date('Y-m-d'));
->attribute('birthdate', v::date('Y-m-d'))
->attribute('birthdat', v::date('Y-m-d'));
$user = new \stdClass;
$user->username = 'alganet';
@ -248,20 +249,21 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
//echo $e->getFullMessage();
}
$validBlogPost = v::object()
->attribute('title', v::string()->length(1, 32))
->attribute('author', $validUser) //reuse!
->attribute('date', v::date())
->attribute('text', v::string());
$validBlogPost = v::allOf(v::allOf(v::object()
->attribute('title', v::string()->length(1, 32))
->attribute('author', $validUser) //reuse!
->attribute('date', v::date())
->attribute('text', v::string())))->setName('Blog Post');
$blogPost = new \stdClass;
$blogPost->author = clone $validUser;
$blogPost->author = clone $user;
$blogPost->author->username = '# invalid #';
try {
$validBlogPost->assert($blogPost);
} catch (\InvalidArgumentException $e) {
//echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage();
//echo $e->getFullMessage();
//echo $e->findRelated('author')->getMainMessage();
}
}