mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
I've moved almost all the code for placeholder replacement and parameter modifiers into an external library called Respect\StringFormatter. This approach allows us to evolve the template capabilities without making major changes to the Validation's code. This commit will introduce another dependency, `respect/string-formatter`, and will upgrade the version of `respect/string-formatter`, which simplifies our internal API greatly. While making this change, I also updated how we generate exceptions. Instead of rendering the full message and the array of messages, we delegate that creation to the `ResultQuery`, which improves performance because we don’t need to render those big messages unless the user actually needs them.
147 lines
4.5 KiB
PHP
147 lines
4.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Parameters;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Name;
|
|
use Respect\Validation\Path;
|
|
use Respect\Validation\Test\Builders\ResultBuilder;
|
|
use Respect\Validation\Test\Message\TestingHandler;
|
|
use Respect\Validation\Test\TestCase;
|
|
use stdClass;
|
|
|
|
use function sprintf;
|
|
|
|
#[CoversClass(ResultHandler::class)]
|
|
final class ResultHandlerTest extends TestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('providerForNonSubjectValues')]
|
|
public function itShouldNotStringifyWhenValueIsNotAnInstanceOfSubject(mixed $value): void
|
|
{
|
|
$handler = new ResultHandler(new TestingHandler());
|
|
|
|
self::assertNull($handler->handle($value, 0));
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyInputWhenPathAndNameAreNull(): void
|
|
{
|
|
$input = ['test' => 'value'];
|
|
$result = (new ResultBuilder())->input($input)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$expected = $testingHandler->handle($input, 0);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyPathWhenNameIsNull(): void
|
|
{
|
|
$path = new Path('field1');
|
|
$result = (new ResultBuilder())->path($path)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$expected = $testingHandler->handle($path, 0);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyNameWhenPathIsNull(): void
|
|
{
|
|
$name = new Name('field_name');
|
|
$result = (new ResultBuilder())->name($name)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$expected = $testingHandler->handle($name, 0);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyNameWhenNameHasPrecedence(): void
|
|
{
|
|
$path = new Path('field1');
|
|
$name = new Name('field_name');
|
|
$result = (new ResultBuilder())->name($name)->path($path)->hasPrecedentName(true)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$expected = $testingHandler->handle($name, 0);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyWithPathAndNameWhenNameHasNoPrecedence(): void
|
|
{
|
|
$path = new Path('field1');
|
|
$name = new Name('field_name');
|
|
$result = (new ResultBuilder())->name($name)->path($path)->hasPrecedentName(false)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$pathString = $testingHandler->handle($path, 0);
|
|
$nameString = $testingHandler->handle($name, 0);
|
|
$expected = sprintf('%s (<- %s)', $pathString, $nameString);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldStringifyWithNestedPathWhenNameHasNoPrecedence(): void
|
|
{
|
|
$path1 = new Path('field1');
|
|
$path2 = new Path('field2', $path1);
|
|
$name = new Name('field_name');
|
|
$result = (new ResultBuilder())->name($name)->path($path2)->hasPrecedentName(false)->build();
|
|
|
|
$testingHandler = new TestingHandler();
|
|
$handler = new ResultHandler($testingHandler);
|
|
|
|
$pathString = $testingHandler->handle($path2, 0);
|
|
$nameString = $testingHandler->handle($name, 0);
|
|
$expected = sprintf('%s (<- %s)', $pathString, $nameString);
|
|
$actual = $handler->handle($result, 0);
|
|
|
|
self::assertSame($expected, $actual);
|
|
}
|
|
|
|
/** @return array<string, array{mixed}> */
|
|
public static function providerForNonSubjectValues(): array
|
|
{
|
|
return [
|
|
'string' => ['test'],
|
|
'integer' => [123],
|
|
'boolean' => [true],
|
|
'array' => [['test']],
|
|
'object' => [new stdClass()],
|
|
'null' => [null],
|
|
];
|
|
}
|
|
}
|