@@ -30,9 +30,9 @@ You can install the component in your project using Composer:
3030Usage
3131-----
3232
33- To start querying a JSON document, first create a :class: ` Symfony \\ Component \\ JsonPath \\ JsonCrawler `
34- object from a JSON string. The following examples use this sample "bookstore"
35- JSON data::
33+ To start querying a JSON document, first create a
34+ :class: ` Symfony \\ Component \\ JsonPath \\ JsonCrawler` object from a JSON string. The
35+ following examples use this sample "bookstore" JSON data::
3636
3737 use Symfony\C omponent\J sonPath\J sonCrawler;
3838
@@ -77,8 +77,9 @@ JSON data::
7777
7878 $crawler = new JsonCrawler($json);
7979
80- Once you have the crawler instance, use its :method: `Symfony\\ Component\\ JsonPath\\ JsonCrawler::find `
81- method to start querying the data. This method returns an array of matching values.
80+ Once you have the crawler instance, use its
81+ :method:`Symfony\\ Component\\ JsonPath\\ JsonCrawler::find ` method to start
82+ querying the data. This method returns an array of matching values.
8283
8384Querying with Expressions
8485-------------------------
@@ -97,9 +98,9 @@ of the document is represented by ``$``::
9798
9899 // $titles is ['Sayings of the Century']
99100
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::
101+ Dot notation is the default, but JSONPath provides other syntaxes for cases
102+ where it doesn't work. Use bracket notation (``['...'] ``) when a key contains
103+ spaces or special characters::
103104
104105 // this is equivalent to the previous example
105106 $titles = $crawler->find('$["store"]["book"][0]["title"]');
@@ -119,7 +120,12 @@ you to find values without specifying the full path::
119120 // get all authors from anywhere in the document
120121 $authors = $crawler->find('$..author');
121122
122- // $authors is ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'John Ronald Reuel Tolkien']
123+ // $authors is equals to [
124+ 'Nigel Rees',
125+ 'Evelyn Waugh',
126+ 'Herman Melville',
127+ 'John Ronald Reuel Tolkien'
128+ ]
123129
124130Filtering Results
125131~~~~~~~~~~~~~~~~~
@@ -135,8 +141,9 @@ Building Queries Programmatically
135141
136142For more dynamic or complex query building, use the fluent API provided
137143by the :class: `Symfony\\ Component\\ JsonPath\\ JsonPath ` class. This lets you
138- construct a query object step by step. The ``JsonPath `` object can then be passed
139- to the crawler's :method: `Symfony\\ Component\\ JsonPath\\ JsonCrawler::find ` method.
144+ construct a query object step by step. The ``JsonPath `` object can then be
145+ passed to the crawler's
146+ :method: `Symfony\\ Component\\ JsonPath\\ JsonCrawler::find ` method.
140147
141148The main advantage of the programmatic builder is that it automatically handles
142149escaping of keys and values, preventing syntax errors::
@@ -178,7 +185,8 @@ methods to build your query:
178185* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::index `
179186 Adds an array index selector. Index numbers start at ``0 ``.
180187
181- * :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::first ` / :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::last `
188+ * :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::first ` /
189+ :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::last `
182190 Shortcuts for ``index(0) `` and ``index(-1) `` respectively::
183191
184192 // get the last book: '$["store"]["book"][-1]'
@@ -213,17 +221,101 @@ filters, refer to the `Querying with Expressions`_ section above. All these
213221features are supported and can be combined with the programmatic builder when
214222appropriate (e.g., inside a ``filter() `` expression).
215223
224+ Testing with JSON Assertions
225+ ----------------------------
226+
227+ The component provides a set of PHPUnit assertions to make testing JSON data
228+ more convenient. Use the
229+ :class: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait `
230+ in your test class::
231+
232+ use PHPUnit\Framework\TestCase;
233+ use Symfony\Component\JsonPath\Test\JsonPathAssertionsTrait;
234+
235+ class MyTest extends TestCase
236+ {
237+ use JsonPathAssertionsTrait;
238+
239+ public function testSomething(): void
240+ {
241+ $json = '{"books": [{"title": "A"}, {"title": "B"}]}';
242+
243+ self::assertJsonPathCount(2, '$.books[*]', $json);
244+ }
245+ }
246+
247+ The trait provides the following assertion methods:
248+
249+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathCount `
250+ Asserts that the number of elements found by the JSONPath expression matches
251+ an expected count::
252+
253+ $json = '{"a": [1, 2, 3]}';
254+ self::assertJsonPathCount(3, '$.a[*]', $json);
255+
256+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathEquals `
257+ Asserts that the result of a JSONPath expression is equal to an expected
258+ value. The comparison uses ``== `` (type coercion) instead of ``=== ``::
259+
260+ $json = '{"a": [1, 2, 3]}';
261+
262+ // passes because "1" == 1
263+ self::assertJsonPathEquals(['1'], '$.a[0]', $json);
264+
265+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotEquals `
266+ Asserts that the result of a JSONPath expression is not equal to an expected
267+ value. The comparison uses ``!= `` (type coercion) instead of ``!== ``::
268+
269+ $json = '{"a": [1, 2, 3]}';
270+ self::assertJsonPathNotEquals([42], '$.a[0]', $json);
271+
272+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathSame `
273+ Asserts that the result of a JSONPath expression is identical (``=== ``) to an
274+ expected value. This is a strict comparison and does not perform type
275+ coercion::
276+
277+ $json = '{"a": [1, 2, 3]}';
278+
279+ // fails because "1" !== 1
280+ // self::assertJsonPathSame(['1'], '$.a[0]', $json);
281+
282+ self::assertJsonPathSame([1], '$.a[0]', $json);
283+
284+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotSame `
285+ Asserts that the result of a JSONPath expression is not identical (``!== ``) to
286+ an expected value::
287+
288+ $json = '{"a": [1, 2, 3]}';
289+ self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
290+
291+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathContains `
292+ Asserts that a given value is found within the array of results from the
293+ JSONPath expression::
294+
295+ $json = '{"tags": ["php", "symfony", "json"]}';
296+ self::assertJsonPathContains('symfony', '$.tags[*]', $json);
297+
298+ * :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotContains `
299+ Asserts that a given value is NOT found within the array of results from the
300+ JSONPath expression::
301+
302+ $json = '{"tags": ["php", "symfony", "json"]}';
303+ self::assertJsonPathNotContains('java', '$.tags[*]', $json);
304+
216305Error Handling
217306--------------
218307
219308The component throws specific exceptions for invalid input or queries:
220309
221310* :class: `Symfony\\ Component\\ JsonPath\\ Exception\\ InvalidArgumentException `:
222- Thrown if the input to the ``JsonCrawler `` constructor is not a valid JSON string;
311+ Thrown if the input to the ``JsonCrawler `` constructor is not a valid JSON
312+ string;
223313* :class: `Symfony\\ Component\\ JsonPath\\ Exception\\ InvalidJsonStringInputException `:
224- Thrown during a ``find() `` call if the JSON string is malformed (e.g., syntax error);
314+ Thrown during a ``find() `` call if the JSON string is malformed
315+ (e.g., syntax error);
225316* :class: `Symfony\\ Component\\ JsonPath\\ Exception\\ JsonCrawlerException `:
226- Thrown for errors within the JsonPath expression itself, such as using an unknown function
317+ Thrown for errors within the JsonPath expression itself, such as using an
318+ unknown function
227319
228320Example of handling errors::
229321
0 commit comments