-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathQueryMatchFilterTest.php
More file actions
300 lines (259 loc) · 9.64 KB
/
Copy pathQueryMatchFilterTest.php
File metadata and controls
300 lines (259 loc) · 9.64 KB
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
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
declare(strict_types=1);
namespace Flow\JSONPath\Test;
use Flow\JSONPath\Filters\QueryMatchFilter;
use Flow\JSONPath\JSONPath;
use Flow\JSONPath\JSONPathException;
use Flow\JSONPath\JSONPathToken;
use Flow\JSONPath\TokenType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
#[CoversClass(QueryMatchFilter::class)]
class QueryMatchFilterTest extends TestCase
{
/**
* @return iterable<string, array{data: mixed, expression: string, expected: array<int, mixed>}>
*/
public static function filterProvider(): iterable
{
yield 'shorthand truthy filters values' => [
'data' => [0, 1, '', 'value', false],
'expression' => '$[?@]',
'expected' => [1 => 1, 3 => 'value'],
];
yield 'negation wrapped' => [
'data' => [['flag' => true], ['flag' => false]],
'expression' => '$[?(!(@.flag==true))]',
'expected' => [['flag' => false]],
];
yield 'negation unwrapped' => [
'data' => [['flag' => true], ['flag' => false]],
'expression' => '$[?(!@.flag==true)]',
'expected' => [['flag' => false]],
];
yield 'grouped logical expressions' => [
'data' => [
['active' => true, 'score' => 1],
['active' => true, 'score' => 2],
['active' => false, 'score' => 3],
],
'expression' => '$[?(@.active==true && (@.score>1))]',
'expected' => [['active' => true, 'score' => 2]],
];
yield 'path comparison current and root' => [
'data' => [
'threshold' => 5,
'items' => [
['v' => 5, 'w' => 5],
['v' => 4, 'w' => 5],
],
],
'expression' => '$.items[?(@.v==@.w && @.v==$.threshold)]',
'expected' => [['v' => 5, 'w' => 5]],
];
yield 'missing key compared to path still evaluates' => [
'data' => [['foo' => 1], ['foo' => 1, 'bar' => 1]],
'expression' => '$[?(@.bar==@.foo)]',
'expected' => [['foo' => 1, 'bar' => 1]],
];
yield 'dot separated key resolves through jsonpath' => [
'data' => [
['nested' => ['value' => 3]],
['nested' => ['value' => 4]],
],
'expression' => '$[?(@.nested.value==3)]',
'expected' => [['nested' => ['value' => 3]]],
];
yield 'deep equal lists and objects' => [
'data' => [
['left' => [1, 2], 'right' => [1, 2]],
['left' => [1, 2], 'right' => [2, 1]],
['left' => (object)['a' => 1, 'b' => 2], 'right' => (object)['b' => 2, 'a' => 1]],
['left' => (object)['a' => 1], 'right' => (object)['a' => 2]],
],
'expression' => '$[?(@.left==@.right)]',
'expected' => [
['left' => [1, 2], 'right' => [1, 2]],
['left' => (object)['a' => 1, 'b' => 2], 'right' => (object)['b' => 2, 'a' => 1]],
],
];
yield 'plain node selection compares current node' => [
'data' => [0, 1, 2],
'expression' => '$[?(@==@)]',
'expected' => [0, 1, 2],
];
yield 'deep equal failure branches' => [
'data' => [
['left' => [1, 2], 'right' => ['a' => 1, 'b' => 2]],
['left' => [1], 'right' => [1, 2]],
],
'expression' => '$[?(@.left==@.right)]',
'expected' => [],
];
yield 'regex comparison' => [
'data' => ['foo', 'bar'],
'expression' => '$[?(@ =~ /fo.*/)]',
'expected' => ['foo'],
];
yield 'in operator' => [
'data' => [1, 2, 3],
'expression' => '$[?(@ in [1,3])]',
'expected' => [1, 3],
];
yield 'nin operator with short circuit or' => [
'data' => [
['a' => 1],
['b' => 1],
['a' => 3],
],
'expression' => '$[?(@.a nin [2,3] || @.b==1)]',
'expected' => [
['a' => 1],
['b' => 1],
],
];
yield 'existence check without operator' => [
'data' => [
['value' => 1],
['other' => 2],
],
'expression' => '$[?(@.value)]',
'expected' => [
['value' => 1],
],
];
yield '!in operator' => [
'data' => [1, 2, 3],
'expression' => '$[?(@ !in [2])]',
'expected' => [1, 3],
];
yield 'less than comparison' => [
'data' => [['n' => 1], ['n' => 3]],
'expression' => '$[?(@.n<2)]',
'expected' => [['n' => 1]],
];
yield 'less or equal comparison' => [
'data' => [['n' => 1], ['n' => 2], ['n' => 3]],
'expression' => '$[?(@.n<=2)]',
'expected' => [['n' => 1], ['n' => 2]],
];
yield 'greater or equal comparison' => [
'data' => [['n' => 1], ['n' => 2], ['n' => 3]],
'expression' => '$[?(@.n>=2)]',
'expected' => [['n' => 2], ['n' => 3]],
];
yield 'not equals comparison' => [
'data' => [['value' => 1], ['value' => 2]],
'expression' => '$[?(@.value!=2)]',
'expected' => [['value' => 1]],
];
}
/**
* @param array<int, mixed> $expected
* @throws JSONPathException
*/
#[DataProvider('filterProvider')]
public function testFilterScenarios(mixed $data, string $expression, array $expected): void
{
$result = new JSONPath($data)->find($expression)->getData();
self::assertEquals(\array_values($expected), \array_values($result));
}
/**
* @return iterable<string, array{expression: string, expectMatch: bool}>
*/
public static function constantExpressionProvider(): iterable
{
yield 'num comparison true' => ['expression' => '[?(1<2)]', 'expectMatch' => true];
yield 'num comparison false' => ['expression' => '[?(2>3)]', 'expectMatch' => false];
yield 'num with leading zeros decoded as number' => ['expression' => '[?(0123==123)]', 'expectMatch' => true];
yield 'string literal decoding' => ['expression' => '[?(foo==foo)]', 'expectMatch' => true];
yield 'invalid less than comparison for non-scalars' => ['expression' => '[?([]<1)]', 'expectMatch' => false];
yield 'not equals' => ['expression' => '[?(2!=3)]', 'expectMatch' => true];
yield 'less or equal' => ['expression' => '[?(2<=2)]', 'expectMatch' => true];
yield 'greater or equal' => ['expression' => '[?(1>=2)]', 'expectMatch' => false];
yield 'json literal deep equal' => ['expression' => '[?({"a":1}=={"a":1})]', 'expectMatch' => true];
}
/**
* @throws JSONPathException
*/
#[DataProvider('constantExpressionProvider')]
public function testConstantExpressions(string $expression, bool $expectMatch): void
{
$data = ['keep'];
$result = new JSONPath($data)->find('$' . $expression)->getData();
self::assertSame($expectMatch ? ['keep'] : [], $result);
}
/**
* @throws JSONPathException
*/
public function testShorthandTokenValueArrayFiltersTruthyNodes(): void
{
$token = new JSONPathToken(TokenType::QueryMatch, ['expression' => '@', 'shorthand' => true]);
$filter = new QueryMatchFilter($token);
$collection = [0, 1, '', 'value', false];
self::assertSame([1 => 1, 3 => 'value'], $filter->filter($collection));
}
/**
* @throws JSONPathException
*/
public function testMalformedFilterThrowsRuntimeException(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Malformed filter query');
new JSONPath([1])->find('$[?(foo)]');
}
/**
* @throws JSONPathException
*/
public function testLiteralOnlyFilterExpressionsReturnWholeCollectionOrEmpty(): void
{
$data = [1, 2, 3];
self::assertSame($data, new JSONPath($data)->find('$[?(true)]')->getData());
self::assertSame([], new JSONPath($data)->find('$[?(false)]')->getData());
}
/**
* @throws JSONPathException
*/
public function testLogicalExpressionsWithLiteralRightOperand(): void
{
$data = [
['key' => 1],
['key' => -1],
];
self::assertSame(
[],
new JSONPath($data)->find('$[?(@.key>0 && false)]')->getData()
);
self::assertSame(
$data,
new JSONPath($data)->find('$[?(@.key>0 || true)]')->getData()
);
}
/**
* @throws JSONPathException
*/
public function testEmptyFilterExpressionReturnsEmpty(): void
{
self::assertSame([], new JSONPath([1, 2])->find('$[?()]')->getData());
}
/**
* @throws JSONPathException
*/
public function testNormalizeKeyCastsNumericStrings(): void
{
$token = new JSONPathToken(TokenType::QueryMatch, '@["2"]=="two"');
$filter = new QueryMatchFilter($token);
$result = $filter->filter([
['2' => 'two', '1' => 'one'],
['2' => 'nope', '1' => 'one'],
]);
self::assertSame([['2' => 'two', '1' => 'one']], \array_values($result));
}
}