respect-validation/tests/unit/Message/Modifier/TransModifierTest.php
Henrique Moody cd6bcd470b
Enable adding modifiers without changing InterpolationRenderer
The `InterpolationRenderer` was violating the open-closed principle,
because every time we would want to add a new modifier, we would need to
change its implementation.

This commit changes that behaviour by creating a `Modifier` interface.
The classes implementing that interface are using a chain of
responsibility to pass the data to the next one. Using a chain of
responsibility makes a lot of sense, since it's only possible to have
one modifier at a time.
2025-12-30 11:17:11 +01:00

83 lines
2.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Message\Modifier;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Message\Translator\ArrayTranslator;
use Respect\Validation\Test\Message\TestingModifier;
use Respect\Validation\Test\TestCase;
#[CoversClass(TransModifier::class)]
final class TransModifierTest extends TestCase
{
#[Test]
public function itShouldNotModifyWhenModifierIsNotTrans(): void
{
$translator = new ArrayTranslator(['message' => 'translated message']);
$nextModifier = new TestingModifier();
$modifier = new TransModifier($translator, $nextModifier);
$value = 'message';
$pipe = 'notTrans';
$result = $modifier->modify($value, $pipe);
self::assertSame($nextModifier->modify($value, $pipe), $result);
}
#[Test]
public function itShouldNotModifyWhenValueIsNotString(): void
{
$translator = new ArrayTranslator(['message' => 'translated message']);
$nextModifier = new TestingModifier();
$modifier = new TransModifier($translator, $nextModifier);
$value = ['not', 'a', 'string'];
$pipe = 'trans';
$result = $modifier->modify($value, $pipe);
self::assertSame($nextModifier->modify($value, $pipe), $result);
}
#[Test]
public function itShouldModifyWhenModifierIsTransAndValueIsString(): void
{
$key = 'message';
$translatedMessage = 'translated message';
$translator = new ArrayTranslator([$key => $translatedMessage]);
$nextModifier = new TestingModifier();
$modifier = new TransModifier($translator, $nextModifier);
$value = $key;
$pipe = 'trans';
$result = $modifier->modify($value, $pipe);
self::assertSame($translatedMessage, $result);
}
#[Test]
public function itShouldReturnKeyWhenTranslationNotFound(): void
{
$key = 'nonexistent';
$translator = new ArrayTranslator(['message' => 'translated message']);
$nextModifier = new TestingModifier();
$modifier = new TransModifier($translator, $nextModifier);
$value = $key;
$pipe = 'trans';
$result = $modifier->modify($value, $pipe);
self::assertSame($key, $result);
}
}