mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 23:59:51 +01:00
Make the ValidationException a little bit less mutable than before. All its dependencies are now passed into the constructor. This commit also make the Factory pass the translator to the exceptions allowing to define the translator before the exception gets created. This change is not the ideal one, later I would like to not need the Singleton from the Factory to do that, but for now it seems like a good approach. One more thing that this commit does is to introduce the "id" for Exceptions. Key can be either the defined "name" or the name of the rule that throwed the exception. This method will be handy to identify exceptions better. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
169 lines
4.7 KiB
PHP
169 lines
4.7 KiB
PHP
<?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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
class AbstractRuleTest extends TestCase
|
|
{
|
|
public function providerForTrueAndFalse()
|
|
{
|
|
return [
|
|
[true],
|
|
[false],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerForTrueAndFalse
|
|
* @covers \Respect\Validation\Rules\AbstractRule::__invoke
|
|
*/
|
|
public function testMagicMethodInvokeCallsValidateWithInput($booleanResult): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects($this->once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will($this->returnValue($booleanResult));
|
|
|
|
self::assertEquals(
|
|
$booleanResult,
|
|
// Invoking it to trigger __invoke
|
|
$abstractRuleMock($input),
|
|
'When invoking an instance of AbstractRule, the method validate should be called with the same input and return the same result.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::assert
|
|
*/
|
|
public function testAssertInvokesValidateOnSuccess(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate', 'reportError'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects($this->once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will($this->returnValue(true));
|
|
|
|
$abstractRuleMock
|
|
->expects($this->never())
|
|
->method('reportError');
|
|
|
|
$abstractRuleMock->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::assert
|
|
* @expectedException \Respect\Validation\Exceptions\ValidationException
|
|
*/
|
|
public function testAssertInvokesValidateAndReportErrorOnFailure(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate', 'reportError'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects($this->once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will($this->returnValue(false));
|
|
|
|
$abstractRuleMock
|
|
->expects($this->once())
|
|
->method('reportError')
|
|
->with($input)
|
|
->will($this->throwException(new ValidationException($input, 'abstract', [], 'trim')));
|
|
|
|
$abstractRuleMock->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::check
|
|
*/
|
|
public function testCheckInvokesAssertToPerformTheValidationByDefault(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['assert'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects($this->once())
|
|
->method('assert')
|
|
->with($input);
|
|
|
|
$abstractRuleMock->check($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setTemplate
|
|
*/
|
|
public function testShouldReturnTheCurrentObjectWhenDefinigTemplate(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
self::assertSame($abstractRuleMock, $abstractRuleMock->setTemplate('whatever'));
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setName
|
|
*/
|
|
public function testShouldReturnTheCurrentObjectWhenDefinigName(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
self::assertSame($abstractRuleMock, $abstractRuleMock->setName('whatever'));
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setName
|
|
* @covers \Respect\Validation\Rules\AbstractRule::getName
|
|
*/
|
|
public function testShouldBeAbleToDefineAndRetrivedRuleName(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
$name = 'something';
|
|
|
|
$abstractRuleMock->setName($name);
|
|
|
|
self::assertSame($name, $abstractRuleMock->getName());
|
|
}
|
|
}
|