mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
I've noticed that the `StandardFormatter` was quite bloated, which made it difficult to maintain. Understanding what each method was doing was quite complicated. Besides, the name "Standard" doesn't mean anything, because it doesn't say what the implementation does. I split the `Formatter` into two different interfaces: `StringFormatter` and `ArrayFormatter`, and I moved some code around: * `StandardFormatter::main()` -> `FirstResultStringFormatter` * `StandardFormatter::full()` -> `NestedListStringFormatter` * `StandardFormatter::array()` -> `NestedArrayFormatter` That opens up new ways of handling error messages, potentially introducing features like `JsonStringFormatter` or `FlatArrayFormatter` in the future. While working on this, I removed a significant amount of unnecessary code, which also improved my overall understanding of those formatters. I'm not very happy with all the methods in `ValidatorDefaults`, but I will refactor that later.
87 lines
2.8 KiB
PHP
87 lines
2.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Formatter;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
use Respect\Validation\Test\Builders\ResultBuilder;
|
|
use Respect\Validation\Test\TestCase;
|
|
use stdClass;
|
|
|
|
#[CoversClass(TemplateResolver::class)]
|
|
final class TemplateResolverTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function itShouldReturnResultWithTemplateWhenKeyExists(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$templates = ['foo-path' => 'My custom template'];
|
|
$sut = new TemplateResolver();
|
|
$newResult = $sut->resolve($result, $templates);
|
|
|
|
self::assertNotSame($result, $newResult);
|
|
self::assertSame('My custom template', $newResult->template);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldThrowExceptionWhenTemplateIsNotString(): void
|
|
{
|
|
$this->expectException(ComponentException::class);
|
|
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$templates = ['foo-path' => new stdClass()];
|
|
$sut = new TemplateResolver();
|
|
$sut->resolve($result, $templates);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldReturnTrueForIsFinalTemplateWhenTemplateIsString(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$templates = ['foo-path' => 'My custom template'];
|
|
$sut = new TemplateResolver();
|
|
|
|
self::assertTrue($sut->hasMatch($result, $templates));
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldReturnFalseForIsFinalTemplateWhenTemplateIsNotString(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$templates = ['foo-path' => ['my-template']];
|
|
$sut = new TemplateResolver();
|
|
|
|
self::assertFalse($sut->hasMatch($result, $templates));
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldSelectSubTemplatesWhenKeyExistsAndIsArray(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$subTemplates = ['sub' => 'template'];
|
|
$templates = ['foo-path' => $subTemplates];
|
|
$sut = new TemplateResolver();
|
|
$selected = $sut->selectMatches($result, $templates);
|
|
|
|
self::assertSame($subTemplates, $selected);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldReturnOriginalTemplatesWhenKeyDoesNotExist(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath('foo-path')->build();
|
|
$templates = ['bar-path' => ['sub' => 'template']];
|
|
$sut = new TemplateResolver();
|
|
$selected = $sut->selectMatches($result, $templates);
|
|
|
|
self::assertSame($templates, $selected);
|
|
}
|
|
}
|