forked from SoftCreatR/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONPathArrayTest.php
More file actions
91 lines (69 loc) · 2.21 KB
/
JSONPathArrayTest.php
File metadata and controls
91 lines (69 loc) · 2.21 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
<?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 Exception;
use Flow\JSONPath\JSONPath;
use Flow\JSONPath\Test\Traits\TestDataTrait;
use PHPUnit\Framework\TestCase;
class JSONPathArrayTest extends TestCase
{
use TestDataTrait;
/**
* @throws Exception
*/
public function testChaining(): void
{
$jsonPath = (new JSONPath($this->getData('conferences')));
$teams = $jsonPath
->find('.conferences.*')
->find('..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
$teams = $jsonPath
->find('.conferences.*')
->find('..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
$teams = $jsonPath
->find('.conferences..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
}
/**
* @throws Exception
*/
public function testIterating(): void
{
$data = $this->getData('conferences');
$conferences = (new JSONPath($data))
->find('.conferences.*');
$names = [];
foreach ($conferences as $conference) {
$players = $conference
->find('.teams.*.players[?(@.active=yes)]');
foreach ($players as $player) {
$names[] = $player->name;
}
}
self::assertEquals(['Joe Face', 'something'], $names);
}
/**
* @throws Exception
*/
public function testDifferentStylesOfAccess(): void
{
$data = (new JSONPath($this->getData('conferences', \random_int(0, 1))));
self::assertArrayHasKey('conferences', $data);
$conferences = $data->__get('conferences')->getData();
if (\is_array($conferences[0])) {
self::assertEquals('Western Conference', $conferences[0]['name']);
} else {
self::assertEquals('Western Conference', $conferences[0]->name);
}
}
}