Skip to content

Commit 70f00bc

Browse files
committed
Merge pull request #19 from RobinvdVleuten/master
A null value can be matched.
2 parents 3bb08a8 + b920d07 commit 70f00bc

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

match.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coduo\PHPMatcher\Matcher\ChainMatcher;
66
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
77
use Coduo\PHPMatcher\Matcher\JsonMatcher;
8+
use Coduo\PHPMatcher\Matcher\NullMatcher;
89
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
910
use Coduo\PHPMatcher\Matcher\TypeMatcher;
1011
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
@@ -36,6 +37,7 @@ function match($value, $pattern)
3637
new CallbackMatcher(),
3738
new ExpressionMatcher(),
3839
new TypeMatcher(),
40+
new NullMatcher(),
3941
new ScalarMatcher(),
4042
new WildcardMatcher()
4143
));

src/Coduo/PHPMatcher/Matcher/JsonMatcher.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class JsonMatcher extends Matcher
66
{
7-
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean)@([^"])/';
7+
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean|null)@([^"])/';
88
const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3';
99

1010
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class NullMatcher extends Matcher
8+
{
9+
const MATCH_PATTERN = "/^@null@$/";
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (null !== $value) {
17+
$this->error = sprintf("%s \"%s\" does not match null.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern);
30+
}
31+
}

tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
55
use Coduo\PHPMatcher\Matcher\ChainMatcher;
66
use Coduo\PHPMatcher\Matcher\JsonMatcher;
7+
use Coduo\PHPMatcher\Matcher\NullMatcher;
78
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
89
use Coduo\PHPMatcher\Matcher\TypeMatcher;
910
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
@@ -20,6 +21,7 @@ public function setUp()
2021
$scalarMatchers = new ChainMatcher(array(
2122
new TypeMatcher(),
2223
new ScalarMatcher(),
24+
new NullMatcher(),
2325
new WildcardMatcher()
2426
));
2527
$this->matcher = new JsonMatcher(new ChainMatcher(array(
@@ -131,6 +133,10 @@ public static function positiveMatches()
131133
'{"foobar":[1.22, 2, "hello"]}',
132134
'{"foobar":[@double@, @integer@, @string@]}'
133135
),
136+
array(
137+
'{"null":[null]}',
138+
'{"null":[@null@]}'
139+
),
134140
array(
135141
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]}]}',
136142
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":"@wildcard@"}]}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
namespace Coduo\PHPMatcher\Tests\Matcher;
3+
4+
use Coduo\PHPMatcher\Matcher\NullMatcher;
5+
6+
class NullMatcherTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @dataProvider positiveCanMatchData
10+
*/
11+
public function test_positive_can_matches($pattern)
12+
{
13+
$matcher = new NullMatcher();
14+
$this->assertTrue($matcher->canMatch($pattern));
15+
}
16+
17+
/**
18+
* @dataProvider negativeCanMatchData
19+
*/
20+
public function test_negative_can_matches($pattern)
21+
{
22+
$matcher = new NullMatcher();
23+
$this->assertFalse($matcher->canMatch($pattern));
24+
}
25+
26+
/**
27+
* @dataProvider positiveMatchData
28+
*/
29+
public function test_positive_match($value, $pattern)
30+
{
31+
$matcher = new NullMatcher();
32+
$this->assertTrue($matcher->match($value, $pattern));
33+
}
34+
35+
/**
36+
* @dataProvider negativeMatchData
37+
*/
38+
public function test_negative_match($value, $pattern)
39+
{
40+
$matcher = new NullMatcher();
41+
$this->assertFalse($matcher->match($value, $pattern));
42+
}
43+
44+
/**
45+
* @dataProvider negativeMatchDescription
46+
*/
47+
public function test_negative_match_description($value, $pattern, $error)
48+
{
49+
$matcher = new NullMatcher();
50+
$matcher->match($value, $pattern);
51+
$this->assertEquals($error, $matcher->getError());
52+
}
53+
54+
public static function positiveCanMatchData()
55+
{
56+
return array(
57+
array("@null@")
58+
);
59+
}
60+
61+
public static function positiveMatchData()
62+
{
63+
return array(
64+
array(null, "@null@"),
65+
);
66+
}
67+
68+
public static function negativeCanMatchData()
69+
{
70+
return array(
71+
array("@null"),
72+
array("null"),
73+
array(0)
74+
);
75+
}
76+
77+
public static function negativeMatchData()
78+
{
79+
return array(
80+
array("null", "@null@"),
81+
array(0, "@null@")
82+
);
83+
}
84+
85+
public static function negativeMatchDescription()
86+
{
87+
return array(
88+
array("test", "@boolean@", "string \"test\" does not match null."),
89+
array(new \stdClass, "@string@", "object \"\\stdClass\" does not match null."),
90+
array(1.1, "@integer@", "double \"1.1\" does not match null."),
91+
array(false, "@double@", "boolean \"false\" does not match null."),
92+
array(1, "@array@", "integer \"1\" does not match null.")
93+
);
94+
}
95+
}

0 commit comments

Comments
 (0)