Skip to content

Commit de034de

Browse files
committed
Merge pull request #1 from norzechowicz/dev
Matcher test
2 parents 07ff7ad + 1a21022 commit de034de

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

src/JsonMatcher/Matcher/ChainMatcher.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public function addMatcher(PropertyMatcher $matcher)
2222
public function match($value, $pattern)
2323
{
2424
foreach ($this->matchers as $propertyMatcher) {
25-
if (false === $propertyMatcher->match($value, $pattern)) {
26-
return false;
25+
if ($propertyMatcher->canMatch($pattern)) {
26+
if (true === $propertyMatcher->match($value, $pattern)) {
27+
return true;
28+
}
2729
}
2830
}
2931

30-
return true;
32+
return false;
3133
}
3234

3335
public function canMatch($pattern)

src/JsonMatcher/Matcher/TypeMatcher.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function match($value, $pattern)
1515

1616
public function canMatch($pattern)
1717
{
18-
return 0 !== preg_match("/^@(string|integer|boolean|double)@$/", $pattern);
18+
return is_string($pattern) && 0 !== preg_match("/^@(string|integer|boolean|double)@$/", $pattern);
1919
}
2020

2121
private function extractType($pattern)

tests/JsonMatcher/MatcherTest.php

+77-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
11
<?php
22
namespace JsonMatcher\Tests;
33

4+
use JsonMatcher\Matcher\ArrayMatcher;
5+
use JsonMatcher\Matcher\ChainMatcher;
6+
use JsonMatcher\Matcher\ScalarMatcher;
7+
use JsonMatcher\Matcher\TypeMatcher;
8+
use JsonMatcher\Matcher\WildcardMatcher;
9+
use JsonMatcher\Matcher;
10+
411
class MatcherTest extends \PHPUnit_Framework_TestCase
512
{
6-
public function test_foo()
13+
protected $matcher;
14+
15+
protected $arrayValue;
16+
17+
public function setUp()
18+
{
19+
$scalarMatchers = new ChainMatcher(array(
20+
new TypeMatcher(),
21+
new ScalarMatcher(),
22+
new WildcardMatcher()
23+
));
24+
$this->matcher = new Matcher(new ChainMatcher(array(
25+
$scalarMatchers,
26+
new ArrayMatcher($scalarMatchers)
27+
)));
28+
29+
$this->arrayValue = array(
30+
'users' => array(
31+
array(
32+
'id' => 1,
33+
'firstName' => 'Norbert',
34+
'lastName' => 'Orzechowicz',
35+
'enabled' => true
36+
),
37+
array(
38+
'id' => 2,
39+
'firstName' => 'Michał',
40+
'lastName' => 'Dąbrowski',
41+
'enabled' => true,
42+
)
43+
),
44+
'readyToUse' => true,
45+
'data' => new \stdClass(),
46+
);
47+
}
48+
49+
public function test_matcher_with_array_value()
50+
{
51+
$this->assertTrue($this->matcher->match(
52+
$this->arrayValue,
53+
array(
54+
'users' => array(
55+
array(
56+
'id' => '@integer@',
57+
'firstName' => '@string@',
58+
'lastName' => 'Orzechowicz',
59+
'enabled' => '@boolean@'
60+
),
61+
array(
62+
'id' => '@integer@',
63+
'firstName' => '@string@',
64+
'lastName' => 'Dąbrowski',
65+
'enabled' => '@boolean@',
66+
)
67+
),
68+
'readyToUse' => true,
69+
'data' => '*',
70+
)
71+
));
72+
}
73+
74+
public function test_matcher_with_scalar_values()
775
{
8-
$this->assertTrue(true);
76+
$this->assertTrue($this->matcher->match(
77+
'Norbert Orzechowicz',
78+
'@string@'
79+
));
80+
$this->assertTrue($this->matcher->match(
81+
6.66,
82+
'@double@'
83+
));
984
}
1085
}

tests/JsonMatcher/TypeMatcherTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function test_can_match()
1515
$this->assertFalse($matcher->canMatch("qweqwe"));
1616
$this->assertFalse($matcher->canMatch(1));
1717
$this->assertFalse($matcher->canMatch("@string"));
18+
$this->assertFalse($matcher->canMatch(new \stdClass()));
1819
}
1920

2021
public function test_type_match()

0 commit comments

Comments
 (0)