respect-validation/tests/unit/NamespacedRuleFactoryTest.php
Henrique Moody 7c681fec66
Fix SPDX headers in all files
I ran the `bin/console spdx --fix` with different strategies for
different files. For most of the core classes, since they've been
drastically rebuilt, I've run it with the `git-blame` strategy, for for
the `src/Validators`, in which the API changed completely but the logic
remains the same, I use the `git-log` strategy.
2026-02-03 15:23:23 +01:00

110 lines
3.8 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Augusto Pascutti <augusto.hp@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidClassException;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\Transformers\StubTransformer;
use Respect\Validation\Test\Validators\Invalid;
use Respect\Validation\Test\Validators\MyAbstractClass;
use Respect\Validation\Test\Validators\Stub;
use Respect\Validation\Test\Validators\Valid;
use function assert;
use function sprintf;
#[Group('core')]
#[CoversClass(NamespacedValidatorFactory::class)]
final class NamespacedRuleFactoryTest extends TestCase
{
private const string TEST_RULES_NAMESPACE = 'Respect\\Validation\\Test\\Validators';
#[Test]
public function shouldCreateRuleByNameBasedOnNamespace(): void
{
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
self::assertInstanceOf(Valid::class, $factory->create('valid'));
}
#[Test]
public function shouldLookUpToAllNamespacesUntilRuleIsFound(): void
{
$factory = (new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]))
->withNamespace(__NAMESPACE__);
self::assertInstanceOf(Valid::class, $factory->create('valid'));
}
#[Test]
public function shouldDefineConstructorArgumentsWhenCreatingRule(): void
{
$constructorArguments = [true, false, true, false];
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
$validator = $factory->create('stub', $constructorArguments);
assert($validator instanceof Stub);
self::assertSame($constructorArguments, $validator->validations);
}
#[Test]
public function shouldThrowsAnExceptionOnConstructorReflectionFailure(): void
{
$constructorArguments = ['a', 'b'];
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage('"noConstructor" could not be instantiated with arguments `["a", "b"]`');
$factory->create('noConstructor', $constructorArguments);
}
#[Test]
public function shouldThrowsAnExceptionWhenRuleIsInvalid(): void
{
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage(sprintf('"%s" must be an instance of "%s"', Invalid::class, Validator::class));
$factory->create('invalid');
}
#[Test]
public function shouldThrowsAnExceptionWhenRuleIsNotInstantiable(): void
{
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage(sprintf('"%s" must be instantiable', MyAbstractClass::class));
$factory->create('myAbstractClass');
}
#[Test]
public function shouldThrowsAnExceptionWhenRuleIsNotFound(): void
{
$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('"nonExistingRule" is not a valid rule name');
$factory->create('nonExistingRule');
}
}