Skip to content

Commit 86dea3b

Browse files
committed
Relax parameter types to allow filters like "array:empty"
1 parent 727823a commit 86dea3b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Codeception/Util/JsonType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected function typeComparison(array $data, array $jsonType): string|bool
170170
return $regexes[1][$pos];
171171
}, $filter);
172172

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

176176
if ($matched) {
@@ -186,7 +186,7 @@ protected function typeComparison(array $data, array $jsonType): string|bool
186186
return true;
187187
}
188188

189-
protected function matchFilter(string $filter, string $value)
189+
protected function matchFilter(string $filter, mixed $value)
190190
{
191191
$filter = trim($filter);
192192
if (str_starts_with($filter, '!')) {
@@ -206,7 +206,7 @@ protected function matchFilter(string $filter, string $value)
206206
}
207207

208208
if (str_starts_with($filter, '=')) {
209-
return $value === substr($filter, 1);
209+
return (string) $value === substr($filter, 1);
210210
}
211211

212212
if ($filter === 'url') {
@@ -232,7 +232,7 @@ protected function matchFilter(string $filter, string $value)
232232
}
233233

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

238238
if (preg_match('#^>=(-?[\d\.]+)$#', $filter, $matches)) {

tests/unit/Codeception/Util/JsonTypeTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ final class JsonTypeTest extends Unit
1515
'name' => 'string|null', // http://codeception.com/docs/modules/REST#seeResponseMatchesJsonType
1616
'user' => [
1717
'url' => 'String:url'
18-
]
18+
],
19+
'empty_array' => 'array',
1920
];
2021

2122
protected array $data = [
2223
'id' => 11,
2324
'retweeted' => false,
2425
'in_reply_to_screen_name' => null,
2526
'name' => null,
26-
'user' => ['url' => 'http://davert.com']
27+
'user' => ['url' => 'http://davert.com'],
28+
'empty_array' => [],
2729
];
2830

2931
protected function _after()
@@ -167,6 +169,7 @@ public function testArray()
167169
$this->types['user'] = 'array';
168170
$jsonType = new JsonType($this->data);
169171
$this->assertTrue($jsonType->matches($this->types));
172+
$this->assertTrue($jsonType->matches(['empty_array' => 'array:empty']));
170173
}
171174

172175
public function testNull()

0 commit comments

Comments
 (0)