mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
The `Id`, `Name`, and `Path` value objects are not only message-related concerns, they're part of the core of the library, hence it makes sense to place them at the root namespace.
88 lines
2.9 KiB
PHP
88 lines
2.9 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\Path;
|
|
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(new Path('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(new Path('foo-path'))->build();
|
|
$templates = ['foo-path' => new stdClass()];
|
|
$sut = new TemplateResolver();
|
|
$sut->resolve($result, $templates);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldReturnTrueForIsFinalTemplateWhenTemplateIsString(): void
|
|
{
|
|
$result = (new ResultBuilder())->withPath(new Path('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(new Path('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(new Path('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(new Path('foo-path'))->build();
|
|
$templates = ['bar-path' => ['sub' => 'template']];
|
|
$sut = new TemplateResolver();
|
|
$selected = $sut->selectMatches($result, $templates);
|
|
|
|
self::assertSame($templates, $selected);
|
|
}
|
|
}
|