Skip to content

Commit bbe4a62

Browse files
[JsonPath] Add JsonPathAssertionsTrait and related constraints
1 parent 1a8b82e commit bbe4a62

File tree

9 files changed

+537
-0
lines changed

9 files changed

+537
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
* @psalm-require-extends \PHPUnit\Framework\Assert
21+
*
22+
* @experimental
23+
*/
24+
trait JsonPathAssertionsTrait
25+
{
26+
public static function assertJsonPathEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
27+
{
28+
self::assertThat($expectedValue, new JsonPathEquals($jsonPath, $json), $message);
29+
}
30+
31+
public static function assertJsonPathNotEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
32+
{
33+
self::assertThat($expectedValue, new JsonPathNotEquals($jsonPath, $json), $message);
34+
}
35+
36+
public static function assertJsonPathCount(int $expectedCount, JsonPath|string $jsonPath, string $json, string $message = ''): void
37+
{
38+
self::assertThat($expectedCount, new JsonPathCount($jsonPath, $json), $message);
39+
}
40+
41+
public static function assertJsonPathSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
42+
{
43+
self::assertThat($expectedValue, new JsonPathSame($jsonPath, $json), $message);
44+
}
45+
46+
public static function assertJsonPathNotSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
47+
{
48+
self::assertThat($expectedValue, new JsonPathNotSame($jsonPath, $json), $message);
49+
}
50+
51+
public static function assertJsonPathContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, bool $strict = true, string $message = ''): void
52+
{
53+
self::assertThat($expectedValue, new JsonPathContains($jsonPath, $json, $strict), $message);
54+
}
55+
56+
public static function assertJsonPathNotContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, bool $strict = true, string $message = ''): void
57+
{
58+
self::assertThat($expectedValue, new JsonPathNotContains($jsonPath, $json, $strict), $message);
59+
}
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathContains extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
private bool $strict = true,
29+
) {
30+
}
31+
32+
public function toString(): string
33+
{
34+
return \sprintf('is found in elements at JSON path "%s"', $this->jsonPath);
35+
}
36+
37+
protected function matches(mixed $other): bool
38+
{
39+
$result = (new JsonCrawler($this->json))->find($this->jsonPath);
40+
41+
return \in_array($other, $result, $this->strict);
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathCount extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
) {
29+
}
30+
31+
public function toString(): string
32+
{
33+
return \sprintf('matches expected count of JSON path "%s"', $this->jsonPath);
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
return $other === \count((new JsonCrawler($this->json))->find($this->jsonPath));
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathEquals extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
) {
29+
}
30+
31+
public function toString(): string
32+
{
33+
return \sprintf('equals JSON path "%s" result', $this->jsonPath);
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
return (new JsonCrawler($this->json))->find($this->jsonPath) == $other;
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathNotContains extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
private bool $strict = true,
29+
) {
30+
}
31+
32+
public function toString(): string
33+
{
34+
return \sprintf('is not found in elements at JSON path "%s"', $this->jsonPath);
35+
}
36+
37+
protected function matches(mixed $other): bool
38+
{
39+
$result = (new JsonCrawler($this->json))->find($this->jsonPath);
40+
41+
return !\in_array($other, $result, $this->strict);
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathNotEquals extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
) {
29+
}
30+
31+
public function toString(): string
32+
{
33+
return \sprintf('does not equal JSON path "%s" result', $this->jsonPath);
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
return (new JsonCrawler($this->json))->find($this->jsonPath) != $other;
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathNotSame extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
) {
29+
}
30+
31+
public function toString(): string
32+
{
33+
return \sprintf('is not identical to JSON path "%s" result', $this->jsonPath);
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
return (new JsonCrawler($this->json))->find($this->jsonPath) !== $other;
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonPath;
17+
18+
/**
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
class JsonPathSame extends Constraint
24+
{
25+
public function __construct(
26+
private JsonPath|string $jsonPath,
27+
private string $json,
28+
) {
29+
}
30+
31+
public function toString(): string
32+
{
33+
return \sprintf('is identical to JSON path "%s" result', $this->jsonPath);
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
return (new JsonCrawler($this->json))->find($this->jsonPath) === $other;
39+
}
40+
}

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