mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
Apply contribution guidelines to "Identical" rule
This commit is contained in:
parent
02b51782c3
commit
2a2bb6dd76
|
@ -13,8 +13,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class IdenticalException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IdenticalException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be identical as {{compareTo}}',
|
||||
|
|
|
@ -13,16 +13,32 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Identical extends AbstractRule
|
||||
/**
|
||||
* Validates if the input is identical to some value.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class Identical extends AbstractRule
|
||||
{
|
||||
public $compareTo;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $compareTo;
|
||||
|
||||
/**
|
||||
* Initializes the rule.
|
||||
*
|
||||
* @param mixed $compareTo
|
||||
*/
|
||||
public function __construct($compareTo)
|
||||
{
|
||||
$this->compareTo = $compareTo;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return $input === $this->compareTo;
|
||||
}
|
||||
|
|
37
tests/integration/rules/identical.phpt
Normal file
37
tests/integration/rules/identical.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IdenticalException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::identical(123)->check(321);
|
||||
} catch (IdenticalException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::identical(321))->check(321);
|
||||
} catch (IdenticalException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::identical(123)->assert(321);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::identical(321))->assert(321);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
321 must be identical as 123
|
||||
321 must not be identical as 321
|
||||
- 321 must be identical as 123
|
||||
- 321 must not be identical as 321
|
|
@ -14,64 +14,57 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Identical
|
||||
* @covers \Respect\Validation\Exceptions\IdenticalException
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class IdenticalTest extends TestCase
|
||||
final class IdenticalTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForIdentical
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testInputIdenticalToExpectedValueShouldPass($compareTo, $input): void
|
||||
{
|
||||
$rule = new Identical($compareTo);
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotIdentical
|
||||
*/
|
||||
public function testInputNotIdenticalToExpectedValueShouldPass($compareTo, $input): void
|
||||
{
|
||||
$rule = new Identical($compareTo);
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\IdenticalException
|
||||
* @expectedExceptionMessage "42" must be identical as 42
|
||||
*/
|
||||
public function testShouldThrowTheProperExceptionWhenFailure(): void
|
||||
{
|
||||
$rule = new Identical(42);
|
||||
$rule->check('42');
|
||||
}
|
||||
|
||||
public function providerForIdentical()
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$object = new stdClass();
|
||||
|
||||
return [
|
||||
['foo', 'foo'],
|
||||
[[], []],
|
||||
[$object, $object],
|
||||
[10, 10],
|
||||
[new Identical('foo'), 'foo'],
|
||||
[new Identical([]), []],
|
||||
[new Identical($object), $object],
|
||||
[new Identical(10), 10],
|
||||
[new Identical(10.0), 10.0],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotIdentical()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
[42, '42'],
|
||||
['foo', 'bar'],
|
||||
[[1], []],
|
||||
[new stdClass(), new stdClass()],
|
||||
[new Identical(42), '42'],
|
||||
[new Identical('foo'), 'bar'],
|
||||
[new Identical([1]), []],
|
||||
[new Identical(new stdClass()), new stdClass()],
|
||||
[new Identical(10), 10.0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function shouldPassCompareToParameterToException(): void
|
||||
{
|
||||
$compareTo = new stdClass();
|
||||
$rule = new Identical($compareTo);
|
||||
$exception = $rule->reportError('input');
|
||||
|
||||
self::assertSame($compareTo, $exception->getParam('compareTo'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue