-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathQueryResultFilterTest.php
More file actions
135 lines (110 loc) · 3.56 KB
/
Copy pathQueryResultFilterTest.php
File metadata and controls
135 lines (110 loc) · 3.56 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
<?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\QueryResultFilter;
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;
#[CoversClass(QueryResultFilter::class)]
class QueryResultFilterTest extends TestCase
{
/**
* @throws JSONPathException
*/
public function testFilterResolvesComputedIndex(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.foo + 2');
$filter = new QueryResultFilter($token);
$collection = [
'foo' => 3,
5 => 'bar',
];
self::assertSame(['bar'], $filter->filter($collection));
}
/**
* @throws JSONPathException
*/
public function testFilterReturnsEmptyWhenLengthExceeded(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.length + 1');
$filter = new QueryResultFilter($token);
$collection = ['a', 'b'];
self::assertSame([], $filter->filter($collection));
}
/**
* @throws JSONPathException
*/
#[DataProvider('arithmeticProvider')]
public function testFilterSupportsAllArithmeticOperators(string $expression, int|float $expectedIndex): void
{
$token = new JSONPathToken(TokenType::QueryResult, $expression);
$filter = new QueryResultFilter($token);
$collection = [
'value' => 4,
$expectedIndex => 'found',
];
self::assertSame(['found'], $filter->filter($collection));
}
/**
* @return list<array{string, int|float}>
*/
public static function arithmeticProvider(): array
{
return [
['@.value - 1', 3],
['@.value * 2', 8],
['@.value / 2', 2],
];
}
/**
* @throws JSONPathException
*/
public function testFilterFallsBackToLengthWhenKeyMissing(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.length - 1');
$filter = new QueryResultFilter($token);
$collection = ['zero', 'one'];
self::assertSame(['one'], $filter->filter($collection));
}
/**
* @throws JSONPathException
*/
public function testFilterReturnsEmptyWhenKeyNotFound(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.missing + 1');
$filter = new QueryResultFilter($token);
self::assertSame([], $filter->filter(['foo' => 'bar']));
}
/**
* @throws JSONPathException
*/
public function testFilterReturnsEmptyWhenComputedIndexMissing(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.foo + 10');
$filter = new QueryResultFilter($token);
self::assertSame([], $filter->filter(['foo' => 1]));
}
/**
* @throws JSONPathException
*/
public function testFilterReturnsEmptyWhenResultKeyMissing(): void
{
$token = new JSONPathToken(TokenType::QueryResult, '@.foo + 100');
$filter = new QueryResultFilter($token);
self::assertSame([], $filter->filter(['foo' => 1]));
}
public function testUnsupportedOperatorThrows(): void
{
$this->expectException(JSONPathException::class);
$token = new JSONPathToken(TokenType::QueryResult, '@.foo ^ 2');
new QueryResultFilter($token)->filter(['foo' => 1]);
}
}