mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
There should not be too much code in the ValidationException. It is hard to test and debug code in exceptions because PHP does not trace further than their constructor. This commit will move the message formatting from ValidationException into a Formatter class (inside a Message namespace). Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
197 lines
5.2 KiB
PHP
197 lines
5.2 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 Respect\Validation\Exceptions\ValidationException;
|
|
use Respect\Validation\Message\Formatter;
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule
|
|
*
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
final class AbstractRuleTest extends TestCase
|
|
{
|
|
/**
|
|
* @return bool[][]
|
|
*/
|
|
public function providerForTrueAndFalse(): array
|
|
{
|
|
return [
|
|
[true],
|
|
[false],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerForTrueAndFalse
|
|
* @covers \Respect\Validation\Rules\AbstractRule::__invoke
|
|
*
|
|
* @test
|
|
*/
|
|
public function magicMethodInvokeCallsValidateWithInput(bool $booleanResult): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects(self::once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will(self::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
|
|
*
|
|
* @test
|
|
*/
|
|
public function assertInvokesValidateOnSuccess(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate', 'reportError'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects(self::once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will(self::returnValue(true));
|
|
|
|
$abstractRuleMock
|
|
->expects(self::never())
|
|
->method('reportError');
|
|
|
|
$abstractRuleMock->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::assert
|
|
* @expectedException \Respect\Validation\Exceptions\ValidationException
|
|
*
|
|
* @test
|
|
*/
|
|
public function assertInvokesValidateAndReportErrorOnFailure(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['validate', 'reportError'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects(self::once())
|
|
->method('validate')
|
|
->with($input)
|
|
->will(self::returnValue(false));
|
|
|
|
$abstractRuleMock
|
|
->expects(self::once())
|
|
->method('reportError')
|
|
->with($input)
|
|
->will(self::throwException(
|
|
new ValidationException($input, 'abstract', [], new Formatter('strval'))
|
|
));
|
|
|
|
$abstractRuleMock->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::check
|
|
*
|
|
* @test
|
|
*/
|
|
public function checkInvokesAssertToPerformTheValidationByDefault(): void
|
|
{
|
|
$input = 'something';
|
|
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->setMethods(['assert'])
|
|
->getMockForAbstractClass();
|
|
|
|
$abstractRuleMock
|
|
->expects(self::once())
|
|
->method('assert')
|
|
->with($input);
|
|
|
|
$abstractRuleMock->check($input);
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setTemplate
|
|
*
|
|
* @test
|
|
*/
|
|
public function shouldReturnTheCurrentObjectWhenDefinigTemplate(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
self::assertSame($abstractRuleMock, $abstractRuleMock->setTemplate('whatever'));
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setName
|
|
*
|
|
* @test
|
|
*/
|
|
public function shouldReturnTheCurrentObjectWhenDefinigName(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
self::assertSame($abstractRuleMock, $abstractRuleMock->setName('whatever'));
|
|
}
|
|
|
|
/**
|
|
* @covers \Respect\Validation\Rules\AbstractRule::getName
|
|
* @covers \Respect\Validation\Rules\AbstractRule::setName
|
|
*
|
|
* @test
|
|
*/
|
|
public function shouldBeAbleToDefineAndRetrivedRuleName(): void
|
|
{
|
|
$abstractRuleMock = $this
|
|
->getMockBuilder(AbstractRule::class)
|
|
->getMockForAbstractClass();
|
|
|
|
$name = 'something';
|
|
|
|
$abstractRuleMock->setName($name);
|
|
|
|
self::assertSame($name, $abstractRuleMock->getName());
|
|
}
|
|
}
|