3
3
4
4
use Coduo \PHPMatcher \Matcher \ArrayMatcher ;
5
5
use Coduo \PHPMatcher \Matcher \CaptureMatcher ;
6
+ use Coduo \PHPMatcher \Matcher \CallbackMatcher ;
6
7
use Coduo \PHPMatcher \Matcher \ChainMatcher ;
7
8
use Coduo \PHPMatcher \Matcher \ExpressionMatcher ;
8
9
use Coduo \PHPMatcher \Matcher \JsonMatcher ;
13
14
14
15
class MatcherTest extends \PHPUnit_Framework_TestCase
15
16
{
17
+ /**
18
+ * @var Matcher
19
+ */
16
20
protected $ matcher ;
17
21
18
22
protected $ arrayValue ;
@@ -24,6 +28,7 @@ public function setUp()
24
28
$ this ->captureMatcher = new CaptureMatcher ();
25
29
26
30
$ scalarMatchers = new ChainMatcher (array (
31
+ new CallbackMatcher (),
27
32
new ExpressionMatcher (),
28
33
$ this ->captureMatcher ,
29
34
new CaptureMatcher (),
@@ -39,8 +44,11 @@ public function setUp()
39
44
$ arrayMatcher ,
40
45
new JsonMatcher ($ arrayMatcher )
41
46
)));
47
+ }
42
48
43
- $ this ->arrayValue = array (
49
+ public function test_matcher_with_array_value ()
50
+ {
51
+ $ value = array (
44
52
'users ' => array (
45
53
array (
46
54
'id ' => 1 ,
@@ -58,73 +66,47 @@ public function setUp()
58
66
'readyToUse ' => true ,
59
67
'data ' => new \stdClass (),
60
68
);
61
- }
62
69
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@ '
81
77
),
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
+ );
86
88
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 ));
108
91
}
109
92
110
- public function test_matcher_with_scalar_values()
93
+ /**
94
+ * @dataProvider scalarValues
95
+ */
96
+ public function test_matcher_with_scalar_values ($ value , $ pattern )
111
97
{
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
+ );
128
110
}
129
111
130
112
public function test_matcher_with_json ()
@@ -184,4 +166,16 @@ public function test_matcher_with_captures()
184
166
));
185
167
$ this ->assertEquals ($ this ->captureMatcher ['uid ' ], 5 );
186
168
}
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
+ }
187
181
}
0 commit comments