respect-validation/tests/unit/Rules/Core/ReducerTest.php
Henrique Moody 137c74c5b3
Change how we trace the path of results
Currently, we’re using scalar values to trace paths. The problem with
that approach is that we can’t create a reliable hierarchy with them, as
we can’t know for sure when a path is the same for different rules. By
using an object, we can easily compare and create a parent-child
relationship with it.

While making these changes, I deemed it necessary to also create objects
to handle Name and Id, which makes the code simpler and more robust. By
having Name and Path, we can create specific stringifiers that allow us
to customise how we render those values.

I didn’t manage to make those changes atomically, which is why this
commit makes so many changes. I found myself moving back and forth, and
making all those changes at once was the best solution I found.
2025-12-20 22:19:17 +01:00

60 lines
1.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules\Core;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Test\Rules\Stub;
#[CoversClass(Reducer::class)]
final class ReducerTest extends TestCase
{
#[Test]
public function shouldWrapTheSingleRule(): void
{
$rule = Stub::any(1);
$reducer = new Reducer($rule);
$result = $reducer->evaluate(null);
self::assertSame($rule, $result->rule);
}
#[Test]
public function shouldWrapWhenThereAreMultipleRules(): void
{
$rule1 = Stub::any(1);
$rule2 = Stub::any(1);
$rule3 = Stub::any(1);
$reducer = new Reducer($rule1, $rule2, $rule3);
$result = $reducer->evaluate(null);
self::assertEquals(new AllOf($rule1, $rule2, $rule3), $result->rule);
}
#[Test]
public function shouldCreateWithTemplate(): void
{
$rule = Stub::any(1);
$template = 'This is my template';
$reducer = new Reducer($rule);
$withTemplated = $reducer->withTemplate($template);
$result = $withTemplated->evaluate(null);
self::assertSame($rule, $result->rule);
self::assertSame($template, $result->template);
}
}