File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ of the document is represented by ``$``::
9797

9898
// $titles is ['Sayings of the Century']
9999

100-
Dot notation is the default, but JSONPath provides other syntaxes for cases where
101-
it doesn't work. Use bracket notation (``['...']``) when a key contains spaces
102-
or special characters::
100+
Dot notation is the default, but JSONPath provides other syntaxes for cases
101+
where it doesn't work. Use bracket notation (``['...']``) when a key contains
102+
spaces or special characters::
103103

104104
// this is equivalent to the previous example
105105
$titles = $crawler->find('$["store"]["book"][0]["title"]');
@@ -178,7 +178,8 @@ methods to build your query:
178178
* :method:`Symfony\\Component\\JsonPath\\JsonPath::index`
179179
Adds an array index selector. Index numbers start at ``0``.
180180

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

184185
// get the last book: '$["store"]["book"][-1]'
@@ -213,6 +214,86 @@ filters, refer to the `Querying with Expressions`_ section above. All these
213214
features are supported and can be combined with the programmatic builder when
214215
appropriate (e.g., inside a ``filter()`` expression).
215216

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

@@ -223,7 +304,8 @@ The component throws specific exceptions for invalid input or queries:
223304
* :class:`Symfony\\Component\\JsonPath\\Exception\\InvalidJsonStringInputException`:
224305
Thrown during a ``find()`` call if the JSON string is malformed (e.g., syntax error);
225306
* :class:`Symfony\\Component\\JsonPath\\Exception\\JsonCrawlerException`:
226-
Thrown for errors within the JsonPath expression itself, such as using an unknown function
307+
Thrown for errors within the JsonPath expression itself, such as using an
308+
unknown function
227309

228310
Example of handling errors::
229311

0 commit comments

Comments
 (0)