2010-09-27 23:02:30 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-09-27 23:02:30 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-03-03 21:17:22 +01:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2024-01-28 01:46:21 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
2024-03-03 21:17:22 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-02-04 19:04:48 +01:00
|
|
|
use Respect\Validation\Test\Rules\Stub;
|
2024-03-03 21:17:22 +01:00
|
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
use stdClass;
|
2017-11-04 11:21:40 +01:00
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Group('rule')]
|
2023-03-22 21:36:57 +01:00
|
|
|
#[CoversClass(Property::class)]
|
2024-03-03 21:17:22 +01:00
|
|
|
final class PropertyTest extends TestCase
|
2010-09-27 23:02:30 +02:00
|
|
|
{
|
2024-03-03 21:17:22 +01:00
|
|
|
#[Test]
|
|
|
|
#[DataProvider('providerForScalarValues')]
|
|
|
|
public function itShouldAlwaysInvalidateNonObjectValues(mixed $input): void
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
2024-03-03 21:17:22 +01:00
|
|
|
self::assertInvalidInput(new Property('foo', Stub::daze()), $input);
|
2010-10-18 07:41:22 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 21:17:22 +01:00
|
|
|
#[Test]
|
|
|
|
#[DataProvider('providerForObjectsWithMissingProperties')]
|
|
|
|
public function itShouldAlwaysInvalidateMissingProperties(string $propertyName, object $object): void
|
2015-10-07 05:54:14 +02:00
|
|
|
{
|
2024-03-03 21:17:22 +01:00
|
|
|
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());
|
2010-11-30 17:21:33 +01:00
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
}
|