mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 08:12:16 +02:00
Create "Identical" rule
This commit is contained in:
parent
641b169c1b
commit
b28e97c70f
|
@ -11,6 +11,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
- Create "Extension" rule (#360)
|
||||
- Create "Factor" rule (#405)
|
||||
- Create "Finite" rule (#397)
|
||||
- Create "Identical" rule (#442)
|
||||
- Create "Infinite" rule (#397)
|
||||
- Create "KeyNested" rule (#429)
|
||||
- Create "KeySet" rule (#374)
|
||||
|
|
18
docs/Identical.md
Normal file
18
docs/Identical.md
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Identical
|
||||
|
||||
- `v::identical(mixed $value)`
|
||||
|
||||
Validates if the input is identical to some value.
|
||||
|
||||
```php
|
||||
v::identical(42)->validate(42); //true
|
||||
v::identical(42)->validate('42'); //false
|
||||
```
|
||||
|
||||
Message template for this validator includes `{{compareTo}}`.
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [Contains](Contains.md)
|
||||
* [Equals](Equals.md)
|
24
library/Exceptions/IdenticalException.php
Normal file
24
library/Exceptions/IdenticalException.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class IdenticalException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be identical as {{compareTo}}',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be identical as {{compareTo}}',
|
||||
),
|
||||
);
|
||||
}
|
27
library/Rules/Identical.php
Normal file
27
library/Rules/Identical.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Identical extends AbstractRule
|
||||
{
|
||||
public $compareTo;
|
||||
|
||||
public function __construct($compareTo)
|
||||
{
|
||||
$this->compareTo = $compareTo;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $input === $this->compareTo;
|
||||
}
|
||||
}
|
73
tests/unit/Rules/IdenticalTest.php
Normal file
73
tests/unit/Rules/IdenticalTest.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\Identical
|
||||
* @covers Respect\Validation\Exceptions\IdenticalException
|
||||
*/
|
||||
class IdenticalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForIdentical
|
||||
*/
|
||||
public function testInputIdenticalToExpectedValueShouldPass($compareTo, $input)
|
||||
{
|
||||
$rule = new Identical($compareTo);
|
||||
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotIdentical
|
||||
*/
|
||||
public function testInputNotIdenticalToExpectedValueShouldPass($compareTo, $input)
|
||||
{
|
||||
$rule = new Identical($compareTo);
|
||||
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\IdenticalException
|
||||
* @expectedExceptionMessage "42" must be identical as 42
|
||||
*/
|
||||
public function testShouldThrowTheProperExceptionWhenFailure()
|
||||
{
|
||||
$rule = new Identical(42);
|
||||
$rule->check('42');
|
||||
}
|
||||
|
||||
public function providerForIdentical()
|
||||
{
|
||||
$object = new stdClass();
|
||||
return array(
|
||||
array('foo', 'foo'),
|
||||
array(array(), array()),
|
||||
array($object, $object),
|
||||
array(10, 10),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotIdentical()
|
||||
{
|
||||
return array(
|
||||
array(42, '42'),
|
||||
array('foo', 'bar'),
|
||||
array(array(1), array()),
|
||||
array(new stdClass(), new stdClass()),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue