Skip to content

Commit 4a33a1f

Browse files
committed
Improve docs about Json Type matches, added support for negative numbers and for matchers >= and <=
1 parent 3664989 commit 4a33a1f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

documentation.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,20 +668,25 @@ You can also apply filters to check values. Filter can be applied with `:` char
668668
Here is the list of possible filters:
669669

670670
* `integer:>{val}` - checks that integer is greater than {val} (works with float and string types too).
671+
* `integer:>={val}` - checks that integer is greater or equal than {val} (works with float and string types too).
671672
* `integer:<{val}` - checks that integer is lower than {val} (works with float and string types too).
673+
* `integer:<={val}` - checks that integer is lower or equal than {val} (works with float and string types too).
672674
* `string:url` - checks that value is valid url.
673675
* `string:date` - checks that value is date in JavaScript format: https://weblog.west-wind.com/posts/2014/Jan/06/JavaScript-JSON-Date-Parsing-and-real-Dates
674676
* `string:email` - checks that value is a valid email according to http://emailregex.com/
675677
* `string:regex({val})` - checks that string matches a regex provided with {val}
678+
* `string:empty` - checks that string is empty
676679

677680
This is how filters can be used:
678681

679682
```php
680683
<?php
681-
// {'user_id': 1, 'email' => '[email protected]'}
684+
// {'user_id': 1, 'email' => '[email protected]', 'name': 'Michael Bodnarchuk', 'karma': -15}
682685
$I->seeResponseMatchesJsonType([
683686
'user_id' => 'string:>0:<1000', // multiple filters can be used
684-
'email' => 'string:regex(~\@~)' // we just check that @ char is included
687+
'email' => 'string:regex(~\@~)', // we just check that @ char is included
688+
'name' => 'string:!empty', // we can check the opposite condition prepending the ! char
689+
'karma' => 'integer:>-1000:<1000' // negative values can also be used
685690
]);
686691
687692
// {'user_id': '1'}

src/Codeception/Util/JsonType.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,16 @@ protected function matchFilter($filter, $value)
228228
if (preg_match('~^regex\((.*?)\)$~', $filter, $matches)) {
229229
return preg_match($matches[1], $value);
230230
}
231-
if (preg_match('~^>([\d\.]+)$~', $filter, $matches)) {
231+
if (preg_match('~^>=(-?[\d\.]+)$~', $filter, $matches)) {
232+
return (float)$value >= (float)$matches[1];
233+
}
234+
if (preg_match('~^<=(-?[\d\.]+)$~', $filter, $matches)) {
235+
return (float)$value <= (float)$matches[1];
236+
}
237+
if (preg_match('~^>(-?[\d\.]+)$~', $filter, $matches)) {
232238
return (float)$value > (float)$matches[1];
233239
}
234-
if (preg_match('~^<([\d\.]+)$~', $filter, $matches)) {
240+
if (preg_match('~^<(-?[\d\.]+)$~', $filter, $matches)) {
235241
return (float)$value < (float)$matches[1];
236242
}
237243
}

tests/unit/Codeception/Util/JsonTypeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,32 @@ public function testNotMatchesBasicType()
4040

4141
public function testIntegerFilter()
4242
{
43+
$this->data['karma'] = -15;
4344
$jsonType = new JsonType($this->data);
4445
$this->assertStringContainsString('`id: 11` is of type', $jsonType->matches(['id' => 'integer:<5']));
4546
$this->assertStringContainsString('`id: 11` is of type', $jsonType->matches(['id' => 'integer:>15']));
4647
$this->assertTrue($jsonType->matches(['id' => 'integer:=11']));
4748
$this->assertTrue($jsonType->matches(['id' => 'integer:>5']));
4849
$this->assertTrue($jsonType->matches(['id' => 'integer:>5:<12']));
4950
$this->assertNotTrue($jsonType->matches(['id' => 'integer:>5:<10']));
51+
$this->assertTrue($jsonType->matches(['id' => 'integer:>=10']));
52+
$this->assertTrue($jsonType->matches(['id' => 'integer:>=11']));
53+
$this->assertNotTrue($jsonType->matches(['id' => 'integer:>=12']));
54+
$this->assertTrue($jsonType->matches(['id' => 'integer:<=11']));
55+
$this->assertTrue($jsonType->matches(['id' => 'integer:<=12']));
56+
$this->assertNotTrue($jsonType->matches(['id' => 'integer:<=10']));
57+
$this->assertTrue($jsonType->matches(['id' => 'integer:<=11:>=11:<=12:>=10']));
58+
$this->assertNotTrue($jsonType->matches(['id' => 'integer:<=11:>=11:<=12:<=10']));
59+
$this->assertTrue($jsonType->matches(['karma' => 'integer:<-14']));
60+
$this->assertNotTrue($jsonType->matches(['karma' => 'integer:<-15']));
61+
$this->assertTrue($jsonType->matches(['karma' => 'integer:>-16']));
62+
$this->assertNotTrue($jsonType->matches(['karma' => 'integer:>-15']));
63+
$this->assertTrue($jsonType->matches(['karma' => 'integer:<=-14']));
64+
$this->assertTrue($jsonType->matches(['karma' => 'integer:<=-15']));
65+
$this->assertNotTrue($jsonType->matches(['karma' => 'integer:<=-16']));
66+
$this->assertTrue($jsonType->matches(['karma' => 'integer:>=-16']));
67+
$this->assertTrue($jsonType->matches(['karma' => 'integer:>=-15']));
68+
$this->assertNotTrue($jsonType->matches(['karma' => 'integer:>=-14']));
5069
}
5170

5271
public function testUrlFilter()

0 commit comments

Comments
 (0)