From e341fef5c072d51f8bfa178af219915a1d10ec79 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 12 Feb 2024 22:23:48 +0100 Subject: [PATCH] Update the validation engine of envelop-based rules Signed-off-by: Henrique Moody --- library/Rules/AbstractEnvelope.php | 45 ------------------- library/Rules/Between.php | 2 +- library/Rules/ContainsAny.php | 2 +- library/Rules/Envelope.php | 29 ++++++++++++ library/Rules/FilterVar.php | 2 +- library/Rules/HexRgbColor.php | 2 +- library/Rules/LanguageCode.php | 2 +- library/Rules/No.php | 2 +- library/Rules/PostalCode.php | 2 +- library/Rules/Roman.php | 2 +- library/Rules/Url.php | 2 +- .../Rules/{Envelop.php => EnvelopStub.php} | 4 +- tests/unit/Rules/BetweenTest.php | 1 - ...tractEnvelopeTest.php => EnvelopeTest.php} | 22 ++++----- tests/unit/Rules/FilterVarTest.php | 1 - tests/unit/Rules/LanguageCodeTest.php | 1 - tests/unit/Rules/UrlTest.php | 1 - 17 files changed, 51 insertions(+), 71 deletions(-) delete mode 100644 library/Rules/AbstractEnvelope.php create mode 100644 library/Rules/Envelope.php rename tests/library/Rules/{Envelop.php => EnvelopStub.php} (66%) rename tests/unit/Rules/{AbstractEnvelopeTest.php => EnvelopeTest.php} (58%) diff --git a/library/Rules/AbstractEnvelope.php b/library/Rules/AbstractEnvelope.php deleted file mode 100644 index 7ef57ef5..00000000 --- a/library/Rules/AbstractEnvelope.php +++ /dev/null @@ -1,45 +0,0 @@ - - * SPDX-License-Identifier: MIT - */ - -declare(strict_types=1); - -namespace Respect\Validation\Rules; - -use Respect\Validation\Exceptions\ValidationException; -use Respect\Validation\Result; -use Respect\Validation\Validatable; - -abstract class AbstractEnvelope extends AbstractRule -{ - /** - * @param mixed[] $parameters - */ - public function __construct( - private readonly Validatable $validatable, - private readonly array $parameters = [] - ) { - } - - public function validate(mixed $input): bool - { - return $this->validatable->validate($input); - } - - public function evaluate(mixed $input): Result - { - return (new Result($this->validatable->evaluate($input)->isValid, $input, $this)) - ->withParameters($this->parameters); - } - - /** - * @param mixed[] $extraParameters - */ - public function reportError(mixed $input, array $extraParameters = []): ValidationException - { - return parent::reportError($input, $extraParameters + $this->parameters); - } -} diff --git a/library/Rules/Between.php b/library/Rules/Between.php index d4a3bdcf..15cbae6f 100644 --- a/library/Rules/Between.php +++ b/library/Rules/Between.php @@ -17,7 +17,7 @@ use Respect\Validation\Message\Template; '{{name}} must be between {{minValue}} and {{maxValue}}', '{{name}} must not be between {{minValue}} and {{maxValue}}', )] -final class Between extends AbstractEnvelope +final class Between extends Envelope { use CanCompareValues; diff --git a/library/Rules/ContainsAny.php b/library/Rules/ContainsAny.php index b75c71dd..f59f861e 100644 --- a/library/Rules/ContainsAny.php +++ b/library/Rules/ContainsAny.php @@ -17,7 +17,7 @@ use function array_map; '{{name}} must contain at least one of the values {{needles}}', '{{name}} must not contain any of the values {{needles}}', )] -final class ContainsAny extends AbstractEnvelope +final class ContainsAny extends Envelope { /** * @param mixed[] $needles At least one of the values provided must be found in input string or array diff --git a/library/Rules/Envelope.php b/library/Rules/Envelope.php new file mode 100644 index 00000000..6472d7f4 --- /dev/null +++ b/library/Rules/Envelope.php @@ -0,0 +1,29 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules; + +use Respect\Validation\Result; +use Respect\Validation\Validatable; + +abstract class Envelope extends Standard +{ + /** @param array $parameters */ + public function __construct( + private readonly Validatable $rule, + private readonly array $parameters = [] + ) { + } + + public function evaluate(mixed $input): Result + { + return (new Result($this->rule->evaluate($input)->isValid, $input, $this)) + ->withParameters($this->parameters); + } +} diff --git a/library/Rules/FilterVar.php b/library/Rules/FilterVar.php index 24ac0a38..eba94c5a 100644 --- a/library/Rules/FilterVar.php +++ b/library/Rules/FilterVar.php @@ -30,7 +30,7 @@ use const FILTER_VALIDATE_URL; '{{name}} must be valid', '{{name}} must not be valid', )] -final class FilterVar extends AbstractEnvelope +final class FilterVar extends Envelope { private const ALLOWED_FILTERS = [ FILTER_VALIDATE_BOOLEAN => 'is_bool', diff --git a/library/Rules/HexRgbColor.php b/library/Rules/HexRgbColor.php index 62919b5f..697076e8 100644 --- a/library/Rules/HexRgbColor.php +++ b/library/Rules/HexRgbColor.php @@ -15,7 +15,7 @@ use Respect\Validation\Message\Template; '{{name}} must be a hex RGB color', '{{name}} must not be a hex RGB color', )] -final class HexRgbColor extends AbstractEnvelope +final class HexRgbColor extends Envelope { public function __construct() { diff --git a/library/Rules/LanguageCode.php b/library/Rules/LanguageCode.php index 719b2a0d..6d9939bd 100644 --- a/library/Rules/LanguageCode.php +++ b/library/Rules/LanguageCode.php @@ -21,7 +21,7 @@ use function sprintf; '{{name}} must be a valid ISO 639 {{set|raw}} language code', '{{name}} must not be a valid ISO 639 {{set|raw}} language code', )] -final class LanguageCode extends AbstractEnvelope +final class LanguageCode extends Envelope { public const ALPHA2 = 'alpha-2'; public const ALPHA3 = 'alpha-3'; diff --git a/library/Rules/No.php b/library/Rules/No.php index bf6bfaf7..6bf5eba4 100644 --- a/library/Rules/No.php +++ b/library/Rules/No.php @@ -19,7 +19,7 @@ use const NOEXPR; '{{name}} must be similar to "No"', '{{name}} must not be similar to "No"', )] -final class No extends AbstractEnvelope +final class No extends Envelope { public function __construct(bool $useLocale = false) { diff --git a/library/Rules/PostalCode.php b/library/Rules/PostalCode.php index d9c026da..ea053f9f 100644 --- a/library/Rules/PostalCode.php +++ b/library/Rules/PostalCode.php @@ -21,7 +21,7 @@ use function sprintf; '{{name}} must be a valid postal code on {{countryCode}}', '{{name}} must not be a valid postal code on {{countryCode}}', )] -final class PostalCode extends AbstractEnvelope +final class PostalCode extends Envelope { private const DEFAULT_PATTERN = '/^$/'; diff --git a/library/Rules/Roman.php b/library/Rules/Roman.php index 3d0e7715..899adb60 100644 --- a/library/Rules/Roman.php +++ b/library/Rules/Roman.php @@ -15,7 +15,7 @@ use Respect\Validation\Message\Template; '{{name}} must be a valid Roman numeral', '{{name}} must not be a valid Roman numeral', )] -final class Roman extends AbstractEnvelope +final class Roman extends Envelope { public function __construct() { diff --git a/library/Rules/Url.php b/library/Rules/Url.php index ea700c4a..06b1bc65 100644 --- a/library/Rules/Url.php +++ b/library/Rules/Url.php @@ -17,7 +17,7 @@ use const FILTER_VALIDATE_URL; '{{name}} must be a URL', '{{name}} must not be a URL', )] -final class Url extends AbstractEnvelope +final class Url extends Envelope { public function __construct() { diff --git a/tests/library/Rules/Envelop.php b/tests/library/Rules/EnvelopStub.php similarity index 66% rename from tests/library/Rules/Envelop.php rename to tests/library/Rules/EnvelopStub.php index 944ac925..7aa50cae 100644 --- a/tests/library/Rules/Envelop.php +++ b/tests/library/Rules/EnvelopStub.php @@ -9,8 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Test\Rules; -use Respect\Validation\Rules\AbstractEnvelope; +use Respect\Validation\Rules\Envelope; -final class Envelop extends AbstractEnvelope +final class EnvelopStub extends Envelope { } diff --git a/tests/unit/Rules/BetweenTest.php b/tests/unit/Rules/BetweenTest.php index a5099a0d..eb19ae9b 100644 --- a/tests/unit/Rules/BetweenTest.php +++ b/tests/unit/Rules/BetweenTest.php @@ -18,7 +18,6 @@ use Respect\Validation\Test\RuleTestCase; use Respect\Validation\Test\Stubs\CountableStub; #[Group('rule')] -#[CoversClass(AbstractEnvelope::class)] #[CoversClass(Between::class)] final class BetweenTest extends RuleTestCase { diff --git a/tests/unit/Rules/AbstractEnvelopeTest.php b/tests/unit/Rules/EnvelopeTest.php similarity index 58% rename from tests/unit/Rules/AbstractEnvelopeTest.php rename to tests/unit/Rules/EnvelopeTest.php index 15d716b6..5a94cd95 100644 --- a/tests/unit/Rules/AbstractEnvelopeTest.php +++ b/tests/unit/Rules/EnvelopeTest.php @@ -12,41 +12,41 @@ namespace Respect\Validation\Rules; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; -use Respect\Validation\Test\Rules\Envelop; +use Respect\Validation\Test\Rules\EnvelopStub; use Respect\Validation\Test\Rules\Stub; use Respect\Validation\Test\TestCase; use function array_intersect_key; #[Group('core')] -#[CoversClass(AbstractEnvelope::class)] -final class AbstractEnvelopeTest extends TestCase +#[CoversClass(Envelope::class)] +final class EnvelopeTest extends TestCase { #[Test] public function itShouldValidateUsingTheInnerRule(): void { - $rule = new Envelop(Stub::pass(1), []); + $rule = new EnvelopStub(Stub::pass(1), []); - self::assertTrue($rule->validate('something')); + self::assertTrue($rule->evaluate('something')->isValid); } #[Test] public function itShouldInvalidateUsingTheInnerRule(): void { - $rule = new Envelop(Stub::fail(1), []); + $rule = new EnvelopStub(Stub::fail(1), []); - self::assertFalse($rule->validate('something')); + self::assertFalse($rule->evaluate('something')->isValid); } #[Test] - public function itShouldReportErrorUsingProperties(): void + public function itShouldEvaluatePassingTheGivenProperties(): void { $input = 'value'; $parameters = ['foo' => true, 'bar' => false, 'baz' => 42]; - $rule = new Envelop(Stub::fail(1), $parameters); - $exception = $rule->reportError($input); + $rule = new EnvelopStub(Stub::fail(1), $parameters); + $result = $rule->evaluate($input); - self::assertEquals($parameters, array_intersect_key($parameters, $exception->getParams())); + self::assertEquals($parameters, array_intersect_key($parameters, $result->parameters)); } } diff --git a/tests/unit/Rules/FilterVarTest.php b/tests/unit/Rules/FilterVarTest.php index f7fb5711..68cf70c1 100644 --- a/tests/unit/Rules/FilterVarTest.php +++ b/tests/unit/Rules/FilterVarTest.php @@ -26,7 +26,6 @@ use const FILTER_VALIDATE_INT; use const FILTER_VALIDATE_URL; #[Group('rule')] -#[CoversClass(AbstractEnvelope::class)] #[CoversClass(FilterVar::class)] final class FilterVarTest extends RuleTestCase { diff --git a/tests/unit/Rules/LanguageCodeTest.php b/tests/unit/Rules/LanguageCodeTest.php index d2eaeff3..16bf5159 100644 --- a/tests/unit/Rules/LanguageCodeTest.php +++ b/tests/unit/Rules/LanguageCodeTest.php @@ -16,7 +16,6 @@ use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Test\RuleTestCase; #[Group('rule')] -#[CoversClass(AbstractEnvelope::class)] #[CoversClass(LanguageCode::class)] final class LanguageCodeTest extends RuleTestCase { diff --git a/tests/unit/Rules/UrlTest.php b/tests/unit/Rules/UrlTest.php index bdf87a24..f83842f8 100644 --- a/tests/unit/Rules/UrlTest.php +++ b/tests/unit/Rules/UrlTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\Attributes\Group; use Respect\Validation\Test\RuleTestCase; #[Group('rule')] -#[CoversClass(AbstractEnvelope::class)] #[CoversClass(Url::class)] final class UrlTest extends RuleTestCase {