-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathIndexFilterTest.php
More file actions
87 lines (71 loc) · 2.29 KB
/
Copy pathIndexFilterTest.php
File metadata and controls
87 lines (71 loc) · 2.29 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
<?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 ArrayObject;
use Flow\JSONPath\Filters\IndexFilter;
use Flow\JSONPath\JSONPath;
use Flow\JSONPath\JSONPathException;
use Flow\JSONPath\JSONPathToken;
use Flow\JSONPath\TokenType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(IndexFilter::class)]
class IndexFilterTest extends TestCase
{
/**
* @throws JSONPathException
*/
public function testArrayValueTokenReturnsOnlyExistingKeys(): void
{
$token = new JSONPathToken(TokenType::Index, [0, 2, 99]);
$filter = new IndexFilter($token);
self::assertSame(
['first', 'third'],
$filter->filter(['first', 'second', 'third'])
);
}
/**
* @throws JSONPathException
*/
public function testSingleIndexWorksForObjectsAndArrayAccess(): void
{
$token = new JSONPathToken(TokenType::Index, 'prop');
$filter = new IndexFilter($token);
$object = (object)['prop' => 5];
self::assertSame([5], $filter->filter($object));
$arrayObject = new ArrayObject(['prop' => 'value']);
self::assertSame(['value'], $filter->filter($arrayObject));
}
/**
* @throws JSONPathException
*/
public function testWildcardReturnsValuesAndLengthReturnsCount(): void
{
$wildcard = new IndexFilter(new JSONPathToken(TokenType::Index, '*'));
$length = new IndexFilter(new JSONPathToken(TokenType::Index, 'length'));
$input = ['a' => 1, 'b' => 2];
self::assertSame([1, 2], $wildcard->filter($input));
self::assertSame([2], $length->filter($input));
}
/**
* @throws JSONPathException
*/
public function testReturnsEmptyWhenKeyMissing(): void
{
$filter = new IndexFilter(new JSONPathToken(TokenType::Index, 'missing'));
self::assertSame([], $filter->filter(['present' => 1]));
}
/**
* @throws JSONPathException
*/
public function testJSONPathFindOnScalarProducesEmptyCollection(): void
{
$result = new JSONPath(123)->find('$.missing');
self::assertSame([], $result->getData());
}
}