mirror of
https://github.com/Respect/Validation.git
synced 2024-06-26 17:20:16 +02:00
Currently, the Property rule has a third parameter that allows the validation of the wrapped rule to be optional, meaning that the validation will only happen if the property exists. That parameter makes the rule harder to understand at times. I'm splitting the Property rule into Property, PropertyExists, and PropertyOptional. That way, it becomes apparent when someone wants only to validate whether a property exists or if they will validate the value of the property only when it exists. I deliberately didn't create an abstract class because those rules are different enough not to have an abstraction. In fact, I can see myself deleting the AbstractRelated after I refactor the KeyNested rule. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
90 lines
2.5 KiB
PHP
90 lines
2.5 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);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldUpdateWrappedRuleNameWithTheGivenName(): void
|
|
{
|
|
$property = 'foo';
|
|
$wrapped = Stub::daze();
|
|
|
|
new Property($property, $wrapped);
|
|
|
|
self::assertEquals($property, $wrapped->getName());
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldNotUpdateWrappedRuleNameWithTheGivenNameWhenRuleAlreadyHasName(): void
|
|
{
|
|
$name = 'bar';
|
|
|
|
$wrapped = Stub::daze();
|
|
$wrapped->setName($name);
|
|
|
|
new Property('foo', $wrapped);
|
|
|
|
self::assertEquals($name, $wrapped->getName());
|
|
}
|
|
}
|