respect-validation/tests/unit/Message/StandardFormatterTest.php
Henrique Moody 238f2d506a
Update validation engine
There are a few "problems" with the current engine:

- Allowing each rule to execute assert() and check() means duplication
  in some cases.

- Because we use exceptions to assert/check, we can only invert a
  validation (with Not) if there are errors. That means that we have
  limited granularity control.

- There is a lot of logic in the exceptions. That means that even after
  it throws an exception, something could still happen. We're stable on
  that front, but I want to simplify them. Besides, debugging exception
  code is painful because the stack trace does not go beyond the
  exception.

Apart from that, there are many limitations with templating, and working
that out in the current implementation makes it much harder.

These changes will improve the library in many aspects, but they will
also change the behavior and break backward compatibility. However,
that's a price I'm willing to pay for the improvements we'll have.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-22 16:54:44 +01:00

109 lines
3.6 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Message;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Message\StandardFormatter\ArrayProvider;
use Respect\Validation\Message\StandardFormatter\FullProvider;
use Respect\Validation\Message\StandardFormatter\MainProvider;
use Respect\Validation\Result;
use Respect\Validation\Test\Builders\ResultBuilder;
use Respect\Validation\Test\Message\TestingMessageRenderer;
use Respect\Validation\Test\TestCase;
use stdClass;
use function Respect\Stringifier\stringify;
use function sprintf;
#[CoversClass(StandardFormatter::class)]
final class StandardFormatterTest extends TestCase
{
use ArrayProvider;
use FullProvider;
use MainProvider;
/** @param array<string, mixed> $templates */
#[Test]
#[DataProvider('provideForMain')]
public function itShouldFormatMainMessage(Result $result, string $expected, array $templates = []): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
self::assertSame($expected, $renderer->main($result, $templates));
}
#[Test]
public function itShouldThrowAnExceptionWhenTryingToFormatAsMainAndTemplateIsInvalid(): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
$result = (new ResultBuilder())->id('foo')->build();
$template = new stdClass();
$this->expectException(ComponentException::class);
$this->expectExceptionMessage(sprintf('Template for "foo" must be a string, %s given', stringify($template)));
$renderer->main($result, ['foo' => $template]);
}
/** @param array<string, mixed> $templates */
#[Test]
#[DataProvider('provideForFull')]
public function itShouldFormatFullMessage(Result $result, string $expected, array $templates = []): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
self::assertSame($expected, $renderer->full($result, $templates));
}
#[Test]
public function itShouldThrowAnExceptionWhenTryingToFormatAsFullAndTemplateIsInvalid(): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
$result = (new ResultBuilder())->id('foo')->build();
$template = new stdClass();
$this->expectException(ComponentException::class);
$this->expectExceptionMessage(sprintf('Template for "foo" must be a string, %s given', stringify($template)));
$renderer->full($result, ['foo' => $template]);
}
/**
* @param array<string, mixed> $expected
* @param array<string, mixed> $templates
*/
#[Test]
#[DataProvider('provideForArray')]
public function itShouldFormatArrayMessage(Result $result, array $expected, array $templates = []): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
self::assertSame($expected, $renderer->array($result, $templates));
}
#[Test]
public function itShouldThrowAnExceptionWhenTryingToFormatAsArrayAndTemplateIsInvalid(): void
{
$renderer = new StandardFormatter(new TestingMessageRenderer());
$result = (new ResultBuilder())->id('foo')->build();
$template = new stdClass();
$this->expectException(ComponentException::class);
$this->expectExceptionMessage(sprintf('Template for "foo" must be a string, %s given', stringify($template)));
$renderer->array($result, ['foo' => $template]);
}
}