Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multi values for regex matcher #353

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getMatchers(): array
{
$response = $this->httpClient->get("{$this->baseUri}/matchers", [
'headers' => ['Accept' => 'application/json'],
'query' => ['ignore' => 'statusCode'],
'query' => 'ignore=statusCode&pages=2&pages=3&locales[]=fr-BE&locales[]=ru-RU',
]);

return \json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
Expand Down
16 changes: 16 additions & 0 deletions example/matchers/consumer/tests/Service/MatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function testGetMatchers()
->setPath($this->matcher->regex('/matchers', '^\/matchers$'))
->setQuery([
'ignore' => 'statusCode',
'pages' => [ // Consumer send multiple values, but provider receive single (last) value
json_encode($this->matcher->regex([1, 22], '\d+')),
],
'locales[]' => [ // Consumer send multiple values, provider receive all values
json_encode($this->matcher->regex(['en-US', 'en-AU'], '^[a-z]{2}-[A-Z]{2}$')),
],
])
->addHeader('Accept', 'application/json');

Expand Down Expand Up @@ -87,6 +93,11 @@ public function testGetMatchers()
['vehicle 1' => 'car'],
[$this->matcher->regex(null, 'car|bike|motorbike')]
),
'query' => [
'ignore' => 'statusCode',
'pages' => '22',
'locales' => ['en-US', 'en-AU'],
],
]);

$config = new MockServerConfig();
Expand Down Expand Up @@ -161,6 +172,11 @@ public function testGetMatchers()
'eachValue' => [
'vehicle 1' => 'car',
],
'query' => [
'ignore' => 'statusCode',
'pages' => '22',
'locales' => ['en-US', 'en-AU'],
],
], $matchersResult);
}
}
37 changes: 36 additions & 1 deletion example/matchers/pacts/matchersConsumer-matchersProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,40 @@
}
]
},
"query": {}
"query": {
"$.pages": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "\\d+"
}
]
},
"$['locales[]']": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^[a-z]{2}-[A-Z]{2}$"
}
]
}
}
},
"method": "GET",
"path": "/matchers",
"query": {
"ignore": [
"statusCode"
],
"locales[]": [
"en-US",
"en-AU"
],
"pages": [
"1",
"22"
]
}
},
Expand Down Expand Up @@ -99,6 +126,14 @@
],
"nullValue": null,
"number": 123,
"query": {
"ignore": "statusCode",
"locales": [
"en-US",
"en-AU"
],
"pages": "22"
},
"regex": "500 miles",
"semver": "10.0.0-alpha4",
"time": "23:59::58",
Expand Down
1 change: 1 addition & 0 deletions example/matchers/provider/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'item 1' => 'bike',
'item 2' => 'motorbike',
],
'query' => $request->getQueryParams(),
]));

return $response->withHeader('Content-Type', 'application/json');
Expand Down
24 changes: 14 additions & 10 deletions src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,35 @@ public function constrainedArrayLike(mixed $value, int $min, int $max, ?int $cou
/**
* Validate that a value will match a regex pattern.
*
* @param string|null $value example of what the expected data would be
* @param string|string[]|null $values example of what the expected data would be
* @param string $pattern valid Ruby regex pattern
*
* @return array<string, mixed>
*
* @throws Exception
*/
public function term(?string $value, string $pattern): array
public function term(string|array|null $values, string $pattern): array
{
if (null === $value) {
if (null === $values) {
return [
'regex' => $pattern,
'pact:matcher:type' => 'regex',
'pact:generator:type' => 'Regex',
];
}

$result = preg_match("/$pattern/", $value);
foreach ((array) $values as $value) {
$result = preg_match("/$pattern/", $value);

if ($result === false || $result === 0) {
$errorCode = preg_last_error();
if ($result === false || $result === 0) {
$errorCode = preg_last_error();

throw new Exception("The pattern {$pattern} is not valid for value {$value}. Failed with error code {$errorCode}.");
throw new Exception("The pattern {$pattern} is not valid for value {$value}. Failed with error code {$errorCode}.");
}
}

return [
'value' => $value,
'value' => $values,
'regex' => $pattern,
'pact:matcher:type' => 'regex',
];
Expand All @@ -159,13 +161,15 @@ public function term(?string $value, string $pattern): array
/**
* Alias for the term matcher.
*
* @param string|string[]|null $values
*
* @return array<string, mixed>
*
* @throws Exception
*/
public function regex(?string $value, string $pattern): array
public function regex(string|array|null $values, string $pattern): array
{
return $this->term($value, $pattern);
return $this->term($values, $pattern);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ public function testRegex()
$this->assertEquals($expected, $actual);
}

/**
* @throws Exception
*/
public function testRegexMultiValues()
{
$expected = [
'value' => [1, 23],
'regex' => '\d+',
'pact:matcher:type' => 'regex',
];

$actual = $this->matcher->regex([1, 23], '\d+');

$this->assertEquals($expected, $actual);
}

/**
* @throws Exception
*/
Expand Down