2014-05-07 05:51:54 +02:00
|
|
|
<?php
|
|
|
|
|
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);
|
|
|
|
|
2014-05-07 05:51:54 +02:00
|
|
|
namespace Respect\Validation;
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2018-01-06 14:49:38 +01:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Exceptions\InvalidClassException;
|
|
|
|
use Respect\Validation\Test\Rules\AbstractClass;
|
|
|
|
use Respect\Validation\Test\Rules\Invalid;
|
|
|
|
use Respect\Validation\Test\Rules\Stub;
|
|
|
|
use Respect\Validation\Test\Rules\Valid;
|
2018-12-05 08:36:38 +01:00
|
|
|
use Respect\Validation\Test\TestCase;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
use function sprintf;
|
2016-10-30 20:07:28 +01:00
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Group('core')]
|
|
|
|
#[CoversClass(Factory::class)]
|
2018-01-06 14:49:38 +01:00
|
|
|
final class FactoryTest extends TestCase
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
private const TEST_RULES_NAMESPACE = 'Respect\\Validation\\Test\\Rules';
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2019-02-07 20:13:01 +01:00
|
|
|
public function shouldCreateRuleByNameBasedOnNamespace(): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())
|
|
|
|
->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2014-05-07 05:51:54 +02:00
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
self::assertInstanceOf(Valid::class, $factory->rule('valid'));
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldLookUpToAllNamespacesUntilRuleIsFound(): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())
|
|
|
|
->withRuleNamespace(__NAMESPACE__)
|
|
|
|
->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
self::assertInstanceOf(Valid::class, $factory->rule('valid'));
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2019-02-07 20:13:01 +01:00
|
|
|
public function shouldDefineConstructorArgumentsWhenCreatingRule(): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
$constructorArguments = [true, false, true, false];
|
|
|
|
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2019-02-04 00:29:13 +01:00
|
|
|
/** @var Stub $rule */
|
2018-01-06 14:49:38 +01:00
|
|
|
$rule = $factory->rule('stub', $constructorArguments);
|
|
|
|
|
|
|
|
self::assertSame($constructorArguments, $rule->validations);
|
2016-02-16 05:21:17 +01:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldThrowsAnExceptionWhenRuleIsInvalid(): void
|
2016-02-16 05:21:17 +01:00
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
$this->expectException(InvalidClassException::class);
|
|
|
|
$this->expectExceptionMessage(sprintf('"%s" must be an instance of "%s"', Invalid::class, Validatable::class));
|
|
|
|
|
|
|
|
$factory->rule('invalid');
|
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldThrowsAnExceptionWhenRuleIsNotInstantiable(): void
|
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
$this->expectException(InvalidClassException::class);
|
|
|
|
$this->expectExceptionMessage(sprintf('"%s" must be instantiable', AbstractClass::class));
|
|
|
|
|
|
|
|
$factory->rule('abstractClass');
|
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldThrowsAnExceptionWhenRuleIsNotFound(): void
|
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = (new Factory())->withRuleNamespace(self::TEST_RULES_NAMESPACE);
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
$this->expectException(ComponentException::class);
|
2024-03-19 01:24:04 +01:00
|
|
|
$this->expectExceptionMessage('"nonExistingRule" is not a valid rule name');
|
2018-01-06 14:49:38 +01:00
|
|
|
|
2024-03-19 01:24:04 +01:00
|
|
|
$factory->rule('nonExistingRule');
|
2018-01-06 14:49:38 +01:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldAlwaysReturnTheSameDefaultInstance(): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
self::assertSame(Factory::getDefaultInstance(), Factory::getDefaultInstance());
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-01-06 14:49:38 +01:00
|
|
|
public function shouldBeAbleToOverwriteDefaultInstance(): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2019-05-12 00:54:33 +02:00
|
|
|
$factory = new Factory();
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
$defaultInstance = Factory::getDefaultInstance();
|
|
|
|
|
|
|
|
Factory::setDefaultInstance($factory);
|
|
|
|
|
|
|
|
self::assertSame($factory, Factory::getDefaultInstance());
|
|
|
|
|
|
|
|
Factory::setDefaultInstance($defaultInstance);
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
}
|