Skip to content

Commit 85bc74a

Browse files
committed
docs(json-path): add testing part doc for the component
Updates the documentation to document `JsonPathAssertionsTrait` and related constraints (closes #20977). Complete the previous work done in #21078. # Conflicts: # components/json_path.rst
1 parent 087a8d1 commit 85bc74a

File tree

1 file changed

+52
-56
lines changed

1 file changed

+52
-56
lines changed

components/json_path.rst

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,12 @@ the data. This method always returns an array of matching values.
8181
Querying with Expressions
8282
-------------------------
8383

84-
The primary way to query the JSON is by passing a JSONPath expression string
85-
to the :method:`Symfony\\Component\\JsonPath\\JsonCrawler::find` method.
84+
The primary way to query the JSON is by passing a JSONPath expression string to the :method:`Symfony\\Component\\JsonPath\\JsonCrawler::find` method.
8685

8786
Accessing a Specific Property
8887
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8988

90-
Use dot-notation for object keys and square brackets for array indices. The root
91-
of the document is represented by ``$``::
89+
Use dot-notation for object keys and square brackets for array indices. The root of the document is represented by ``$``::
9290

9391
// Get the title of the first book in the store
9492
$titles = $crawler->find('$.store.book[0].title');
@@ -157,53 +155,51 @@ the correct escaping of keys and values, preventing syntax errors::
157155
The :class:`Symfony\\Component\\JsonPath\\JsonPath` class provides several methods to build your query:
158156

159157
* :method:`Symfony\\Component\\JsonPath\\JsonPath::key`
160-
Adds a key selector. The key name will be properly escaped::
158+
Adds a key selector. The key name will be properly escaped::
161159

162-
// Creates the path '$["key\"with\"quotes"]'
163-
$path = (new JsonPath())->key('key"with"quotes');
160+
// Creates the path '$["key\"with\"quotes"]'
161+
$path = (new JsonPath())->key('key"with"quotes');
164162

165163
* :method:`Symfony\\Component\\JsonPath\\JsonPath::deepScan`
166-
Adds the descendant operator ``..`` to perform a recursive search from the
167-
current point in the path::
164+
Adds the descendant operator ``..`` to perform a recursive search from the current point in the path::
168165

169-
// Get all prices in the store: '$["store"]..["price"]'
170-
$path = (new JsonPath())->key('store')->deepScan()->key('price');
166+
// Get all prices in the store: '$["store"]..["price"]'
167+
$path = (new JsonPath())->key('store')->deepScan()->key('price');
171168

172169
* :method:`Symfony\\Component\\JsonPath\\JsonPath::all`
173-
Adds the wildcard operator ``[*]`` to select all items in an array or object::
170+
Adds the wildcard operator ``[*]`` to select all items in an array or object::
174171

175-
// Creates the path '$["store"]["book"][*]'
176-
$path = (new JsonPath())->key('store')->key('book')->all();
172+
// Creates the path '$["store"]["book"][*]'
173+
$path = (new JsonPath())->key('store')->key('book')->all();
177174

178175
* :method:`Symfony\\Component\\JsonPath\\JsonPath::index`
179-
Adds an array index selector.
176+
Adds an array index selector.
180177

181178
* :method:`Symfony\\Component\\JsonPath\\JsonPath::first` / :method:`Symfony\\Component\\JsonPath\\JsonPath::last`
182-
Shortcuts for ``index(0)`` and ``index(-1)`` respectively::
179+
Shortcuts for ``index(0)`` and ``index(-1)`` respectively::
183180

184-
// Get the last book: '$["store"]["book"][-1]'
185-
$path = (new JsonPath())->key('store')->key('book')->last();
181+
// Get the last book: '$["store"]["book"][-1]'
182+
$path = (new JsonPath())->key('store')->key('book')->last();
186183

187184
* :method:`Symfony\\Component\\JsonPath\\JsonPath::slice`
188-
Adds an array slice selector ``[start:end:step]``::
185+
Adds an array slice selector ``[start:end:step]``::
189186

190-
// Get books from index 1 up to (but not including) index 3
191-
// Creates the path '$["store"]["book"][1:3]'
192-
$path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
187+
// Get books from index 1 up to (but not including) index 3
188+
// Creates the path '$["store"]["book"][1:3]'
189+
$path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
193190

194-
// Get every second book from the first four books
195-
// Creates the path '$["store"]["book"][0:4:2]'
196-
$path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
191+
// Get every second book from the first four books
192+
// Creates the path '$["store"]["book"][0:4:2]'
193+
$path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
197194

198195
* :method:`Symfony\\Component\\JsonPath\\JsonPath::filter`
199-
Adds a filter expression. The expression string is the part that goes inside
200-
the ``?()`` syntax::
196+
Adds a filter expression. The expression string is the part that goes inside the ``?()`` syntax::
201197

202-
// Get expensive books: '$["store"]["book"][?(@.price > 20)]'
203-
$path = (new JsonPath())
204-
->key('store')
205-
->key('book')
206-
->filter('@.price > 20');
198+
// Get expensive books: '$["store"]["book"][?(@.price > 20)]'
199+
$path = (new JsonPath())
200+
->key('store')
201+
->key('book')
202+
->filter('@.price > 20');
207203

208204
Advanced Querying
209205
-----------------
@@ -237,52 +233,52 @@ The component provides a set of PHPUnit assertions to make testing JSON data mor
237233
The trait provides the following assertion methods:
238234

239235
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathCount`
240-
Asserts that the number of elements found by the JSONPath expression matches an expected count::
236+
Asserts that the number of elements found by the JSONPath expression matches an expected count::
241237

242-
$json = '{"a": [1, 2, 3]}';
243-
self::assertJsonPathCount(3, '$.a[*]', $json);
238+
$json = '{"a": [1, 2, 3]}';
239+
self::assertJsonPathCount(3, '$.a[*]', $json);
244240

245241
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathEquals`
246-
Asserts that the result of a JSONPath expression is equal (``==``) to an expected value. This assertion uses type coercion::
242+
Asserts that the result of a JSONPath expression is equal to an expected value.The comparison uses ``==`` (type coercion) instead of ``===``::
247243

248-
$json = '{"a": [1, 2, 3]}';
244+
$json = '{"a": [1, 2, 3]}';
249245

250-
// passes because "1" == 1
251-
self::assertJsonPathEquals(['1'], '$.a[0]', $json);
246+
// passes because "1" == 1
247+
self::assertJsonPathEquals(['1'], '$.a[0]', $json);
252248

253249
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathNotEquals`
254-
Asserts that the result of a JSONPath expression is not equal (``!=``) to an expected value::
250+
Asserts that the result of a JSONPath expression is not equal to an expected value.The comparison uses ``!=`` (type coercion) instead of ``!==``::
255251

256-
$json = '{"a": [1, 2, 3]}';
257-
self::assertJsonPathNotEquals([42], '$.a[0]', $json);
252+
$json = '{"a": [1, 2, 3]}';
253+
self::assertJsonPathNotEquals([42], '$.a[0]', $json);
258254

259255
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathSame`
260-
Asserts that the result of a JSONPath expression is identical (``===``) to an expected value. This is a strict comparison and does not perform type coercion::
256+
Asserts that the result of a JSONPath expression is identical (``===``) to an expected value. This is a strict comparison and does not perform type coercion::
261257

262-
$json = '{"a": [1, 2, 3]}';
258+
$json = '{"a": [1, 2, 3]}';
263259

264-
// fails because "1" !== 1
265-
// self::assertJsonPathSame(['1'], '$.a[0]', $json);
260+
// fails because "1" !== 1
261+
// self::assertJsonPathSame(['1'], '$.a[0]', $json);
266262

267-
self::assertJsonPathSame([1], '$.a[0]', $json);
263+
self::assertJsonPathSame([1], '$.a[0]', $json);
268264

269265
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathNotSame`
270-
Asserts that the result of a JSONPath expression is not identical (``!==``) to an expected value::
266+
Asserts that the result of a JSONPath expression is not identical (``!==``) to an expected value::
271267

272-
$json = '{"a": [1, 2, 3]}';
273-
self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
268+
$json = '{"a": [1, 2, 3]}';
269+
self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
274270

275271
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathContains`
276-
Asserts that a given value is found within the array of results from the JSONPath expression::
272+
Asserts that a given value is found within the array of results from the JSONPath expression::
277273

278-
$json = '{"tags": ["php", "symfony", "json"]}';
279-
self::assertJsonPathContains('symfony', '$.tags[*]', $json);
274+
$json = '{"tags": ["php", "symfony", "json"]}';
275+
self::assertJsonPathContains('symfony', '$.tags[*]', $json);
280276

281277
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathNotContains`
282-
Asserts that a given value is NOT found within the array of results from the JSONPath expression::
278+
Asserts that a given value is NOT found within the array of results from the JSONPath expression::
283279

284-
$json = '{"tags": ["php", "symfony", "json"]}';
285-
self::assertJsonPathNotContains('java', '$.tags[*]', $json);
280+
$json = '{"tags": ["php", "symfony", "json"]}';
281+
self::assertJsonPathNotContains('java', '$.tags[*]', $json);
286282

287283
Error Handling
288284
--------------

0 commit comments

Comments
 (0)