mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
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:
parent
04b3c78ba7
commit
e879c62f60
65
library/Rules/AbstractEnvelope.php
Normal file
65
library/Rules/AbstractEnvelope.php
Normal 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);
|
||||
}
|
||||
}
|
83
tests/unit/Rules/AbstractEnvelopeTest.php
Normal file
83
tests/unit/Rules/AbstractEnvelopeTest.php
Normal 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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue