respect-validation/tests/unit/FactoryTest.php
Henrique Moody 8c529c433e
Refactor ValidationException
Make the ValidationException a little bit less mutable than before. All
its dependencies are now passed into the constructor.

This commit also make the Factory pass the translator to the exceptions
allowing to define the translator before the exception gets created.
This change is not the ideal one, later I would like to not need the
Singleton from the Factory to do that, but for now it seems like a good
approach.

One more thing that this commit does is to introduce the "id" for
Exceptions. Key can be either the defined "name" or the name of the rule
that throwed the exception. This method will be handy to identify
exceptions better.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-05-27 16:12:05 +02:00

219 lines
5.9 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidClassException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Test\Exceptions\StubException;
use Respect\Validation\Test\Rules\AbstractClass;
use Respect\Validation\Test\Rules\Invalid;
use Respect\Validation\Test\Rules\Stub;
use Respect\Validation\Test\Rules\Valid;
use function sprintf;
/**
* @covers \Respect\Validation\Factory
*/
final class FactoryTest extends TestCase
{
private const TEST_RULES_NAMESPACE = 'Respect\\Validation\\Test\\Rules';
private const TEST_EXCEPTIONS_NAMESPACE = 'Respect\\Validation\\Test\\Exceptions';
/**
* @test
*/
public function shouldCreateARuleByNameBasedOnNamespace(): void
{
$factory = new Factory([self::TEST_RULES_NAMESPACE], [], 'trim');
self::assertInstanceOf(Valid::class, $factory->rule('valid'));
}
/**
* @test
*/
public function shouldLookUpToAllNamespacesUntilRuleIsFound(): void
{
$factory = new Factory([__NAMESPACE__, self::TEST_RULES_NAMESPACE], [], 'trim');
self::assertInstanceOf(Valid::class, $factory->rule('valid'));
}
/**
* @test
*/
public function shouldDefineConstructorArgumentsWhenCreatingARule(): void
{
$constructorArguments = [true, false, true, false];
$factory = new Factory([self::TEST_RULES_NAMESPACE], [], 'trim');
$rule = $factory->rule('stub', $constructorArguments);
self::assertSame($constructorArguments, $rule->validations);
}
/**
* @test
*/
public function shouldThrowsAnExceptionWhenRuleIsInvalid(): void
{
$factory = new Factory([self::TEST_RULES_NAMESPACE], [], 'trim');
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage(sprintf('"%s" must be an instance of "%s"', Invalid::class, Validatable::class));
$factory->rule('invalid');
}
/**
* @test
*/
public function shouldThrowsAnExceptionWhenRuleIsNotInstantiable(): void
{
$factory = new Factory([self::TEST_RULES_NAMESPACE], [], 'trim');
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage(sprintf('"%s" must be instantiable', AbstractClass::class));
$factory->rule('abstractClass');
}
/**
* @test
*/
public function shouldThrowsAnExceptionWhenRuleIsNotFound(): void
{
$factory = new Factory([self::TEST_RULES_NAMESPACE], [], 'trim');
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('"notFoundRule" is not a valid rule name');
$factory->rule('notFoundRule');
}
/**
* @test
*/
public function shouldCreateExceptionBasedOnRule(): void
{
$factory = new Factory([], [self::TEST_EXCEPTIONS_NAMESPACE], 'trim');
$rule = new Stub();
$input = 2;
self::assertInstanceOf(StubException::class, $factory->exception($rule, $input));
}
/**
* @test
*/
public function shouldLookUpToAllNamespacesUntilExceptionIsCreated(): void
{
$factory = new Factory([], [__NAMESPACE__, self::TEST_EXCEPTIONS_NAMESPACE], 'trim');
$rule = new Stub();
$input = 2;
self::assertInstanceOf(StubException::class, $factory->exception($rule, $input));
}
/**
* @test
*/
public function shouldCreateValidationExceptionWhenExceptionIsNotFound(): void
{
$factory = new Factory([], [], 'trim');
$input = 'input';
$rule = new Stub();
self::assertInstanceOf(ValidationException::class, $factory->exception($rule, $input));
}
/**
* @test
*/
public function shouldSetInputAsParameterOfCreatedException(): void
{
$factory = new Factory([], [self::TEST_EXCEPTIONS_NAMESPACE], 'trim');
$rule = new Stub();
$input = 2;
$exception = $factory->exception($rule, $input);
self::assertSame($input, $exception->getParam('input'));
}
/**
* @test
*/
public function shouldPassPropertiesToCreatedException(): void
{
$factory = new Factory([], [self::TEST_EXCEPTIONS_NAMESPACE], 'trim');
$validations = [true, false, true, true];
$rule = new Stub(...$validations);
$input = 2;
$exception = $factory->exception($rule, $input);
self::assertSame($validations, $exception->getParam('validations'));
}
/**
* @test
*/
public function shouldSetTemplateWhenTemplateKeyIsDefined(): void
{
$factory = new Factory([], [self::TEST_EXCEPTIONS_NAMESPACE], 'trim');
$extraParams = [
'template' => 'This is my template',
];
$validations = [true, false, true, true];
$rule = new Stub(...$validations);
$input = 2;
$exception = $factory->exception($rule, $input, $extraParams);
self::assertSame($extraParams['template'], $exception->getMessage());
}
/**
* @test
*/
public function shouldAlwaysReturnTheSameDefaultInstance(): void
{
self::assertSame(Factory::getDefaultInstance(), Factory::getDefaultInstance());
}
/**
* @test
*/
public function shouldBeAbleToOverwriteDefaultInstance(): void
{
$factory = new Factory([], [], 'trim');
$defaultInstance = Factory::getDefaultInstance();
Factory::setDefaultInstance($factory);
self::assertSame($factory, Factory::getDefaultInstance());
Factory::setDefaultInstance($defaultInstance);
}
}