respect-validation/tests/unit/Exceptions/NestedValidationExceptionTest.php
Henrique Moody dd896bb12d
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>
2024-01-29 23:17:27 +01:00

67 lines
2.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
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)]
final class NestedValidationExceptionTest extends TestCase
{
#[Test]
public function getChildrenShouldReturnExceptionAddedByAddRelated(): void
{
$composite = $this->createNestedValidationException();
$node = $this->createValidationException();
$composite->addChild($node);
self::assertCount(1, $composite->getChildren());
self::assertContainsOnly(IntValException::class, $composite->getChildren());
}
#[Test]
public function addingTheSameInstanceShouldAddJustOneSingleReference(): void
{
$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())
);
}
}