Skip to content

Commit

Permalink
Relax parameter types to allow filters like "array:empty" in JsonType (
Browse files Browse the repository at this point in the history
…#107)

* Relax parameter types to allow filters like "array:empty"
* Verify that the "array:empty" filter is case-insensitive
* Add "array:empty" filter to the documentation
  • Loading branch information
W0rma authored Jul 12, 2024
1 parent 727823a commit 311c2ba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Codeception/Module/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ public function dontSeeResponseContainsJson(array $json = []): void
*
* Here is the list of possible filters:
*
* * `array:empty` - check that value is an empty array
* * `integer:>{val}` - checks that integer is greater than {val} (works with float and string types too).
* * `integer:<{val}` - checks that integer is lower than {val} (works with float and string types too).
* * `string:url` - checks that value is valid url.
Expand Down
11 changes: 6 additions & 5 deletions src/Codeception/Util/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
*
* ```php
* <?php
* $jsonType = new JsonType(['name' => 'davert', 'id' => 1]);
* $jsonType = new JsonType(['name' => 'davert', 'id' => 1, 'data' => []]);
* $jsonType->matches([
* 'name' => 'string:!empty',
* 'id' => 'integer:>0|string:>0',
* 'data' => 'array:empty',
* ]); // => true
*
* $jsonType->matches([
Expand Down Expand Up @@ -170,7 +171,7 @@ protected function typeComparison(array $data, array $jsonType): string|bool
return $regexes[1][$pos];
}, $filter);

$matched = $matched && $this->matchFilter($filter, (string)$data[$key]);
$matched = $matched && $this->matchFilter($filter, $data[$key]);
}

if ($matched) {
Expand All @@ -186,7 +187,7 @@ protected function typeComparison(array $data, array $jsonType): string|bool
return true;
}

protected function matchFilter(string $filter, string $value)
protected function matchFilter(string $filter, mixed $value)
{
$filter = trim($filter);
if (str_starts_with($filter, '!')) {
Expand All @@ -206,7 +207,7 @@ protected function matchFilter(string $filter, string $value)
}

if (str_starts_with($filter, '=')) {
return $value === substr($filter, 1);
return (string) $value === substr($filter, 1);
}

if ($filter === 'url') {
Expand All @@ -232,7 +233,7 @@ protected function matchFilter(string $filter, string $value)
}

if (preg_match('#^regex\((.*?)\)$#', $filter, $matches)) {
return preg_match($matches[1], $value);
return preg_match($matches[1], (string) $value);
}

if (preg_match('#^>=(-?[\d\.]+)$#', $filter, $matches)) {
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/Codeception/Module/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,10 @@ public function testApplicationJsonSubtypeIncludesObjectSerialized()

public function testJsonTypeMatches()
{
$this->setStubResponse('{"xxx": "yyy", "user_id": 1}');
$this->module->seeResponseMatchesJsonType(['xxx' => 'string', 'user_id' => 'integer:<10']);
$this->module->dontSeeResponseMatchesJsonType(['xxx' => 'integer', 'user_id' => 'integer:<10']);
$this->setStubResponse('{"xxx": "yyy", "user_id": 1, "empty_array": [], "non_empty_array": ["foo"]}');
$this->module->seeResponseMatchesJsonType(['xxx' => 'string', 'user_id' => 'integer:<10', 'empty_array' => 'array:empty', 'non_empty_array' => 'array:!empty']);
$this->module->dontSeeResponseMatchesJsonType(['xxx' => 'integer', 'user_id' => 'integer:<10', 'empty_array' => 'array:empty', 'non_empty_array' => 'array:!empty']);
$this->module->dontSeeResponseMatchesJsonType(['xxx' => 'string', 'user_id' => 'integer:<10', 'empty_array' => 'array:!empty', 'non_empty_array' => 'array:!empty']);
}

public function testJsonTypeMatchesWithJsonPath()
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/Codeception/Util/JsonTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ final class JsonTypeTest extends Unit
'name' => 'string|null', // http://codeception.com/docs/modules/REST#seeResponseMatchesJsonType
'user' => [
'url' => 'String:url'
]
],
'empty_array' => 'array',
];

protected array $data = [
'id' => 11,
'retweeted' => false,
'in_reply_to_screen_name' => null,
'name' => null,
'user' => ['url' => 'http://davert.com']
'user' => ['url' => 'http://davert.com'],
'empty_array' => [],
];

protected function _after()
Expand Down Expand Up @@ -135,10 +137,11 @@ public function testEmailFilter()

public function testNegativeFilters()
{
$jsonType = new JsonType(['name' => 'davert', 'id' => 1]);
$jsonType = new JsonType(['name' => 'davert', 'id' => 1, 'data' => ['foo']]);
$this->assertTrue($jsonType->matches([
'name' => 'string:!date|string:!empty',
'id' => 'integer:!=0',
'data' => 'array:!empty',
]));
}

Expand Down Expand Up @@ -167,6 +170,8 @@ public function testArray()
$this->types['user'] = 'array';
$jsonType = new JsonType($this->data);
$this->assertTrue($jsonType->matches($this->types));
$this->assertTrue($jsonType->matches(['empty_array' => 'array:empty']));
$this->assertTrue($jsonType->matches(['empty_array' => 'Array:empty']));
}

public function testNull()
Expand Down

0 comments on commit 311c2ba

Please sign in to comment.