Skip to content

Commit b1c2340

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.
1 parent 870b265 commit b1c2340

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

components/json_path.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,78 @@ filters, please refer to the `Querying with Expressions`_ section above. All the
213213
features are supported and can be combined with the programmatic builder where
214214
appropriate (e.g., inside a ``filter()`` expression).
215215

216+
Testing with JSON Assertions
217+
----------------------------
218+
219+
The component provides a set of PHPUnit assertions to make testing JSON data more convenient. To use them, include the
220+
:class:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait` in your test class::
221+
222+
use PHPUnit\Framework\TestCase;
223+
use Symfony\Component\JsonPath\Test\JsonPathAssertionsTrait;
224+
225+
class MyTest extends TestCase
226+
{
227+
use JsonPathAssertionsTrait;
228+
229+
public function testSomething(): void
230+
{
231+
$json = '{"books": [{"title": "A"}, {"title": "B"}]}';
232+
233+
self::assertJsonPathCount(2, '$.books[*]', $json);
234+
}
235+
}
236+
237+
The trait provides the following assertion methods:
238+
239+
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathCount`
240+
Asserts that the number of elements found by the JSONPath expression matches an expected count::
241+
242+
$json = '{"a": [1, 2, 3]}';
243+
self::assertJsonPathCount(3, '$.a[*]', $json);
244+
245+
* :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::
247+
248+
$json = '{"a": [1, 2, 3]}';
249+
250+
// passes because "1" == 1
251+
self::assertJsonPathEquals(['1'], '$.a[0]', $json);
252+
253+
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathNotEquals`
254+
Asserts that the result of a JSONPath expression is not equal (``!=``) to an expected value::
255+
256+
$json = '{"a": [1, 2, 3]}';
257+
self::assertJsonPathNotEquals([42], '$.a[0]', $json);
258+
259+
* :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::
261+
262+
$json = '{"a": [1, 2, 3]}';
263+
264+
// fails because "1" !== 1
265+
// self::assertJsonPathSame(['1'], '$.a[0]', $json);
266+
267+
self::assertJsonPathSame([1], '$.a[0]', $json);
268+
269+
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathNotSame`
270+
Asserts that the result of a JSONPath expression is not identical (``!==``) to an expected value::
271+
272+
$json = '{"a": [1, 2, 3]}';
273+
self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
274+
275+
* :method:`Symfony\\Component\\JsonPath\\Test\\JsonPathAssertionsTrait::assertJsonPathContains`
276+
Asserts that a given value is found within the array of results from the JSONPath expression::
277+
278+
$json = '{"tags": ["php", "symfony", "json"]}';
279+
self::assertJsonPathContains('symfony', '$.tags[*]', $json);
280+
281+
* :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::
283+
284+
$json = '{"tags": ["php", "symfony", "json"]}';
285+
self::assertJsonPathNotContains('java', '$.tags[*]', $json);
286+
287+
216288
Error Handling
217289
--------------
218290

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