Skip to content

Commit 8d20ebb

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 8d20ebb

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

components/json_path.rst

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -157,53 +157,51 @@ the correct escaping of keys and values, preventing syntax errors::
157157
The :class:`Symfony\\Component\\JsonPath\\JsonPath` class provides several methods to build your query:
158158

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

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

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

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

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

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

178177
* :method:`Symfony\\Component\\JsonPath\\JsonPath::index`
179-
Adds an array index selector.
178+
Adds an array index selector.
180179

181180
* :method:`Symfony\\Component\\JsonPath\\JsonPath::first` / :method:`Symfony\\Component\\JsonPath\\JsonPath::last`
182-
Shortcuts for ``index(0)`` and ``index(-1)`` respectively::
181+
Shortcuts for ``index(0)`` and ``index(-1)`` respectively::
183182

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

187186
* :method:`Symfony\\Component\\JsonPath\\JsonPath::slice`
188-
Adds an array slice selector ``[start:end:step]``::
187+
Adds an array slice selector ``[start:end:step]``::
189188

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);
189+
// Get books from index 1 up to (but not including) index 3
190+
// Creates the path '$["store"]["book"][1:3]'
191+
$path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
193192

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);
193+
// Get every second book from the first four books
194+
// Creates the path '$["store"]["book"][0:4:2]'
195+
$path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
197196

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

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

208206
Advanced Querying
209207
-----------------
@@ -216,7 +214,7 @@ appropriate (e.g., inside a ``filter()`` expression).
216214
Testing with JSON Assertions
217215
----------------------------
218216

219-
The component provides a set of PHPUnit assertions to make testing JSON data more convenient. To use them, include the
217+
The component provides a set of PHPUnit assertions to make testing JSON data more convenient. Use the
220218
:class:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait` in your test class::
221219

222220
use PHPUnit\Framework\TestCase;
@@ -237,52 +235,52 @@ The component provides a set of PHPUnit assertions to make testing JSON data mor
237235
The trait provides the following assertion methods:
238236

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

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

245243
* :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::
244+
Asserts that the result of a JSONPath expression is equal to an expected value.The comparison uses ``==`` (type coercion) instead of ``===``::
247245

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

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

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

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

259257
* :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::
258+
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::
261259

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

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

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

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

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

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

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

281279
* :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::
280+
Asserts that a given value is NOT found within the array of results from the JSONPath expression::
283281

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

287285
Error Handling
288286
--------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy