Skip to content

Commit

Permalink
chore: Don't validate example value with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Dec 2, 2024
1 parent 251f566 commit 58bea54
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 42 deletions.

This file was deleted.

22 changes: 0 additions & 22 deletions src/PhpPact/Consumer/Matcher/Matchers/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Exception\InvalidRegexException;
use PhpPact\Consumer\Matcher\Generators\Regex as RegexGenerator;
use PhpPact\Consumer\Matcher\Model\Attributes;
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

use function preg_last_error;
use function preg_match;

class Regex extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
Expand All @@ -27,28 +23,10 @@ public function __construct(
) {
if ($values === null) {
$this->setGenerator(new RegexGenerator($this->regex));
} else {
$this->validateRegex();
}
parent::__construct();
}

/**
* @todo Use json_validate()
*/
private function validateRegex(): void
{
foreach ((array) $this->values as $value) {
$result = preg_match("/$this->regex/", $value);

if ($result !== 1) {
$errorCode = preg_last_error();

throw new InvalidRegexException("The value '{$value}' doesn't match pattern '{$this->regex}'. Failed with error code {$errorCode}.");
}
}
}

public function formatJson(): Attributes
{
return $this->mergeJson(new Attributes([
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpPact/Consumer/Matcher/Matchers/EachValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testFormatJson(): void
"value": null
}
]
}
}
JSON,
$jsonEncoded
);
Expand Down
12 changes: 0 additions & 12 deletions tests/PhpPact/Consumer/Matcher/Matchers/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpPactTest\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Exception\InvalidRegexException;
use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Matchers\Regex;
Expand All @@ -14,17 +13,6 @@ class RegexTest extends TestCase
{
private string $regex = '\d+';

#[TestWith(['number', true])]
#[TestWith(['integer', false])]
public function testInvalidRegex(string $value, bool $isArray): void
{
$values = $isArray ? [$value] : $value;
$this->expectException(InvalidRegexException::class);
$value = is_array($values) ? $values[0] : $values;
$this->expectExceptionMessage("The value '{$value}' doesn't match pattern '{$this->regex}'. Failed with error code 0.");
new Regex($this->regex, $values);
}

/**
* @param string|string[]|null $values
*/
Expand Down

0 comments on commit 58bea54

Please sign in to comment.