Repository navigation
Expand file tree
/
Copy pathJSONPathArrayAccessTest.php
More file actions
139 lines (121 loc) · 3.87 KB
/
Copy pathJSONPathArrayAccessTest.php
File metadata and controls
139 lines (121 loc) · 3.87 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
<?php
/**
* JSONPath implementation for PHP.
*
* @copyright Copyright (c) 2018 Flow Communications
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
*/
namespace Flow\JSONPath\Test;
use Exception;
use PHPUnit\Framework\TestCase;
use Flow\JSONPath\JSONPath;
use function json_decode;
use function mt_rand;
class JSONPathArrayAccessTest extends TestCase
{
/**
* @throws Exception
*/
public function testChaining()
{
$data = $this->exampleData(mt_rand(0, 1));
$conferences = (new JSONPath($data))->find('.conferences.*');
$teams = $conferences->find('..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
$teams = (new JSONPath($data))->find('.conferences.*')->find('..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
$teams = (new JSONPath($data))->find('.conferences..teams.*');
self::assertEquals('Dodger', $teams[0]['name']);
self::assertEquals('Mets', $teams[1]['name']);
}
/**
* @throws Exception
*/
public function testIterating()
{
$data = $this->exampleData(mt_rand(0, 1));
$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()
{
$data = (new JSONPath($this->exampleData(mt_rand(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);
}
}
/**
* @param int $asArray
* @return array|object
*/
public function exampleData($asArray = 1)
{
$json = '{
"name":"Major League Baseball",
"abbr":"MLB",
"conferences":[
{
"name":"Western Conference",
"abbr":"West",
"teams":[
{
"name":"Dodger",
"city":"Los Angeles",
"whatever":"else",
"players":[
{
"name":"Bob Smith",
"number":22
},
{
"name":"Joe Face",
"number":23,
"active":"yes"
}
]
}
]
},
{
"name":"Eastern Conference",
"abbr":"East",
"teams":[
{
"name":"Mets",
"city":"New York",
"whatever":"else",
"players":[
{
"name":"something",
"number":14,
"active":"yes"
},
{
"name":"something",
"number":15
}
]
}
]
}
]
}';
return json_decode($json, $asArray === 1);
}
}