Removed AtLeast and MostOf (pointless validators). Lots of fixes and new tests and assertions. >96% code coverage achieved.

This commit is contained in:
Alexandre Gomes Gaigalas 2011-04-27 20:41:12 -03:00
parent 63cc857801
commit 5d7a9e191d
28 changed files with 240 additions and 302 deletions

2
.gitignore vendored
View file

@ -4,3 +4,5 @@ library/Symfony/
.project
.settings/
nbproject
/library/.fr-6ayG1u/
/library/.fr-n9BajN/

View file

@ -3,6 +3,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
@ -22,7 +23,7 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
$this->appendRule(Validator::buildRule($validator, $arguments));
else
$this->appendRule($validator);
return $this;
}
@ -56,12 +57,13 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
if (empty($this->rules))
return false;
if ($validator instanceof Valitatable)
if ($validator instanceof Validatable)
return isset($this->rules[spl_object_hash($validator)]);
foreach ($this->rules as $rule)
if ($rule instanceof $validator)
return true;
if (is_string($validator))
foreach ($this->rules as $rule)
if (get_class($rule) == __NAMESPACE__ . '\\' . $validator)
return true;
return false;
}

View file

@ -64,7 +64,8 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
elseif (!$this->mandatory && !$hasReference)
return true;
return $this->validator->validate($this->getReferenceValue($input));
return is_null($this->validator)
|| $this->validator->validate($this->getReferenceValue($input));
}
}

View file

@ -1,106 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class AtLeast extends AbstractComposite
{
public $howMany = 1;
public function __construct($howMany, $rules=array())
{
$this->howMany = $howMany;
$this->addRules($rules);
}
public function assert($input)
{
$validators = $this->getRules();
$exceptions = $this->validateRules($input);
$numRules = count($validators);
$numExceptions = count($exceptions);
$numPassed = $numRules - $numExceptions;
$summary = array(
'total' => $numRules,
'failed' => $numExceptions,
'passed' => $numPassed
);
if ($this->howMany > $numPassed)
throw $this->reportError($input, $summary)->setRelated($exceptions);
return true;
}
public function check($input)
{
$validators = $this->getRules();
$exceptions = array();
$numRules = count($validators);
$numPassed = 0;
$maxExceptions = $numRules - $this->howMany;
foreach ($validators as $v) {
try {
$v->check($input);
if (++$numPassed >= $this->howMany)
return true;
if (count($exceptions) > $maxExceptions)
throw $this->reportError(
$input,
array('passed' => $numPassed))->setRelated($exceptions);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
}
return false;
}
public function validate($input)
{
$validators = $this->getRules();
$numPassed = 0;
foreach ($validators as $v)
try {
$v->check($input);
if (++$numPassed >= $this->howMany)
return true;
} catch (ValidationException $e) {
//empty catch block is nasty, i know, but no need to do
//anything here. We just wanna count how many rules passed
}
return false;
}
}
/**
* LICENSE
*
* Copyright (c) 2009-2011, Alexandre Gomes Gaigalas.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

View file

@ -9,11 +9,6 @@ class Date extends AbstractRule
public $format = null;
protected function formatDate(DateTime $date)
{
return $date->format($this->format);
}
public function __construct($format=null)
{
$this->format = $format;

View file

@ -90,6 +90,7 @@ class Domain extends AbstractComposite
$this->whitespace->check($input);
$this->dot->check($input);
$this->domainLength->check($input);
$this->doubleHyphen->check($input);
$parts = explode('.', $input);

View file

@ -12,7 +12,8 @@ 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;
@ -24,24 +25,22 @@ class Each extends AbstractRule
if (empty($input))
return true;
elseif (is_object($input))
$input = get_object_vars($input);
elseif (!is_array($input) || $input instanceof Traversable)
if (!is_array($input) || $input instanceof Traversable)
throw $this->reportError($input);
else
foreach ($input as $key => $item)
if (isset($this->itemValidator))
try {
$this->itemValidator->assert($item);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
elseif (isset($this->keyValidator))
try {
$this->keyValidator->assert($item);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
foreach ($input as $key => $item)
if (isset($this->itemValidator))
try {
$this->itemValidator->assert($item);
} catch (ValidationException $e) {
$exceptions[] = $e;
} elseif (isset($this->keyValidator))
try {
$this->keyValidator->assert($item);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
if (!empty($exceptions))
throw $this->reportError($input)->setRelated($exceptions);
@ -51,11 +50,17 @@ class Each extends AbstractRule
public function check($input)
{
foreach ($input as $item)
if (empty($input))
return true;
if (!is_array($input) || $input instanceof Traversable)
throw $this->reportError($input);
foreach ($input as $key => $item)
if (isset($this->itemValidator))
$this->itemValidator->check($item);
elseif (isset($this->keyValidator))
$this->keyValidator->check($item);
$this->keyValidator->check($key);
return true;
}
@ -64,14 +69,15 @@ class Each extends AbstractRule
{
if (empty($input))
return true;
elseif (!is_array($input) || $input instanceof Traversable)
return false;
else
foreach ($input as $key => $item)
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
return false;
elseif (isset($this->keyValidator) && !$this->keyValidator->validate($key))
return false;
foreach ($input as $key => $item)
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
return false;
elseif (isset($this->keyValidator) && !$this->keyValidator->validate($key))
return false;
return true;
}

View file

@ -1,46 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class MostOf extends AtLeast
{
public function __construct()
{
$this->howMany = ceil(func_num_args() / 2);
$this->addRules(func_get_args());
}
}
/**
* LICENSE
*
* Copyright (c) 2009-2011, Alexandre Gomes Gaigalas.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

View file

@ -10,8 +10,8 @@
<email>alexandre@gaigalas.net</email>
<active>yes</active>
</lead>
<date>2011-04-17</date>
<time>12:29:42</time>
<date>2011-04-27</date>
<time>20:40:59</time>
<version>
<release>0.2</release>
<api>0.2</api>
@ -27,7 +27,7 @@
<contents>
<dir baseinstalldir="Respect/Validation" name="/">
<file baseinstalldir="Respect/Validation" md5sum="1f0a6acb3e14603fa75a6dc96052947e" name="Exceptions/AbstractGroupedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0f7ede91c65a22b2f10c095a96a383d7" name="Exceptions/AbstractNestedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d1bf3c5bd8867251770188f303433742" name="Exceptions/AbstractNestedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8a4dd9c923407917701a243e5b8cbe98" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d895b3896ffa43b932c790662c7502c0" name="Exceptions/AlnumException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d86f455852a42aba71393c2f643481b3" name="Exceptions/AlphaException.php" role="php" />
@ -73,23 +73,22 @@
<file baseinstalldir="Respect/Validation" md5sum="b586352bd8f14779f45fe88b56402b74" name="Exceptions/TldException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="39ee61d03e9901df3f57d8429fae47cf" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="59ffa243ffc76998c041121a67910e49" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="69c3486f35b2144149d34bfa15ad5b82" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4f5dbde6198921823a199ccf09b63770" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e66388c3d06a5ae83d30fa03552b0bcc" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0b4dbaf3ec8a99992122776ab0cbaf04" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="538ed692251e1a2593c1cd96534b075a" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3b6e284d165b2934650214b9af3422dc" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ba19b564226ca821e5ad20a094d46ae7" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8d75ac998a6f9565d336414773137588" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a1025c62f5ca30c895fcce9cc6ccab6e" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ea461cd10c0c370b6688cbdd1390914d" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="37c47be9f5980a1ef302c2b0db1c92f4" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="22d780a6fbde4e7857ff31fe37c49378" name="Rules/Between.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="48f411a25cf6ff19fbcbf2462d7fde52" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c9a56e67bd3a223a02e61d9ebb5e0686" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1991c883762c51e3500688edacd7fda4" name="Rules/Contains.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7491bfd622cb9d5f45cc6d9380a90c4b" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="13522282c1c471a9cf15478be62d0f99" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e39d5076b9f1202770e1e949c3caae64" name="Rules/Digits.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ac2c94c310c7c59e833b50bdf306d706" name="Rules/Domain.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2f2b1457eef43cb620ef66bec0a2294b" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="47b6f98b6c6eac3e70a74a71c8286024" name="Rules/Domain.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="846e44bbb31639feb33497bdf2081466" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="29308df1b087fbabaf066084da6aebbe" name="Rules/EndsWith.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0a6b646c35fe40824ca1494b7a9661d1" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d8076da57a7549b902aa62769bc42e67" name="Rules/Float.php" role="php" />
@ -102,10 +101,9 @@
<file baseinstalldir="Respect/Validation" md5sum="f67d417f8067c8e547ae0486dc67d7e4" name="Rules/Length.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="998ade345898447919a43730c1334282" name="Rules/Max.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="492fd7483a84888f37de24f25ade58a3" name="Rules/Min.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9eccb13230dba42ee5fef5465ed2d788" name="Rules/MostOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8d5d38b16b9407a45b5a1a11078fe68a" name="Rules/Negative.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9b58e8b9868b467e9cb9b02f0bb2c428" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="891d5667cf38afa0d49e16dcc38c2636" name="Rules/Not.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2ba0b8d870a03950ef22fcd660470fb1" name="Rules/Not.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d9af701fa6d2dc7f1c4c22dcd252623d" name="Rules/NotEmpty.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a8dfcac9f375bbc402163dc968636c65" name="Rules/NoWhitespace.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7cde6db2e3ec8ff81ccc6f9071bde955" name="Rules/NullValue.php" role="php" />
@ -121,7 +119,7 @@
<file baseinstalldir="Respect/Validation" md5sum="afa7d33bee75d08ac2302eb5491b590b" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="904f06fb7ae379ec7f5a8af65a30e365" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a19a2b951bb4aa67531fb6dc5a161e46" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a93bff0ce964b9954ecd3e8ade26e347" name="Validator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="03f21c4b124a2e64f3b0548564549172" name="Validator.php" role="php" />
</dir>
</contents>
<dependencies>
@ -145,7 +143,7 @@
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2011-04-17</date>
<date>2011-04-27</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
<notes>
.

View file

@ -9,6 +9,32 @@ use Respect\Validation\Validator;
class AllOfTest extends \PHPUnit_Framework_TestCase
{
public function testRemoveRules()
{
$o = new AllOf(new Int, new Positive);
$o->removeRules();
$this->assertEquals(0, count($o->getRules()));
}
public function testAddRulesArrayMulti()
{
$o = new AllOf();
$o->addRules(
array(
array($x = new Int, new Positive)
)
);
$this->assertTrue($o->hasRule($x));
$this->assertTrue($o->hasRule('Positive'));
}
public function testAddRulesSpec()
{
$o = new AllOf();
$o->addRules(array("Between" => array(1, 2)));
$this->assertTrue($o->hasRule('Between'));
}
public function testValid()
{
$valid1 = new Callback(function() {

View file

@ -21,7 +21,8 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
public function testAlnumInvalid($invalidAlnum, $aditional)
{
$validator = new Alnum($aditional);
$validator->assert($invalidAlnum);
$this->assertFalse($validator->validate($invalidAlnum));
$this->assertFalse($validator->assert($invalidAlnum));
}
/**

View file

@ -21,7 +21,8 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
public function testAlphaInvalid($invalidAlpha, $aditional)
{
$validator = new Alpha($aditional);
$validator->assert($invalidAlpha);
$this->assertFalse($validator->validate($invalidAlpha));
$this->assertFalse($validator->assert($invalidAlpha));
}
/**

View file

@ -18,6 +18,7 @@ class ArrTest extends \PHPUnit_Framework_TestCase
*/
public function testArray($input)
{
$this->assertTrue($this->object->validate($input));
$this->assertTrue($this->object->assert($input));
}
@ -27,7 +28,8 @@ class ArrTest extends \PHPUnit_Framework_TestCase
*/
public function testNotArray($input)
{
$this->assertTrue($this->object->assert($input));
$this->assertFalse($this->object->validate($input));
$this->assertFalse($this->object->assert($input));
}
public function providerForArray()

View file

@ -1,44 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\ValidatorTestCase;
use Respect\Validation\Exceptions\InvalidException;
class AtLeastTest extends \PHPUnit_Framework_TestCase
{
public function testValid()
{
$valid1 = new Callback(function() {
return true;
});
$valid2 = new Callback(function() {
return true;
});
$valid3 = new Callback(function() {
return false;
});
$o = new AtLeast(2, array($valid1, $valid2, $valid3));
$this->assertTrue($o->assert('any'));
}
/**
* @expectedException Respect\Validation\Exceptions\AtLeastException
*/
public function testInvalid()
{
$valid1 = new Callback(function() {
return false;
});
$valid2 = new Callback(function() {
return false;
});
$valid3 = new Callback(function() {
return true;
});
$o = new AtLeast(2, array($valid1, $valid2, $valid3));
$this->assertFalse($o->assert('any'));
}
}

View file

@ -17,6 +17,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
$validator = new Attribute('bar');
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->validate($obj));
$this->assertTrue($validator->assert($obj));
}
@ -28,7 +29,19 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
$validator = new Attribute('bar');
$obj = new \stdClass;
$obj->baraaaaa = 'foo';
$this->assertTrue($validator->assert($obj));
$this->assertFalse($validator->validate($obj));
$this->assertFalse($validator->assert($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNullCheck()
{
$validator = new Attribute('bar');
$obj = new \stdClass;
$obj->baraaaaa = 'foo';
$this->assertFalse($validator->validate($obj));
$this->assertFalse($validator->check($obj));
}
/**
@ -55,7 +68,9 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
$validator = new Attribute('bar', $subValidator);
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->validate($obj));
$this->assertTrue($validator->assert($obj));
$this->assertTrue($validator->check($obj));
}
public function testNotMandatory()

View file

@ -53,6 +53,7 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
public function testBetweenBounds($min, $max, $inclusive, $input)
{
$o = new Between($min, $max, $inclusive);
$this->assertTrue($o->validate($input));
$this->assertTrue($o->assert($input));
}
@ -63,7 +64,8 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
public function testNotBetweenBounds($min, $max, $inclusive, $input)
{
$o = new Between($min, $max, $inclusive);
$this->assertTrue($o->assert($input));
$this->assertFalse($o->validate($input));
$this->assertFalse($o->assert($input));
}
}

View file

@ -36,7 +36,8 @@ class CallTest extends \PHPUnit_Framework_TestCase
public function testCallbackNot()
{
$v = new Call('strrev', new Arr);
$this->assertTrue($v->assert('test'));
$this->assertFalse($v->validate('test'));
$this->assertFalse($v->assert('test'));
}
}

View file

@ -34,6 +34,11 @@ class DateTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->object->validate('aids'));
}
public function testInvalidDateObject()
{
$this->assertFalse($this->object->validate(new \stdClass));
}
public function testDateFormat()
{
$this->object = new Date('Y-m-d');

View file

@ -21,7 +21,8 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
public function testDigitsInvalid($invalidDigits, $aditional='')
{
$validator = new Digits($aditional);
$validator->assert($invalidDigits);
$this->assertFalse($validator->validate($invalidDigits));
$this->assertFalse($validator->assert($invalidDigits));
}
/**

View file

@ -18,16 +18,27 @@ class DomainTest extends \PHPUnit_Framework_TestCase
*/
public function testDomain($input)
{
$this->assertTrue($this->object->validate($input));
$this->assertTrue($this->object->assert($input));
$this->assertTrue($this->object->check($input));
}
/**
* @dataProvider providerForNotDomain
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotDomain($input)
{
$this->assertFalse($this->object->check($input));
}
/**
* @dataProvider providerForNotDomain
* @expectedException Respect\Validation\Exceptions\DomainException
*/
public function testNotDomain($input)
public function testNotDomainCheck($input)
{
$this->assertTrue($this->object->assert($input));
$this->assertFalse($this->object->assert($input));
}
public function providerForDomain()

View file

@ -12,12 +12,25 @@ class EachTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($result);
}
public function testEachCheck()
{
$v = new Each(new NotEmpty());
$result = $v->check(array(1, 2, 3, 4, 5));
$this->assertTrue($result);
}
public function testEachKey()
{
$v = new Each(new Alpha(), new Int());
$result = $v->validate(array('a', 'b', 'c', 'd', 'e'));
$this->assertTrue($result);
}
public function testEachKeyCheck()
{
$v = new Each(new Alpha(), new Int());
$result = $v->check(array('a', 'b', 'c', 'd', 'e'));
$this->assertTrue($result);
}
public function testEachKeyOnly()
{
@ -25,6 +38,12 @@ class EachTest extends \PHPUnit_Framework_TestCase
$result = $v->validate(array('a', 'b', 'c', 'd', 'e'));
$this->assertTrue($result);
}
public function testEachKeyOnlyCheck()
{
$v = new Each(null, new Int());
$result = $v->check(array('a', 'b', 'c', 'd', 'e'));
$this->assertTrue($result);
}
public function testEachNotTraversable()
{

View file

@ -14,6 +14,17 @@ class LengthTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($validator->assert($string));
}
/**
* @dataProvider providerForInvalidLenghtInclusive
* @expectedException Respect\Validation\Exceptions\LengthException
*/
public function testLengthInvalidInclusive($string, $min, $max)
{
$validator = new Length($min, $max, false);
$this->assertfalse($validator->validate($string));
$this->assertfalse($validator->assert($string));
}
/**
* @dataProvider providerForInvalidLenght
* @expectedException Respect\Validation\Exceptions\LengthException
@ -21,6 +32,7 @@ class LengthTest extends \PHPUnit_Framework_TestCase
public function testLengthInvalid($string, $min, $max)
{
$validator = new Length($min, $max);
$this->assertFalse($validator->validate($string));
$this->assertFalse($validator->assert($string));
}
@ -31,6 +43,7 @@ class LengthTest extends \PHPUnit_Framework_TestCase
public function testLengthComponentException($string, $min, $max)
{
$validator = new Length($min, $max);
$this->assertFalse($validator->validate($string));
$this->assertFalse($validator->assert($string));
}
@ -39,15 +52,27 @@ class LengthTest extends \PHPUnit_Framework_TestCase
return array(
array('alganet', 1, 15),
array(range(1, 20), 1, 30),
array((object) array('foo'=>'bar', 'bar'=>'baz'), 1, 2),
array('alganet', 1, null), //null is a valid max length, means "no maximum",
array('alganet', null, 15) //null is a valid min length, means "no minimum"
);
}
public function providerForInvalidLenghtInclusive()
{
return array(
array('alganet', 1, 7),
array(range(1, 20), 1, 20),
array('alganet', 7, null), //null is a valid max length, means "no maximum",
array('alganet', null, 7) //null is a valid min length, means "no minimum"
);
}
public function providerForInvalidLenght()
{
return array(
array('alganet', 1, 3),
array((object) array('foo'=>'bar', 'bar'=>'baz'), 3, 5),
array(range(1, 50), 1, 30),
);
}
@ -57,6 +82,7 @@ class LengthTest extends \PHPUnit_Framework_TestCase
return array(
array('alganet', 'a', 15),
array('alganet', 1, 'abc d'),
array('alganet', 10, 1),
);
}

View file

@ -1,44 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\ValidatorTestCase;
use Respect\Validation\Exceptions\InvalidException;
class MostOfTest extends \PHPUnit_Framework_TestCase
{
public function testValid()
{
$valid1 = new Callback(function() {
return true;
});
$valid2 = new Callback(function() {
return true;
});
$valid3 = new Callback(function() {
return false;
});
$o = new MostOf($valid1, $valid2, $valid3);
$this->assertTrue($o->assert('any'));
}
/**
* @expectedException Respect\Validation\Exceptions\MostOfException
*/
public function testInvalid()
{
$valid1 = new Callback(function() {
return false;
});
$valid2 = new Callback(function() {
return false;
});
$valid3 = new Callback(function() {
return true;
});
$o = new MostOf($valid1, $valid2, $valid3);
$this->assertFalse($o->assert('any'));
}
}

View file

@ -20,6 +20,7 @@ class NoneOfTest extends \PHPUnit_Framework_TestCase
return false;
});
$o = new NoneOf($valid1, $valid2, $valid3);
$this->assertTrue($o->validate('any'));
$this->assertTrue($o->assert('any'));
}
@ -38,6 +39,7 @@ class NoneOfTest extends \PHPUnit_Framework_TestCase
return true;
});
$o = new NoneOf($valid1, $valid2, $valid3);
$this->assertFalse($o->validate('any'));
$this->assertFalse($o->assert('any'));
}

View file

@ -0,0 +1,44 @@
<?php
namespace Respect\Validation\Rules;
class NotTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForValidNot
*
*/
public function testNot($v, $input)
{
$not = new Not($v);
$this->assertTrue($not->assert($input));
}
/**
* @dataProvider providerForInvalidNot
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNotHaha($v, $input)
{
$not = new Not($v);
$this->assertFalse($not->assert($input));
}
public function providerForValidNot()
{
return array(
array(new Int, 'aaa'),
array(new AllOf(new NoWhitespace, new Digits), 'as df')
);
}
public function providerForInvalidNot()
{
return array(
array(new Int, 123),
array(new AllOf(new NoWhitespace, new Digits), '12 34')
);
}
}

View file

@ -41,4 +41,13 @@ class OneOfTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($o->assert('any'));
}
/**
* @expectedException Respect\Validation\Exceptions\StringException
*/
public function testInvalidCheck()
{
$o = new OneOf(new String, new Alnum);
$this->assertFalse($o->check(-10));
}
}

View file

@ -15,6 +15,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
public function testSimpleOk()
{
$v = new Zend('alnum');
$this->assertTrue($v->validate('wp2oiur'));
$this->assertTrue($v->assert('wp2oiur'));
}
@ -29,7 +30,8 @@ class ZendTest extends \PHPUnit_Framework_TestCase
public function testSimpleNot()
{
$v = new Zend('alnum');
$this->assertTrue($v->assert('#$%#$%'));
$this->assertFalse($v->validate('#$%#$%'));
$this->assertFalse($v->assert('#$%#$%'));
}
public function testParamsOk()
@ -44,7 +46,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
public function testParamsNot()
{
$v = new Zend('stringLength', array('min' => 10, 'max' => 25));
$this->assertTrue($v->assert('aw'));
$this->assertFalse($v->assert('aw'));
}
}

View file

@ -11,5 +11,11 @@
syntaxCheck="false"
verbose="true"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
<filter>
<blacklist>
<directory>../library/Zend/</directory>
<directory>../library/Symfony/</directory>
</blacklist>
</filter>
</phpunit>