Skip to content

Commit 59965aa

Browse files
[JsonPath] Add JsonPathAssertionsTrait
1 parent 1a8b82e commit 59965aa

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonPath\Test;
13+
14+
use Symfony\Component\JsonPath\JsonCrawler;
15+
use Symfony\Component\JsonPath\JsonPath;
16+
17+
/**
18+
* @author Alexandre Daubois <alex.daubois@gmail.com>
19+
*
20+
* @experimental
21+
*/
22+
trait JsonPathAssertionsTrait
23+
{
24+
public static function assertJsonPathEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
25+
{
26+
self::assertEquals($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
27+
}
28+
29+
public static function assertJsonPathNotEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
30+
{
31+
self::assertNotEquals($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
32+
}
33+
34+
public static function assertJsonPathCount(int $expectedCount, JsonPath|string $jsonPath, string $json, string $message = ''): void
35+
{
36+
$actualCount = \count((new JsonCrawler($json))->find($jsonPath));
37+
38+
self::assertEquals($expectedCount, $actualCount, $message);
39+
}
40+
41+
public static function assertJsonPathSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
42+
{
43+
self::assertSame($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
44+
}
45+
46+
public static function assertJsonPathNotSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
47+
{
48+
self::assertNotSame($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
49+
}
50+
51+
public static function assertJsonPathContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
52+
{
53+
self::assertContains($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
54+
}
55+
56+
public static function assertJsonPathNotContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
57+
{
58+
self::assertNotContains($expectedValue, self::evaluateJsonPath($jsonPath, $json), $message);
59+
}
60+
61+
private static function evaluateJsonPath(JsonPath|string $jsonPath, string $json): mixed
62+
{
63+
return (new JsonCrawler($json))->find($jsonPath);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Symfony\Component\JsonPath\Tests\Test;
4+
5+
use PHPUnit\Framework\AssertionFailedError;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\JsonPath\Test\JsonPathAssertionsTrait;
8+
9+
class JsonPathAssertionsTraitTest extends TestCase
10+
{
11+
use JsonPathAssertionsTrait;
12+
13+
public function testAssertJsonPathEqualsOk()
14+
{
15+
self::assertJsonPathEquals([1], '$.a[2]', self::getSimpleCollectionCrawlerData());
16+
}
17+
18+
public function testAssertJsonPathEqualsOkWithTypeCoercion()
19+
{
20+
self::assertJsonPathEquals(['1'], '$.a[2]', self::getSimpleCollectionCrawlerData());
21+
}
22+
23+
public function testAssertJsonPathEqualsKo()
24+
{
25+
try {
26+
self::assertJsonPathEquals([2], '$.a[2]', self::getSimpleCollectionCrawlerData());
27+
28+
$this->fail();
29+
} catch (AssertionFailedError $exception) {
30+
self::assertSame('Failed asserting that two arrays are equal.', $exception->getMessage());
31+
}
32+
}
33+
34+
public function testAssertJsonPathNotEqualsOk()
35+
{
36+
self::assertJsonPathNotEquals([2], '$.a[2]', self::getSimpleCollectionCrawlerData());
37+
}
38+
39+
public function testAssertJsonPathNotEqualsKo()
40+
{
41+
try {
42+
self::assertJsonPathNotEquals([1], '$.a[2]', self::getSimpleCollectionCrawlerData());
43+
44+
$this->fail();
45+
} catch (AssertionFailedError $exception) {
46+
self::assertMatchesRegularExpression('/^Failed asserting that .+ is not equal to .+\.$/s', $exception->getMessage());
47+
}
48+
}
49+
50+
public function testAssertJsonPathCountOk()
51+
{
52+
self::assertJsonPathCount(6, '$.a[*]', self::getSimpleCollectionCrawlerData());
53+
}
54+
55+
public function testAssertJsonPathCountOkWithFilter()
56+
{
57+
self::assertJsonPathCount(2, '$.book[?(@.price > 25)]', <<<JSON
58+
{
59+
"book": [
60+
{ "price": 10 },
61+
{ "price": 20 },
62+
{ "price": 30 },
63+
{ "price": 40 }
64+
]
65+
}
66+
JSON
67+
);
68+
}
69+
70+
public function testAssertJsonPathCountKo()
71+
{
72+
try {
73+
self::assertJsonPathCount(5, '$.a[*]', self::getSimpleCollectionCrawlerData());
74+
75+
$this->fail();
76+
} catch (AssertionFailedError $exception) {
77+
self::assertSame('Failed asserting that 6 matches expected 5.', $exception->getMessage());
78+
}
79+
}
80+
81+
public function testAssertJsonPathSameOk()
82+
{
83+
self::assertJsonPathSame([1], '$.a[2]', self::getSimpleCollectionCrawlerData());
84+
}
85+
86+
public function testAssertJsonPathSameKo()
87+
{
88+
try {
89+
self::assertJsonPathSame([2], '$.a[2]', self::getSimpleCollectionCrawlerData());
90+
91+
$this->fail();
92+
} catch (AssertionFailedError $exception) {
93+
self::assertSame('Failed asserting that two arrays are identical.', $exception->getMessage());
94+
}
95+
}
96+
97+
public function testAssertJsonPathHasNoTypeCoercion()
98+
{
99+
try {
100+
self::assertJsonPathSame(['1'], '$.a[2]', self::getSimpleCollectionCrawlerData());
101+
102+
$this->fail();
103+
} catch (AssertionFailedError $exception) {
104+
self::assertSame('Failed asserting that two arrays are identical.', $exception->getMessage());
105+
}
106+
}
107+
108+
public function testAssertJsonPathNotSameOk()
109+
{
110+
self::assertJsonPathNotSame([2], '$.a[2]', self::getSimpleCollectionCrawlerData());
111+
}
112+
113+
public function testAssertJsonPathNotSameKo()
114+
{
115+
try {
116+
self::assertJsonPathNotSame([1], '$.a[2]', self::getSimpleCollectionCrawlerData());
117+
118+
$this->fail();
119+
} catch (AssertionFailedError $exception) {
120+
self::assertSame('Failed asserting that two arrays are not identical.', $exception->getMessage());
121+
}
122+
}
123+
124+
public function testAssertJsonPathNotSameHasNoTypeCoercion()
125+
{
126+
self::assertJsonPathNotSame(['1'], '$.a[2]', self::getSimpleCollectionCrawlerData());
127+
}
128+
129+
public function testAssertJsonPathContainsOk()
130+
{
131+
self::assertJsonPathContains(1, '$.a[*]', self::getSimpleCollectionCrawlerData());
132+
}
133+
134+
public function testAssertJsonPathContainsKo()
135+
{
136+
try {
137+
self::assertJsonPathContains(0, '$.a[*]', self::getSimpleCollectionCrawlerData());
138+
139+
$this->fail();
140+
} catch (AssertionFailedError $exception) {
141+
self::assertSame('Failed asserting that an array contains 0.', $exception->getMessage());
142+
}
143+
}
144+
145+
public function testAssertJsonPathNotContainsOk()
146+
{
147+
self::assertJsonPathNotContains(0, '$.a[*]', self::getSimpleCollectionCrawlerData());
148+
}
149+
150+
public function testAssertJsonPathNotContainsKo()
151+
{
152+
try {
153+
self::assertJsonPathNotContains(1, '$.a[*]', self::getSimpleCollectionCrawlerData());
154+
155+
$this->fail();
156+
} catch (AssertionFailedError $exception) {
157+
self::assertSame('Failed asserting that an array does not contain 1.', $exception->getMessage());
158+
}
159+
}
160+
161+
private static function getSimpleCollectionCrawlerData(): string
162+
{
163+
return <<<JSON
164+
{"a": [3, 5, 1, 2, 4, 6]}
165+
JSON;
166+
}
167+
}

0 commit comments

Comments
 (0)