respect-validation/tests/unit/Rules/PropertyTest.php
Henrique Moody a3c197f600
Handle names via the Named rule
Because of how the validation engine works now [1], there's no reason to
keep adding names to each rule. Instead, create a single rule that
handles naming rules with a few other accessories. This change is not
necessarily simple, but it shrinks the `Rule` interface, and it's more
aligned with how the library works right now.

Personally, I think this API is much more straightforward than the
`setName()` method, as it's way more explicit about which rule we're
naming. Because of this change, the behaviour changed slightly, but it's
for the best.

Because of this change, I managed to remove a lot of code, but
unfortunately, it's quite a big-bang commit. It would be too complicated
to make it atomic since names are an intrinsic part of the library.

[1]: 238f2d506a
2024-12-26 23:10:19 +01:00

65 lines
1.9 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\Rules\Stub;
use Respect\Validation\Test\TestCase;
use stdClass;
#[Group('rule')]
#[CoversClass(Property::class)]
final class PropertyTest extends TestCase
{
#[Test]
#[DataProvider('providerForScalarValues')]
public function itShouldAlwaysInvalidateNonObjectValues(mixed $input): void
{
self::assertInvalidInput(new Property('foo', Stub::daze()), $input);
}
#[Test]
#[DataProvider('providerForObjectsWithMissingProperties')]
public function itShouldAlwaysInvalidateMissingProperties(string $propertyName, object $object): void
{
self::assertInvalidInput(new Property($propertyName, Stub::fail(1)), $object);
}
#[Test]
#[DataProvider('providerForObjectsWithExistingProperties')]
public function itShouldValidateExistingPropertiesWithWrappedRule(string $propertyName, object $object): void
{
self::assertValidInput(new Property($propertyName, Stub::pass(1)), $object);
}
#[Test]
#[DataProvider('providerForObjectsWithExistingProperties')]
public function itShouldInvalidateExistingPropertiesWithWrappedRule(string $propertyName, object $object): void
{
self::assertInvalidInput(new Property($propertyName, Stub::fail(1)), $object);
}
#[Test]
public function itShouldValidatePropertyWithTheWrappedRule(): void
{
$object = new stdClass();
$object->foo = 'bar';
$wrapped = Stub::pass(1);
$rule = new Property('foo', $wrapped);
$rule->evaluate($object);
self::assertEquals([$object->foo], $wrapped->inputs);
}
}