mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Increase code coverage of rules
This commit is contained in:
parent
dddad80f48
commit
e2b02b9777
|
@ -13,7 +13,7 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class AlwaysInvalidException extends ValidationException
|
||||
{
|
||||
const SIMPLE = 0;
|
||||
const SIMPLE = 1;
|
||||
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
|
|
|
@ -32,10 +32,8 @@ class AllOf extends AbstractComposite
|
|||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v) {
|
||||
if (!$v->check($input)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->getRules() as $rule) {
|
||||
$rule->check($input);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -43,8 +41,8 @@ class AllOf extends AbstractComposite
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v) {
|
||||
if (!$v->validate($input)) {
|
||||
foreach ($this->getRules() as $rule) {
|
||||
if (!$rule->validate($input)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,6 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
class AlwaysInvalid extends AbstractRule
|
||||
{
|
||||
public function __invoke($input)
|
||||
{
|
||||
return $this->validate($input);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return false;
|
||||
|
|
17
tests/integration/alwaysInvalid_1.phpt
Normal file
17
tests/integration/alwaysInvalid_1.phpt
Normal file
|
@ -0,0 +1,17 @@
|
|||
--TEST--
|
||||
alwaysInvalid()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AlwaysInvalidException;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
try {
|
||||
Validator::alwaysInvalid()->check('whatever');
|
||||
} catch (AlwaysInvalidException $e) {
|
||||
echo $e->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"whatever" is always invalid
|
17
tests/integration/alwaysInvalid_2.phpt
Normal file
17
tests/integration/alwaysInvalid_2.phpt
Normal file
|
@ -0,0 +1,17 @@
|
|||
--TEST--
|
||||
alwaysInvalid()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
try {
|
||||
Validator::alwaysInvalid()->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
\-"" is always invalid
|
14
tests/integration/alwaysValid_1.phpt
Normal file
14
tests/integration/alwaysValid_1.phpt
Normal file
|
@ -0,0 +1,14 @@
|
|||
--TEST--
|
||||
alwaysValid()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::alwaysValid()->check(true);
|
||||
v::alwaysValid()->check(false);
|
||||
v::alwaysValid()->assert('string');
|
||||
v::alwaysValid()->assert(new stdClass());
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,12 +0,0 @@
|
|||
--TEST--
|
||||
alwaysInvalid()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
var_dump(Validator::alwaysInvalid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
||||
?>
|
||||
--EXPECTF--
|
||||
bool(false)
|
|
@ -1,12 +0,0 @@
|
|||
--TEST--
|
||||
alwaysValid()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
var_dump(Validator::alwaysValid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
||||
?>
|
||||
--EXPECTF--
|
||||
bool(true)
|
|
@ -29,9 +29,12 @@ abstract class RuleTestCase extends \PHPUnit_Framework_TestCase
|
|||
abstract public function providerForInvalidInput();
|
||||
|
||||
/**
|
||||
* @param bool $expectedResult
|
||||
* @param string[optional] $mockClassName
|
||||
*
|
||||
* @return \Respect\Validation\Validatable
|
||||
*/
|
||||
public function getRuleMock($expectedResult = true)
|
||||
public function getRuleMock($expectedResult, $mockClassName = '')
|
||||
{
|
||||
$ruleMocked = $this->getMockBuilder('Respect\Validation\Validatable')
|
||||
->disableOriginalConstructor()
|
||||
|
@ -40,6 +43,7 @@ abstract class RuleTestCase extends \PHPUnit_Framework_TestCase
|
|||
'assert', 'check', 'getName', 'reportError', 'setName', 'setTemplate', 'validate',
|
||||
]
|
||||
)
|
||||
->setMockClassName($mockClassName)
|
||||
->getMock();
|
||||
|
||||
$ruleMocked
|
||||
|
@ -63,12 +67,12 @@ abstract class RuleTestCase extends \PHPUnit_Framework_TestCase
|
|||
$ruleMocked
|
||||
->expects($this->any())
|
||||
->method('check')
|
||||
->willThrowException(new ValidationException())
|
||||
->willThrowException(new ValidationException('Exception for '.$mockClassName.':check() method'))
|
||||
;
|
||||
$ruleMocked
|
||||
->expects($this->any())
|
||||
->method('assert')
|
||||
->willThrowException(new ValidationException())
|
||||
->willThrowException(new ValidationException('Exception for '.$mockClassName.':assert() method'))
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,8 +13,8 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\AllOf
|
||||
* @covers Respect\Validation\Exceptions\AllOfException
|
||||
* @covers Respect\Validation\Rules\Alnum
|
||||
* @covers Respect\Validation\Exceptions\AlnumException
|
||||
*/
|
||||
class AlnumTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
|
|
@ -14,17 +14,31 @@ namespace Respect\Validation\Rules;
|
|||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\AlwaysInvalid
|
||||
* @covers Respect\Validation\Exceptions\AlwaysInvalidException
|
||||
*/
|
||||
class AlwaysInvalidTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\AlwaysInvalidException
|
||||
*/
|
||||
public function testAssertShouldThrowExceptionForEmptyInput()
|
||||
public function providerForValidAlwaysInvalid()
|
||||
{
|
||||
$validator = new AlwaysInvalid();
|
||||
return [
|
||||
[0],
|
||||
[1],
|
||||
['string'],
|
||||
[true],
|
||||
[false],
|
||||
[null],
|
||||
[''],
|
||||
[[]],
|
||||
[['array_full']],
|
||||
];
|
||||
}
|
||||
|
||||
$validator->assert('');
|
||||
/**
|
||||
* @dataProvider providerForValidAlwaysInvalid
|
||||
*/
|
||||
public function testShouldValidateInputWhenItIsAValidAlwaysInvalid($input)
|
||||
{
|
||||
$rule = new AlwaysInvalid();
|
||||
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,20 +14,77 @@ namespace Respect\Validation\Rules;
|
|||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\When
|
||||
* @covers Respect\Validation\Exceptions\WhenException
|
||||
*/
|
||||
class WhenTest extends RuleTestCase
|
||||
{
|
||||
public function testShouldConstructAnObjectWithoutElseRule()
|
||||
{
|
||||
$v = new When($this->getRuleMock(), $this->getRuleMock());
|
||||
$this->assertInstanceOf('\Respect\Validation\Rules\AlwaysInvalid', $v->else);
|
||||
$rule = new When($this->getRuleMock(true), $this->getRuleMock(true));
|
||||
|
||||
$this->assertInstanceOf('\Respect\Validation\Rules\AlwaysInvalid', $rule->else);
|
||||
}
|
||||
|
||||
public function testShouldConstructAnObjectWithElseRule()
|
||||
{
|
||||
$v = new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock());
|
||||
$this->assertNotNull($v->else);
|
||||
$rule = new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true));
|
||||
|
||||
$this->assertNotNull($rule->else);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedExceptionMessage Exception for ThenNotValid:assert() method
|
||||
*/
|
||||
public function testShouldThrowExceptionForTheThenRuleWhenTheIfRuleIsValidAndTheThenRuleIsNotOnAssertMethod()
|
||||
{
|
||||
$if = $this->getRuleMock(true);
|
||||
$then = $this->getRuleMock(false, 'ThenNotValid');
|
||||
$else = $this->getRuleMock(true);
|
||||
|
||||
$rule = new When($if, $then, $else);
|
||||
$rule->assert('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedExceptionMessage Exception for ThenNotValid:check() method
|
||||
*/
|
||||
public function testShouldThrowExceptionForTheThenRuleWhenTheIfRuleIsValidAndTheThenRuleIsNotOnCheckMethod()
|
||||
{
|
||||
$if = $this->getRuleMock(true);
|
||||
$then = $this->getRuleMock(false, 'ThenNotValid');
|
||||
$else = $this->getRuleMock(true);
|
||||
|
||||
$rule = new When($if, $then, $else);
|
||||
$rule->check('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedExceptionMessage Exception for ElseNotValid:assert() method
|
||||
*/
|
||||
public function testShouldThrowExceptionForTheElseRuleWhenTheIfRuleIsNotValidAndTheElseRuleIsNotOnAssertMethod()
|
||||
{
|
||||
$if = $this->getRuleMock(false);
|
||||
$then = $this->getRuleMock(false);
|
||||
$else = $this->getRuleMock(false, 'ElseNotValid');
|
||||
|
||||
$rule = new When($if, $then, $else);
|
||||
$rule->assert('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedExceptionMessage Exception for ElseNotValid:check() method
|
||||
*/
|
||||
public function testShouldThrowExceptionForTheElseRuleWhenTheIfRuleIsNotValidAndTheElseRuleIsNotOnCheckMethod()
|
||||
{
|
||||
$if = $this->getRuleMock(false);
|
||||
$then = $this->getRuleMock(false);
|
||||
$else = $this->getRuleMock(false, 'ElseNotValid');
|
||||
|
||||
$rule = new When($if, $then, $else);
|
||||
$rule->check('');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,31 +96,31 @@ class WhenTest extends RuleTestCase
|
|||
{
|
||||
return [
|
||||
'int (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
42,
|
||||
],
|
||||
'bool (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
true,
|
||||
],
|
||||
'empty (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
'',
|
||||
],
|
||||
'object (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
new \stdClass(),
|
||||
],
|
||||
'empty array (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
[],
|
||||
],
|
||||
'not empty array (all true)' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(true)),
|
||||
['test'],
|
||||
],
|
||||
'when = true, then = false, else = true' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(), $this->getRuleMock(false)),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(true), $this->getRuleMock(false)),
|
||||
false,
|
||||
],
|
||||
|
||||
|
@ -77,11 +134,11 @@ class WhenTest extends RuleTestCase
|
|||
{
|
||||
return [
|
||||
'when = true, then = false, else = false' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(false), $this->getRuleMock(false)),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(false), $this->getRuleMock(false)),
|
||||
false,
|
||||
],
|
||||
'when = true, then = false, else = true' => [
|
||||
new When($this->getRuleMock(), $this->getRuleMock(false), $this->getRuleMock()),
|
||||
new When($this->getRuleMock(true), $this->getRuleMock(false), $this->getRuleMock(true)),
|
||||
false,
|
||||
],
|
||||
'when = false, then = false, else = false' => [
|
||||
|
|
Loading…
Reference in a new issue