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: Dont allow null as example value #701

Merged
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
28 changes: 13 additions & 15 deletions compatibility-suite/tests/Service/BodyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

final class BodyValidator implements BodyValidatorInterface
{
public const INT_REGEX = '/\d+/';
public const DEC_REGEX = '/\d+\.\d+/';
public const HEX_REGEX = '/[a-fA-F0-9]+/';
public const STR_REGEX = '/\d{1,8}/';
public const DATE_REGEX = '/\d{4}-\d{2}-\d{2}/';
Expand All @@ -23,19 +21,19 @@ public function __construct(private BodyStorageInterface $bodyStorage)
public function validateType(string $path, string $type): void
{
$value = $this->getActualValue($path);
Assert::assertTrue((bool) match ($type) {
'integer' => is_numeric($value) && preg_match(self::INT_REGEX, $value),
'decimal number' => is_string($value) && preg_match(self::DEC_REGEX, $value),
'hexadecimal number' => is_string($value) && preg_match(self::HEX_REGEX, $value),
'random string' => is_string($value),
'string from the regex' => is_string($value) && preg_match(self::STR_REGEX, $value),
'date' => is_string($value) && preg_match(self::DATE_REGEX, $value),
'time' => is_string($value) && preg_match(self::TIME_REGEX, $value),
'date-time' => is_string($value) && preg_match(self::DATETIME_REGEX, $value),
'UUID', 'simple UUID', 'lower-case-hyphenated UUID', 'upper-case-hyphenated UUID', 'URN UUID' => Uuid::isValid($value),
'boolean' => is_bool($value),
default => false,
});
match ($type) {
'integer' => Assert::assertIsInt($value),
'decimal number' => Assert::assertIsFloat($value),
'hexadecimal number' => Assert::assertIsString($value) && Assert::assertMatchesRegularExpression(self::HEX_REGEX, $value),
'random string' => Assert::assertIsString($value),
'string from the regex' => Assert::assertIsString($value) && Assert::assertMatchesRegularExpression(self::STR_REGEX, $value),
'date' => Assert::assertIsString($value) && Assert::assertMatchesRegularExpression(self::DATE_REGEX, $value),
'time' => Assert::assertIsString($value) && Assert::assertMatchesRegularExpression(self::TIME_REGEX, $value),
'date-time' => Assert::assertIsString($value) && Assert::assertMatchesRegularExpression(self::DATETIME_REGEX, $value),
'UUID', 'simple UUID', 'lower-case-hyphenated UUID', 'upper-case-hyphenated UUID', 'URN UUID' => Assert::assertTrue(Uuid::isValid($value)),
'boolean' => Assert::assertIsBool($value),
default => null,
};
}

public function validateValue(string $path, string $value): void
Expand Down
52 changes: 29 additions & 23 deletions compatibility-suite/tests/Service/MatchingRuleConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Consumer\Matcher\Enum\HttpStatus;
use PhpPact\Consumer\Matcher\Matchers\ArrayContains;
use PhpPact\Consumer\Matcher\Matchers\Boolean;
use PhpPact\Consumer\Matcher\Matchers\ContentType;
Expand Down Expand Up @@ -55,10 +56,10 @@ public function convert(MatchingRule $rule, mixed $value): ?MatcherInterface
return new Number($this->getNumber($value));

case 'integer':
return new Integer($this->getNumber($value));
return new Integer($this->getInteger($value));

case 'decimal':
return new Decimal($this->getNumber($value));
return new Decimal($this->getDecimal($value));

case 'null':
return new NullValue();
Expand Down Expand Up @@ -92,43 +93,48 @@ public function convert(MatchingRule $rule, mixed $value): ?MatcherInterface

case 'regex':
$regex = $rule->getMatcherAttribute('regex');
return new Regex($regex, $this->ignoreInvalidValue($regex, $value));
return new Regex($regex, $value ?? '');

case 'statusCode':
return new StatusCode($rule->getMatcherAttribute('status'));
return new StatusCode($this->getHttpStatus($rule));

default:
return null;
}
}

private function getNumber(mixed $value): int|float|null
private function getNumber(mixed $value): int|float
{
if (is_numeric($value)) {
$value = $value + 0;
} else {
$value = null;
return $value + 0;
}

return $value;
// @todo Fix this compatibility-suite's mistake: there is no number in `basic.json`
return 1;
}

private function ignoreInvalidValue(string $regex, mixed $value): string|array|null
private function getInteger(mixed $value): int
{
if (is_string($value)) {
if (!preg_match("/$regex/", $value)) {
$value = null;
}
} elseif (is_array($value)) {
foreach (array_keys($value) as $key) {
if (!preg_match("/$regex/", $value[$key])) {
$value[$key] = null;
}
}
} else {
$value = null;
if (is_numeric($value)) {
return $value + 0;
}

return $value;
// @todo Fix this compatibility-suite's mistake: there is no integer in `basic.json`
return 1;
}

private function getDecimal(mixed $value): float
{
if (is_numeric($value)) {
return $value + 0;
}

// @todo Fix this compatibility-suite's mistake: there is no decimal in `basic.json`
return 1.1;
}

private function getHttpStatus(MatchingRule $rule): HttpStatus
{
return HttpStatus::from($rule->getMatcherAttribute('status') ?? '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testGetGenerators(): void
$this->assertTrue($this->validateDateTime($body['time'], 'H:i:s'));
$this->assertTrue($this->validateDateTime($body['datetime'], "Y-m-d\TH:i:s"));
$this->assertIsString($body['string']);
$this->assertNotSame(StringValue::DEFAULT_VALUE, $body['string']);
$this->assertNotSame('', $body['string']);
$this->assertIsNumeric($body['number']);
$this->assertNotSame('http://localhost/users/1234/posts/latest', $body['url']);
$this->assertMatchesRegularExpression('/.*(\\/users\\/\\d+\\/posts\\/latest)$/', $body['url']);
Expand Down
28 changes: 14 additions & 14 deletions example/generators/pacts/generatorsConsumer-generatorsProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"request": {
"body": {
"content": {
"id": null
"id": 13
},
"contentType": "application/json",
"encoded": false
Expand Down Expand Up @@ -54,25 +54,25 @@
"body": {
"content": {
"boolean": true,
"boolean_v3": null,
"date": null,
"datetime": null,
"boolean_v3": false,
"date": "",
"datetime": "",
"decimal": 13.01,
"decimal_v3": null,
"decimal_v3": 13.01,
"equality": "Hello World!",
"hexadecimal": null,
"hexadecimal": "",
"integer": 13,
"integer_v3": null,
"integer_v3": 13,
"like": "6057401b-c539-4948-971a-24b702d79882",
"notEmpty": "text",
"number": null,
"regex": null,
"number": 13,
"regex": "",
"requestId": 222,
"semver": null,
"string": "some string",
"time": null,
"url": null,
"uuid": null
"semver": "",
"string": "",
"time": "",
"url": "",
"uuid": ""
},
"contentType": "application/json",
"encoded": false
Expand Down
4 changes: 2 additions & 2 deletions example/message/pacts/messageConsumer-messageProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"messages": [
{
"contents": {
"number": null,
"number": 13,
"text": "Hello Mary"
},
"description": "an alligator named Mary exists",
Expand Down Expand Up @@ -49,7 +49,7 @@
"metadata": {
"contentType": "application/json",
"queue": "wind cries",
"routing_key": "some string"
"routing_key": ""
},
"providerStates": [
{
Expand Down

This file was deleted.

Loading
Loading