@@ -49,6 +49,19 @@ public function testAllAuthors()
49
49
], $ result );
50
50
}
51
51
52
+ public function testAllAuthorsWithBrackets ()
53
+ {
54
+ $ result = self ::getBookstoreCrawler ()->find ('$..["author"] ' );
55
+
56
+ $ this ->assertCount (4 , $ result );
57
+ $ this ->assertSame ([
58
+ 'Nigel Rees ' ,
59
+ 'Evelyn Waugh ' ,
60
+ 'Herman Melville ' ,
61
+ 'J. R. R. Tolkien ' ,
62
+ ], $ result );
63
+ }
64
+
52
65
public function testAllThingsInStore ()
53
66
{
54
67
$ result = self ::getBookstoreCrawler ()->find ('$.store.* ' );
@@ -58,6 +71,15 @@ public function testAllThingsInStore()
58
71
$ this ->assertArrayHasKey ('color ' , $ result [1 ]);
59
72
}
60
73
74
+ public function testAllThingsInStoreWithBrackets ()
75
+ {
76
+ $ result = self ::getBookstoreCrawler ()->find ('$["store"][*] ' );
77
+
78
+ $ this ->assertCount (2 , $ result );
79
+ $ this ->assertCount (4 , $ result [0 ]);
80
+ $ this ->assertArrayHasKey ('color ' , $ result [1 ]);
81
+ }
82
+
61
83
public function testEscapedDoubleQuotesInFieldName ()
62
84
{
63
85
$ crawler = new JsonCrawler (<<<JSON
@@ -77,6 +99,14 @@ public function testBasicNameSelector()
77
99
$ this ->assertSame ('Nigel Rees ' , $ result [0 ]['author ' ]);
78
100
}
79
101
102
+ public function testBasicNameSelectorWithBrackts ()
103
+ {
104
+ $ result = self ::getBookstoreCrawler ()->find ('$["store"]["book"] ' )[0 ];
105
+
106
+ $ this ->assertCount (4 , $ result );
107
+ $ this ->assertSame ('Nigel Rees ' , $ result [0 ]['author ' ]);
108
+ }
109
+
80
110
public function testAllPrices ()
81
111
{
82
112
$ result = self ::getBookstoreCrawler ()->find ('$.store..price ' );
@@ -121,6 +151,17 @@ public function testBooksWithIsbn()
121
151
], [$ result [0 ]['isbn ' ], $ result [1 ]['isbn ' ]]);
122
152
}
123
153
154
+ public function testBooksWithBracketsAndFilter ()
155
+ {
156
+ $ result = self ::getBookstoreCrawler ()->find ('$..["book"][?(@.isbn)] ' );
157
+
158
+ $ this ->assertCount (2 , $ result );
159
+ $ this ->assertSame ([
160
+ '0-553-21311-3 ' ,
161
+ '0-395-19395-8 ' ,
162
+ ], [$ result [0 ]['isbn ' ], $ result [1 ]['isbn ' ]]);
163
+ }
164
+
124
165
public function testBooksLessThanTenDollars ()
125
166
{
126
167
$ result = self ::getBookstoreCrawler ()->find ('$..book[?(@.price < 10)] ' );
@@ -216,6 +257,14 @@ public function testEverySecondElementReverseSlice()
216
257
$ this ->assertSame ([6 , 2 , 5 ], $ result );
217
258
}
218
259
260
+ public function testEverySecondElementReverseSliceAndBrackets ()
261
+ {
262
+ $ crawler = self ::getSimpleCollectionCrawler ();
263
+
264
+ $ result = $ crawler ->find ('$["a"][::-2] ' );
265
+ $ this ->assertSame ([6 , 2 , 5 ], $ result );
266
+ }
267
+
219
268
public function testEmptyResults ()
220
269
{
221
270
$ crawler = self ::getSimpleCollectionCrawler ();
@@ -404,6 +453,19 @@ public function testAcceptsJsonPath()
404
453
$ this ->assertSame ('red ' , $ result [0 ]['color ' ]);
405
454
}
406
455
456
+ public function testStarAsKey ()
457
+ {
458
+ $ crawler = new JsonCrawler (<<<JSON
459
+ {"*": {"a": 1, "b": 2}, "something else": {"c": 3}}
460
+ JSON );
461
+
462
+ $ result = $ crawler ->find ('$["*"] ' );
463
+
464
+ $ this ->assertCount (1 , $ result );
465
+ $ this ->assertSame (['a ' => 1 , 'b ' => 2 ], $ result [0 ]);
466
+ }
467
+
468
+
407
469
private static function getBookstoreCrawler (): JsonCrawler
408
470
{
409
471
return new JsonCrawler (<<<JSON
0 commit comments