Skip to content

Commit a9d72c0

Browse files
committedMay 22, 2014
Merge branch 'norzechowicz-callback-matcher'
* norzechowicz-callback-matcher: Added callback matcher
2 parents f6d7b7b + 40ba9b7 commit a9d72c0

File tree

5 files changed

+120
-64
lines changed

5 files changed

+120
-64
lines changed
 

‎match.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
4+
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
45
use Coduo\PHPMatcher\Matcher\ChainMatcher;
56
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
67
use Coduo\PHPMatcher\Matcher\JsonMatcher;
@@ -32,6 +33,7 @@
3233
function match($value, $pattern)
3334
{
3435
$scalarMatchers = new ChainMatcher(array(
36+
new CallbackMatcher(),
3537
new ExpressionMatcher(),
3638
new TypeMatcher(),
3739
new ScalarMatcher(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
class CallbackMatcher extends Matcher
6+
{
7+
/**
8+
* {@inheritdoc}
9+
*/
10+
public function match($value, $pattern)
11+
{
12+
return (boolean) $pattern->__invoke($value);
13+
}
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function canMatch($pattern)
19+
{
20+
return is_object($pattern) && $pattern instanceof \Closure;
21+
}
22+
}

‎src/Coduo/PHPMatcher/Matcher/JsonMatcher.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public function match($value, $pattern)
4444
*/
4545
public function canMatch($pattern)
4646
{
47-
$pattern = $this->transformPattern($pattern);
48-
return is_string($pattern) && $this->isValidJson($pattern);
47+
if (!is_string($pattern)) {
48+
return false;
49+
}
50+
51+
return $this->isValidJson($this->transformPattern($pattern));
4952
}
5053

5154
private function isValidJson($string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace Coduo\PHPMatcher\Tests\Matcher;
3+
4+
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
5+
6+
class CallbackMatcherTest extends \PHPUnit_Framework_TestCase
7+
{
8+
function test_positive_can_match()
9+
{
10+
$matcher = new CallbackMatcher();
11+
$this->assertTrue($matcher->canMatch(function() { return true; }));
12+
}
13+
14+
function test_negative_can_match()
15+
{
16+
$matcher = new CallbackMatcher();
17+
$this->assertFalse($matcher->canMatch(new \DateTime()));
18+
}
19+
20+
function test_positive_matches()
21+
{
22+
$matcher = new CallbackMatcher();
23+
$this->assertTrue($matcher->match(2, function($value) { return true; }));
24+
$this->assertTrue($matcher->match('true', function($value) { return $value; }));
25+
}
26+
27+
function test_negative_matches()
28+
{
29+
$matcher = new CallbackMatcher();
30+
$this->assertFalse($matcher->match(2, function($value) { return false; }));
31+
$this->assertFalse($matcher->match(0, function($value) { return $value; }));
32+
$this->assertFalse($matcher->match(null, function($value) { return $value; }));
33+
$this->assertFalse($matcher->match(array(), function($value) { return $value; }));
34+
}
35+
}

‎tests/Coduo/PHPMatcher/MatcherTest.php

+56-62
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
55
use Coduo\PHPMatcher\Matcher\CaptureMatcher;
6+
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
67
use Coduo\PHPMatcher\Matcher\ChainMatcher;
78
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
89
use Coduo\PHPMatcher\Matcher\JsonMatcher;
@@ -13,6 +14,9 @@
1314

1415
class MatcherTest extends \PHPUnit_Framework_TestCase
1516
{
17+
/**
18+
* @var Matcher
19+
*/
1620
protected $matcher;
1721

1822
protected $arrayValue;
@@ -24,6 +28,7 @@ public function setUp()
2428
$this->captureMatcher = new CaptureMatcher();
2529

2630
$scalarMatchers = new ChainMatcher(array(
31+
new CallbackMatcher(),
2732
new ExpressionMatcher(),
2833
$this->captureMatcher,
2934
new CaptureMatcher(),
@@ -39,8 +44,11 @@ public function setUp()
3944
$arrayMatcher,
4045
new JsonMatcher($arrayMatcher)
4146
)));
47+
}
4248

43-
$this->arrayValue = array(
49+
public function test_matcher_with_array_value()
50+
{
51+
$value = array(
4452
'users' => array(
4553
array(
4654
'id' => 1,
@@ -58,73 +66,47 @@ public function setUp()
5866
'readyToUse' => true,
5967
'data' => new \stdClass(),
6068
);
61-
}
6269

63-
public function test_matcher_with_array_value()
64-
{
65-
$this->assertTrue($this->matcher->match(
66-
$this->arrayValue,
67-
array(
68-
'users' => array(
69-
array(
70-
'id' => '@integer@',
71-
'firstName' => '@string@',
72-
'lastName' => 'Orzechowicz',
73-
'enabled' => '@boolean@'
74-
),
75-
array(
76-
'id' => '@integer@',
77-
'firstName' => '@string@',
78-
'lastName' => 'Dąbrowski',
79-
'enabled' => '@boolean@',
80-
)
70+
$expecation = array(
71+
'users' => array(
72+
array(
73+
'id' => '@integer@',
74+
'firstName' => '@string@',
75+
'lastName' => 'Orzechowicz',
76+
'enabled' => '@boolean@'
8177
),
82-
'readyToUse' => true,
83-
'data' => '@wildcard@',
84-
)
85-
));
78+
array(
79+
'id' => '@integer@',
80+
'firstName' => '@string@',
81+
'lastName' => 'Dąbrowski',
82+
'enabled' => '@boolean@',
83+
)
84+
),
85+
'readyToUse' => true,
86+
'data' => '@wildcard@',
87+
);
8688

87-
$this->assertTrue(match(
88-
$this->arrayValue,
89-
array(
90-
'users' => array(
91-
array(
92-
'id' => '@integer@',
93-
'firstName' => '@string@',
94-
'lastName' => 'Orzechowicz',
95-
'enabled' => '@boolean@'
96-
),
97-
array(
98-
'id' => '@integer@',
99-
'firstName' => '@string@',
100-
'lastName' => 'Dąbrowski',
101-
'enabled' => '@boolean@',
102-
)
103-
),
104-
'readyToUse' => true,
105-
'data' => '@wildcard@',
106-
)
107-
));
89+
$this->assertTrue($this->matcher->match($value, $expecation));
90+
$this->assertTrue(match($value, $expecation));
10891
}
10992

110-
public function test_matcher_with_scalar_values()
93+
/**
94+
* @dataProvider scalarValues
95+
*/
96+
public function test_matcher_with_scalar_values($value, $pattern)
11197
{
112-
$this->assertTrue($this->matcher->match(
113-
'Norbert Orzechowicz',
114-
'@string@'
115-
));
116-
$this->assertTrue(match(
117-
'Norbert Orzechowicz',
118-
'@string@'
119-
));
120-
$this->assertTrue($this->matcher->match(
121-
6.66,
122-
'@double@'
123-
));
124-
$this->assertTrue(match(
125-
6.66,
126-
'@double@'
127-
));
98+
$this->assertTrue($this->matcher->match($value, $pattern));
99+
$this->assertTrue(match($value, $pattern));
100+
}
101+
102+
public function scalarValues()
103+
{
104+
return array(
105+
array('Norbert Orzechowicz', '@string@'),
106+
array(6.66, '@double@'),
107+
array(1, '@integer@'),
108+
array(array('foo'), '@array@')
109+
);
128110
}
129111

130112
public function test_matcher_with_json()
@@ -184,4 +166,16 @@ public function test_matcher_with_captures()
184166
));
185167
$this->assertEquals($this->captureMatcher['uid'], 5);
186168
}
169+
170+
function test_matcher_with_callback()
171+
{
172+
$this->assertTrue($this->matcher->match('test', function($value) { return $value === 'test';}));
173+
$this->assertFalse($this->matcher->match('test', function($value) { return $value !== 'test';}));
174+
}
175+
176+
function test_matcher_with_wildcard()
177+
{
178+
$this->assertTrue($this->matcher->match('test', '@*@'));
179+
$this->assertTrue($this->matcher->match('test', '@wildcard@'));
180+
}
187181
}

0 commit comments

Comments
 (0)