-
Notifications
You must be signed in to change notification settings - Fork 9
/
ParserFilterModeTest.php
306 lines (277 loc) · 10.9 KB
/
ParserFilterModeTest.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace Tests\Tmilos\ScimFilterParser;
use Tmilos\ScimFilterParser\Error\FilterException;
use Tmilos\ScimFilterParser\Mode;
use Tmilos\ScimFilterParser\Parser;
use Tmilos\ScimFilterParser\Version;
class ParserFilterModeTest extends \PHPUnit_Framework_TestCase
{
public function parser_provider_v2()
{
return [
[
'userName eq "bjensen"',
['ComparisonExpression' => 'userName eq bjensen'],
],
[
'name.familyName co "O\'Malley"',
['ComparisonExpression' => 'name.familyName co O\'Malley'],
],
[
'userName sw "J"',
['ComparisonExpression' => 'userName sw J'],
],
[
'urn:ietf:params:scim:schemas:core:2.0:User:userName sw "J"',
['ComparisonExpression' => 'urn:ietf:params:scim:schemas:core:2.0:User : userName sw J'],
],
[
'title pr',
['ComparisonExpression' => 'title pr'],
],
[
'meta.lastModified gt "2011-05-13T04:42:34Z"',
['ComparisonExpression' => 'meta.lastModified gt 2011-05-13T04:42:34Z'],
],
[
'title pr and userType eq "Employee"',
[
'Conjunction' => [
['ComparisonExpression' => 'title pr'],
['ComparisonExpression' => 'userType eq Employee'],
]
]
],
[
'title pr or userType eq "Intern"',
[
'Disjunction' => [
['ComparisonExpression' => 'title pr'],
['ComparisonExpression' => 'userType eq Intern'],
]
]
],
[
'schemas eq "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"',
['ComparisonExpression' => 'schemas eq urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'],
],
[
'userType eq "Employee" and (emails co "example.com" or emails.value co "example.org")',
[
'Conjunction' => [
['ComparisonExpression' => 'userType eq Employee'],
[
'Disjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
]
]
]
]
],
[
'userType ne "Employee" and not (emails co "example.com" or emails.value co "example.org")',
[
'Conjunction' => [
['ComparisonExpression' => 'userType ne Employee'],
[
'Negation' => [
'Disjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
]
]
]
]
]
],
[
'userType eq "Employee" and (emails.type eq "work")',
[
'Conjunction' => [
['ComparisonExpression' => 'userType eq Employee'],
['ComparisonExpression' => 'emails.type eq work']
]
]
],
[
'emails[type eq "work"]',
[
'ValuePath' => [
['AttributePath' => 'emails'],
['ComparisonExpression' => 'type eq work'],
],
]
],
[
'userType eq "Employee" and emails[type eq "work" and value co "@example.com"]',
[
'Conjunction' => [
['ComparisonExpression' => 'userType eq Employee'],
[
'ValuePath' => [
['AttributePath' => 'emails'],
[
'Conjunction' => [
['ComparisonExpression' => 'type eq work'],
['ComparisonExpression' => 'value co @example.com'],
]
]
]
]
]
]
],
[
'emails[type eq "work" and value co "@example.com"] or ims[type eq "xmpp" and value co "@foo.com"]',
[
'Disjunction' => [
[
'ValuePath' => [
['AttributePath' => 'emails'],
[
'Conjunction' => [
['ComparisonExpression' => 'type eq work'],
['ComparisonExpression' => 'value co @example.com'],
]
]
]
],
[
'ValuePath' => [
['AttributePath' => 'ims'],
[
'Conjunction' => [
['ComparisonExpression' => 'type eq xmpp'],
['ComparisonExpression' => 'value co @foo.com'],
]
]
]
]
]
]
],
[
'username eq "john" and name sw "mike"',
[
'Conjunction' => [
['ComparisonExpression' => 'username eq john'],
['ComparisonExpression' => 'name sw mike']
]
]
],
[
'username eq "john" or name sw "mike"',
[
'Disjunction' => [
['ComparisonExpression' => 'username eq john'],
['ComparisonExpression' => 'name sw mike']
]
]
],
[
'username eq "john" or name sw "mike" and id ew "123"',
[
'Disjunction' => [
['ComparisonExpression' => 'username eq john'],
[
'Conjunction' => [
['ComparisonExpression' => 'name sw mike'],
['ComparisonExpression' => 'id ew 123'],
]
]
]
]
],
[
'username eq "john" and (name sw "mike" or id ew "123")',
[
'Conjunction' => [
['ComparisonExpression' => 'username eq john'],
[
'Disjunction' => [
['ComparisonExpression' => 'name sw mike'],
['ComparisonExpression' => 'id ew 123'],
]
]
]
]
],
[
'username eq "john" and not (name sw "mike" or id ew "123")',
[
'Conjunction' => [
['ComparisonExpression' => 'username eq john'],
[
'Negation' => [
'Disjunction' => [
['ComparisonExpression' => 'name sw mike'],
['ComparisonExpression' => 'id ew 123'],
]
]
]
]
]
],
];
}
/**
* @dataProvider parser_provider_v2
*/
public function test_parser_v2($filterString, array $expectedDump)
{
$parser = $this->getParser();
$node = $parser->parse($filterString);
$this->assertEquals($expectedDump, $node->dump(), sprintf("\n\n%s\n%s\n\n", $filterString, json_encode($node->dump(), JSON_PRETTY_PRINT)));
}
public function error_provider_v2()
{
return [
['none a valid filter', "[Syntax Error] line 0, col 5: Error: Expected comparision operator, got 'a'"],
['username xx "mike"', "[Syntax Error] line 0, col 9: Error: Expected comparision operator, got 'xx'"],
['username eq', "[Syntax Error] line 0, col 9: Error: Expected SP, got end of string."],
['username eq ', "[Syntax Error] line 0, col 11: Error: Expected comparison value, got end of string."],
['emails[type[value eq "1"]]', "[Syntax Error] line 0, col 11: Error: Expected SP, got '['"],
['members.value', '[Syntax Error] line 0, col 8: Error: Expected SP, got end of string.'],
];
}
/**
* @dataProvider error_provider_v2
*/
public function test_error_v2($filterString, $expectedMessage, $expectedException = FilterException::class)
{
$this->expectException($expectedException);
$this->expectExceptionMessage($expectedMessage);
$parser = $this->getParser();
$parser->parse($filterString);
}
/**
* @expectedException \Tmilos\ScimFilterParser\Error\FilterException
* @expectedExceptionMessage [Syntax Error] line 0, col 6: Error: Expected SP, got '['
*/
public function test_v1_no_value_path()
{
$parser = $this->getParser(Version::V1());
$parser->parse('emails[type eq "work"]');
}
/**
* @expectedException \Tmilos\ScimFilterParser\Error\FilterException
* @expectedExceptionMessage [Syntax Error] line 0, col 25: Error: Expected end of input, got '.'
*/
public function test_throws_error_for_value_path_with_attribute_path_in_filter_mode()
{
$parser = $this->getParser();
$node = $parser->parse('addresses[type eq "work"].streetAddress co "main"');
var_dump($node->dump());
}
/**
* @param Version $version
*
* @return Parser
*/
private function getParser(Version $version = null)
{
$version = $version ?: Version::V2();
return new Parser(Mode::FILTER(), $version);
}
}