Skip to content

Commit

Permalink
It is actually possible to test minimum PCRE version support error
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Jul 8, 2024
1 parent 6bfd922 commit ccee171
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Validator/Constraints/BannedScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Validator/BannedScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ccee171

Please sign in to comment.