mirror of
https://github.com/Respect/Validation.git
synced 2024-06-26 01:00:19 +02: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>
118 lines
2.7 KiB
PHP
118 lines
2.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;
|
|
|
|
/**
|
|
* @group rule
|
|
* @covers \Respect\Validation\Rules\Base
|
|
* @covers \Respect\Validation\Exceptions\BaseException
|
|
*/
|
|
class BaseTest extends TestCase
|
|
{
|
|
protected $object;
|
|
|
|
/**
|
|
* @dataProvider providerForBase
|
|
*/
|
|
public function testBase($base, $input): void
|
|
{
|
|
$object = new Base($base);
|
|
self::assertTrue($object->__invoke($input));
|
|
$object->check($input);
|
|
$object->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerForInvalidBase
|
|
*/
|
|
public function testInvalidBase($base, $input): void
|
|
{
|
|
$object = new Base($base);
|
|
self::assertFalse($object->__invoke($input));
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerForExceptionBase
|
|
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
|
*/
|
|
public function testExceptionBase($base, $input): void
|
|
{
|
|
$object = new Base($base);
|
|
self::assertTrue($object->__invoke($input));
|
|
$object->assert($input);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerForCustomBase
|
|
*/
|
|
public function testCustomBase($base, $custom, $input): void
|
|
{
|
|
$object = new Base($base, $custom);
|
|
self::assertTrue($object->__invoke($input));
|
|
$object->check($input);
|
|
$object->assert($input);
|
|
}
|
|
|
|
public function providerForBase()
|
|
{
|
|
return [
|
|
[2, '011010001'],
|
|
[3, '0120122001'],
|
|
[8, '01234567520'],
|
|
[16, '012a34f5675c20d'],
|
|
[20, '012ah34f5675hic20dj'],
|
|
[50, '012ah34f56A75FGhic20dj'],
|
|
[62, 'Z01xSsg5675hic20dj'],
|
|
];
|
|
}
|
|
|
|
public function providerForInvalidBase()
|
|
{
|
|
return [
|
|
[2, ''],
|
|
[3, ''],
|
|
[8, ''],
|
|
[16, ''],
|
|
[20, ''],
|
|
[50, ''],
|
|
[62, ''],
|
|
[2, '01210103001'],
|
|
[3, '0120125f2001'],
|
|
[8, '01234dfZ567520'],
|
|
[16, '012aXS34f5675c20d'],
|
|
[20, '012ahZX34f5675hic20dj'],
|
|
[50, '012ahGZ34f56A75FGhic20dj'],
|
|
[61, 'Z01xSsg5675hic20dj'],
|
|
];
|
|
}
|
|
|
|
public function providerForCustomBase()
|
|
{
|
|
return [
|
|
[2, 'xy', 'xyyxyxxy'],
|
|
[3, 'pfg', 'gfpffp'],
|
|
];
|
|
}
|
|
|
|
public function providerForExceptionBase()
|
|
{
|
|
return [
|
|
[63, '01210103001'],
|
|
[125, '0120125f2001'],
|
|
];
|
|
}
|
|
}
|