From ccee171b130537596d2524239adac019f7c34bb2 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Mon, 8 Jul 2024 10:01:50 +0200 Subject: [PATCH] It is actually possible to test minimum PCRE version support error --- src/Validator/Constraints/BannedScripts.php | 5 ++--- tests/Unit/Validator/BannedScriptTest.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Validator/Constraints/BannedScripts.php b/src/Validator/Constraints/BannedScripts.php index 29b536a..fd3620e 100644 --- a/src/Validator/Constraints/BannedScripts.php +++ b/src/Validator/Constraints/BannedScripts.php @@ -46,9 +46,8 @@ public function __construct( public function getCharacterClass(): string { if (!isset($this->characterClass)) { - /* @infection-ignore-all impossible to reliably test this check */ - if (version_compare(self::MINIMUM_PCRE_VERSION, PCRE_VERSION) > 0) { - throw new \LogicException(sprintf('PHP is using PCRE version %s but requires at least version %s to detect banned scripts. Update your PHP installation and/or operating system.', PCRE_VERSION, self::MINIMUM_PCRE_VERSION)); // @codeCoverageIgnore + if (version_compare(static::MINIMUM_PCRE_VERSION, PCRE_VERSION) > 0) { + throw new \LogicException(sprintf('PHP is using PCRE version %s but requires at least version %s to detect banned scripts. Update your PHP installation and/or operating system.', PCRE_VERSION, static::MINIMUM_PCRE_VERSION)); } $this->characterClass = sprintf('[%s]', implode('', array_map(fn (Script $script) => sprintf('\\p{%s}', $script->value), $this->scripts))); } diff --git a/tests/Unit/Validator/BannedScriptTest.php b/tests/Unit/Validator/BannedScriptTest.php index 351b577..d7fc1b2 100644 --- a/tests/Unit/Validator/BannedScriptTest.php +++ b/tests/Unit/Validator/BannedScriptTest.php @@ -50,6 +50,18 @@ public function testValidatorMismatchThrows(): void $this->constraintValidator->validate(684, new Length(min: 3)); } + public function testMinimumPCREVersionIsSatisfied(): void + { + $constraint = new class(Script::Armenian) extends BannedScripts { + public const MINIMUM_PCRE_VERSION = '684.666'; + }; + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('requires at least version 684.666'); + + $constraint->getCharacterClass(); + } + public function testOnlyStringablesAndNullAreAccepted(): void { $constraint = new BannedScripts(Script::Bengali);