mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 23:59:51 +01: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>
89 lines
2.5 KiB
PHP
89 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(PropertyOptional::class)]
|
|
final class PropertyOptionalTest extends TestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('providerForScalarValues')]
|
|
public function itShouldAlwaysValidateNonObjectValues(mixed $input): void
|
|
{
|
|
self::assertValidInput(new PropertyOptional('foo', Stub::daze()), $input);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForObjectsWithMissingProperties')]
|
|
public function itShouldAlwaysValidateMissingProperties(string $propertyName, object $object): void
|
|
{
|
|
self::assertValidInput(new PropertyOptional($propertyName, Stub::daze()), $object);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForObjectsWithExistingProperties')]
|
|
public function itShouldValidateExistingPropertiesWithWrappedRule(string $propertyName, object $object): void
|
|
{
|
|
self::assertValidInput(new PropertyOptional($propertyName, Stub::pass(1)), $object);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForObjectsWithExistingProperties')]
|
|
public function itShouldInvalidateExistingPropertiesWithWrappedRule(string $propertyName, object $object): void
|
|
{
|
|
self::assertInvalidInput(new PropertyOptional($propertyName, Stub::fail(1)), $object);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldValidatePropertyWithTheWrappedRule(): void
|
|
{
|
|
$object = new stdClass();
|
|
$object->foo = 'bar';
|
|
|
|
$wrapped = Stub::pass(1);
|
|
|
|
$rule = new PropertyOptional('foo', $wrapped);
|
|
$rule->evaluate($object);
|
|
|
|
self::assertEquals([$object->foo], $wrapped->inputs);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldUpdateWrappedRuleNameWithTheGivenName(): void
|
|
{
|
|
$property = 'foo';
|
|
$wrapped = Stub::daze();
|
|
|
|
new PropertyOptional($property, $wrapped);
|
|
|
|
self::assertEquals($property, $wrapped->getName());
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldNotUpdateWrappedRuleNameWithTheGivenNameWhenRuleAlreadyHasName(): void
|
|
{
|
|
$name = 'bar';
|
|
|
|
$wrapped = Stub::daze();
|
|
$wrapped->setName($name);
|
|
|
|
new PropertyOptional('foo', $wrapped);
|
|
|
|
self::assertEquals($name, $wrapped->getName());
|
|
}
|
|
}
|