mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
Currently, defining translations is quite cumbersome, and the translator callback is passed to the constructor of multiple classes, which makes it quite ugly and could make translations inconsistent. This commit completely changes how translations are done in Validation. Instead of using a callback, it uses a specific class, and `Validator` will pass that object through the objects that render the messages.
47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Translator;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
#[CoversClass(ArrayTranslator::class)]
|
|
final class ArrayTranslatorTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function shouldReturnOriginalMessageWhenCannotFindTranslation(): void
|
|
{
|
|
$translator = new ArrayTranslator([]);
|
|
$message = 'This is a test message';
|
|
|
|
self::assertSame($message, $translator->translate($message));
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldReturnTranslatedMessage(): void
|
|
{
|
|
$messages = ['foo' => 'bar'];
|
|
|
|
$translator = new ArrayTranslator($messages);
|
|
|
|
self::assertSame($messages['foo'], $translator->translate('foo'));
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldReturnOriginalMessageWhenTranslationIsNotString(): void
|
|
{
|
|
$messages = ['foo' => 123];
|
|
|
|
$translator = new ArrayTranslator($messages);
|
|
|
|
self::assertSame('foo', $translator->translate('foo'));
|
|
}
|
|
}
|