mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
81befe8fa1
That helps organize the code better, making it easier to spot the core rule. It also helps not allow the Factory to load those rules, as the new namespace is not registered in it. Note that the "AbstractAge", "AbstractRelated", and "AbstractRule" were not moved. I want to do that only when I refactor them. After I moved classes, I realized that "Comparison" and "FilteredString" had no tests. I created the tests, and while I did that, I spotted two bugs: * The "Equals" rule was failing when comparing non-scalar wth scalar values; * The "Equals" and "Identical" rules were not working correctly because "Comparison" was converting their values. I fixed those bugs in this commit. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
85 lines
2.3 KiB
PHP
85 lines
2.3 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\Group;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Test\Rules\Core\ConcreteComposite;
|
|
use Respect\Validation\Test\Rules\Stub;
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
#[Group('core')]
|
|
#[CoversClass(Composite::class)]
|
|
final class CompositeTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function itShouldReturnItsChildren(): void
|
|
{
|
|
$expected = [Stub::daze(), Stub::daze(), Stub::daze()];
|
|
$sut = new ConcreteComposite(...$expected);
|
|
$actual = $sut->getRules();
|
|
|
|
self::assertCount(3, $actual);
|
|
self::assertEquals($expected, $actual);
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldDefineAndRetrieveTemplate(): void
|
|
{
|
|
$template = 'This is a template';
|
|
|
|
$sut = new ConcreteComposite(Stub::daze(), Stub::daze());
|
|
$sut->setTemplate($template);
|
|
|
|
self::assertEquals($template, $sut->getTemplate());
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldUpdateTheNameOfTheChildWhenUpdatingItsName(): void
|
|
{
|
|
$ruleName = 'something';
|
|
|
|
$rule1 = Stub::daze();
|
|
$rule2 = Stub::daze();
|
|
|
|
$composite = new ConcreteComposite($rule1, $rule2);
|
|
|
|
self::assertNull($rule1->getName());
|
|
self::assertNull($rule2->getName());
|
|
|
|
$composite->setName($ruleName);
|
|
|
|
self::assertEquals($ruleName, $rule1->getName());
|
|
self::assertEquals($ruleName, $rule2->getName());
|
|
self::assertEquals($ruleName, $composite->getName());
|
|
}
|
|
|
|
#[Test]
|
|
public function itShouldNotUpdateTheNameOfTheChildWhenUpdatingItsNameIfTheChildAlreadyHasSomeName(): void
|
|
{
|
|
$ruleName1 = 'something';
|
|
$ruleName2 = 'something else';
|
|
|
|
$rule1 = Stub::daze();
|
|
$rule1->setName($ruleName1);
|
|
|
|
$rule2 = Stub::daze();
|
|
$rule2->setName($ruleName1);
|
|
|
|
$composite = new ConcreteComposite($rule1, $rule2);
|
|
$composite->setName($ruleName2);
|
|
|
|
self::assertEquals($ruleName1, $rule1->getName());
|
|
self::assertEquals($ruleName1, $rule2->getName());
|
|
self::assertEquals($ruleName2, $composite->getName());
|
|
}
|
|
}
|