Create "AbstractEnvelope" class

This abstract class is very similar to "AbstractWrapper" the difference
is that "AbstractWapper" will throw the exceptions of the rule that is
defined inside it, while "AbstractEnvelope" uses the exception of the
rule that extends it.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-22 17:15:32 +01:00
parent 04b3c78ba7
commit e879c62f60
2 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,65 @@
<?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\Rules;
use Respect\Validation\Validatable;
/**
* Abstract class that creates an envelope around another rule.
*
* This class is usefull when you want to create rules that use other rules, but
* having an custom message.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
abstract class AbstractEnvelope extends AbstractRule
{
/**
* @var Validatable
*/
private $validatable;
/**
* @var array
*/
private $parameters;
/**
* Initializes the rule.
*
* @param Validatable $validatable
* @param array $parameters
*/
public function __construct(Validatable $validatable, array $parameters)
{
$this->validatable = $validatable;
$this->parameters = $parameters;
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return $this->validatable->validate($input);
}
/**
* {@inheritdoc}
*/
public function reportError($input, array $extraParameters = [])
{
return parent::reportError($input, $extraParameters + $this->parameters);
}
}

View file

@ -0,0 +1,83 @@
<?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\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Validatable;
/**
* @test core
*
* @covers \Respect\Validation\Rules\AbstractEnvelope
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class AbstractEnvelopeTest extends TestCase
{
/**
* @test
*/
public function itShouldValidateUsingTheInnerRule(): void
{
$input = 'value';
$innerRule = $this->createMock(Validatable::class);
$innerRule
->expects($this->once())
->method('validate')
->with($input)
->willReturn(true);
$rule = $this->getMockForAbstractClass(AbstractEnvelope::class, [$innerRule, []]);
self::assertTrue($rule->validate($input));
}
/**
* @test
*/
public function itShouldInvalidateUsingTheInnerRule(): void
{
$input = 'value';
$innerRule = $this->createMock(Validatable::class);
$innerRule
->expects($this->once())
->method('validate')
->with($input)
->willReturn(false);
$rule = $this->getMockForAbstractClass(AbstractEnvelope::class, [$innerRule, []]);
self::assertFalse($rule->validate($input));
}
/**
* @test
*/
public function itShouldReportErrorUringProperties(): void
{
$input = 'value';
$parameters = ['foo' => true, 'bar' => false, 'baz' => 42];
$rule = $this->getMockForAbstractClass(
AbstractEnvelope::class,
[$this->createMock(Validatable::class), $parameters]
);
$exception = $rule->reportError($input);
self::assertArraySubset($parameters, $exception->getParams());
}
}