forked from coduo/php-matcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayMatcherTest.php
249 lines (222 loc) · 7.88 KB
/
ArrayMatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Coduo\PHPMatcher\Tests\Matcher;
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;
class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Matcher\ArrayMatcher
*/
private $matcher;
public function setUp()
{
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer());
$this->matcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
)),
$parser
);
}
/**
* @dataProvider positiveMatchData
*/
public function test_positive_match_arrays($value, $pattern)
{
$this->assertTrue($this->matcher->match($value, $pattern));
}
/**
* @dataProvider negativeMatchData
*/
public function test_negative_match_arrays($value, $pattern)
{
$this->assertFalse($this->matcher->match($value, $pattern));
}
public function test_negative_match_when_cant_find_matcher_that_can_match_array_element()
{
$matcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(array(
new Matcher\WildcardMatcher()
)),
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer())
);
$this->assertTrue($matcher->match(array('test' => 1), array('test' => 1)));
}
public function test_error_when_path_in_pattern_does_not_exist()
{
$this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('bar' => 'bar value')));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern.');
}
public function test_error_when_path_in_nested_pattern_does_not_exist()
{
$array = array('foo' => array('bar' => array('baz' => 'bar value')));
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));
$this->assertFalse($this->matcher->match($array, $pattern));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
}
public function test_error_when_path_in_value_does_not_exist()
{
$array = array('foo' => 'foo');
$pattern = array('foo' => 'foo', 'bar' => 'bar');
$this->assertFalse($this->matcher->match($array, $pattern));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [bar] in value.');
}
public function test_error_when_path_in_nested_value_does_not_exist()
{
$array = array('foo' => array('bar' => array()));
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));
$this->assertFalse($this->matcher->match($array, $pattern));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
}
public function test_error_when_matching_fail()
{
$this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('foo' => 'bar value')));
$this->assertEquals($this->matcher->getError(), '"foo value" does not match "bar value".');
}
public function test_error_message_when_matching_non_array_value()
{
$this->assertFalse($this->matcher->match(new \DateTime(), "@array@"));
$this->assertEquals($this->matcher->getError(), "object \"\\DateTime\" is not a valid array.");
}
public function test_matching_array_to_array_pattern()
{
$this->assertTrue($this->matcher->match(array("foo", "bar"), "@array@"));
$this->assertTrue($this->matcher->match(array("foo"), "@array@.inArray(\"foo\")"));
$this->assertTrue($this->matcher->match(
array("foo", array("bar")),
array(
"@string@",
"@array@.inArray(\"bar\")"
)
));
}
public static function positiveMatchData()
{
$simpleArr = array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz'
),
array(
'firstName' => 'Michał',
'lastName' => 'Dąbrowski'
)
),
true,
false,
1,
6.66
);
$simpleArrPattern = array(
'users' => array(
array(
'firstName' => '@string@',
'lastName' => '@string@'
),
Matcher\ArrayMatcher::UNBOUNDED_PATTERN
),
true,
false,
1,
6.66
);
return array(
array($simpleArr, $simpleArr),
array($simpleArr, $simpleArrPattern),
array(array(), array()),
array(array('foo' => null), array('foo' => null)),
array(array('foo' => null), array('foo' => "@null@")),
array(array('key' => 'val'), array('key' => 'val')),
array(array(1), array(1)),
array(
array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')),
array('roles' => '@wildcard@'),
),
'unbound array should match one or none elements' => array(
array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Foobar',
),
),
true,
false,
1,
6.66,
),
$simpleArrPattern,
),
);
}
public static function negativeMatchData()
{
$simpleArr = array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz'
),
array(
'firstName' => 'Michał',
'lastName' => 'Dąbrowski'
)
),
true,
false,
1,
6.66
);
$simpleDiff = array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz'
),
array(
'firstName' => 'Pablo',
'lastName' => 'Dąbrowski'
)
),
true,
false,
1,
6.66
);
return array(
array($simpleArr, $simpleDiff),
array(array("status" => "ok", "data" => array(array('foo'))), array("status" => "ok", "data" => array())),
array(array(1), array()),
array(array('key' => 'val'), array('key' => 'val2')),
array(array(1), array(2)),
array(array('foo', 1, 3), array('foo', 2, 3)),
array(array(), array('foo' => 'bar')),
'unbound array should match one or none elements' => array(
array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Foobar',
),
),
true,
false,
1,
6.66,
),
$simpleDiff,
),
);
}
}