Move template definitions to the rules

It's easier to identify the reason for choosing a specific message in
the rule than in the exception. The same goes for the key we use to
determine the templates.

This change will simplify the `ValidationException` because it will
already receive the template it needs to use. As a consequence, the
`Factory` also becomes more straightforward.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-01-29 17:29:01 +01:00
commit dd896bb12d
No known key found for this signature in database
GPG key ID: 221E9281655813A6
181 changed files with 1042 additions and 680 deletions

View file

@ -15,6 +15,7 @@ use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Message\Formatter;
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Validatable;
#[Group('core')]
#[CoversClass(NestedValidationException::class)]
@ -23,8 +24,8 @@ final class NestedValidationExceptionTest extends TestCase
#[Test]
public function getChildrenShouldReturnExceptionAddedByAddRelated(): void
{
$composite = new PropertyException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName()));
$node = new IntValException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName()));
$composite = $this->createNestedValidationException();
$node = $this->createValidationException();
$composite->addChild($node);
self::assertCount(1, $composite->getChildren());
self::assertContainsOnly(IntValException::class, $composite->getChildren());
@ -33,12 +34,34 @@ final class NestedValidationExceptionTest extends TestCase
#[Test]
public function addingTheSameInstanceShouldAddJustOneSingleReference(): void
{
$composite = new PropertyException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName()));
$node = new IntValException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName()));
$composite = $this->createNestedValidationException();
$node = $this->createValidationException();
$composite->addChild($node);
$composite->addChild($node);
$composite->addChild($node);
self::assertCount(1, $composite->getChildren());
self::assertContainsOnly(IntValException::class, $composite->getChildren());
}
public function createNestedValidationException(): NestedValidationException
{
return new NestedValidationException(
input: 'input',
id: 'id',
params: [],
template: Validatable::TEMPLATE_STANDARD,
formatter: new Formatter('strval', new KeepOriginalStringName())
);
}
public function createValidationException(): IntValException
{
return new IntValException(
input: 'input',
id: 'id',
params: [],
template: Validatable::TEMPLATE_STANDARD,
formatter: new Formatter('strval', new KeepOriginalStringName())
);
}
}