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>
This commit is contained in:
Henrique Moody 2018-05-24 07:47:09 +02:00
commit 8c529c433e
No known key found for this signature in database
GPG key ID: 221E9281655813A6
35 changed files with 385 additions and 285 deletions

View file

@ -15,20 +15,12 @@ namespace Respect\Validation\Exceptions;
use PHPUnit\Framework\TestCase;
/**
* phpunit has an issue with mocking exceptions when in HHVM:
* https://github.com/sebastianbergmann/phpunit-mock-objects/issues/207.
*/
class PrivateNestedValidationException extends NestedValidationException
{
}
class NestedValidationExceptionTest extends TestCase
{
public function testGetRelatedShouldReturnExceptionAddedByAddRelated(): void
{
$composite = new AttributeException();
$node = new IntValException();
$composite = new AttributeException('input', 'id', [], 'trim');
$node = new IntValException('input', 'id', [], 'trim');
$composite->addRelated($node);
self::assertEquals(1, count($composite->getRelated(true)));
self::assertContainsOnly($node, $composite->getRelated());
@ -36,8 +28,8 @@ class NestedValidationExceptionTest extends TestCase
public function testAddingTheSameInstanceShouldAddJustASingleReference(): void
{
$composite = new AttributeException();
$node = new IntValException();
$composite = new AttributeException('input', 'id', [], 'trim');
$node = new IntValException('input', 'id', [], 'trim');
$composite->addRelated($node);
$composite->addRelated($node);
$composite->addRelated($node);