mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
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.
This commit is contained in:
parent
562d98d805
commit
cd6bcd470b
18 changed files with 825 additions and 131 deletions
69
tests/unit/Message/Modifier/ListOrModifierTest.php
Normal file
69
tests/unit/Message/Modifier/ListOrModifierTest.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?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\Placeholder\Listed;
|
||||
use Respect\Validation\Message\Translator\ArrayTranslator;
|
||||
use Respect\Validation\Test\Message\TestingModifier;
|
||||
use Respect\Validation\Test\TestCase;
|
||||
|
||||
#[CoversClass(ListOrModifier::class)]
|
||||
final class ListOrModifierTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function itShouldNotModifyWhenModifierIsNotListOr(): void
|
||||
{
|
||||
$translator = new ArrayTranslator(['or' => 'or']);
|
||||
$nextModifier = new TestingModifier();
|
||||
$modifier = new ListOrModifier($translator, $nextModifier);
|
||||
|
||||
$value = ['item1', 'item2'];
|
||||
$pipe = 'listAnd';
|
||||
|
||||
$result = $modifier->modify($value, $pipe);
|
||||
|
||||
self::assertSame($nextModifier->modify($value, $pipe), $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itShouldNotModifyWhenValueIsNotArray(): void
|
||||
{
|
||||
$translator = new ArrayTranslator(['or' => 'or']);
|
||||
$nextModifier = new TestingModifier();
|
||||
$modifier = new ListOrModifier($translator, $nextModifier);
|
||||
|
||||
$value = 'not an array';
|
||||
$pipe = 'listOr';
|
||||
|
||||
$result = $modifier->modify($value, $pipe);
|
||||
|
||||
self::assertSame($nextModifier->modify($value, $pipe), $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itShouldModifyWhenModifierIsListOrAndValueIsArray(): void
|
||||
{
|
||||
$translator = new ArrayTranslator(['or' => 'or']);
|
||||
$nextModifier = new TestingModifier();
|
||||
$modifier = new ListOrModifier($translator, $nextModifier);
|
||||
|
||||
$value = ['item1', 'item2', 'item3'];
|
||||
$pipe = 'listOr';
|
||||
|
||||
$result = $modifier->modify($value, $pipe);
|
||||
|
||||
$expectedValue = new Listed($value, $translator->translate('or'));
|
||||
$expected = $nextModifier->modify($expectedValue, null);
|
||||
|
||||
self::assertSame($expected, $result);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue