Skip to content

[JsonPath] Add JsonPathAssertionsTrait and related constraints #60105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathAssertionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
trait JsonPathAssertionsTrait
{
/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathEquals($jsonPath, $json), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathNotEquals(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathNotEquals($jsonPath, $json), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathCount(int $expectedCount, JsonPath|string $jsonPath, string $json, string $message = ''): void
{
Assert::assertThat($expectedCount, new JsonPathCount($jsonPath, $json), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathSame($jsonPath, $json), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathNotSame(mixed $expectedValue, JsonPath|string $jsonPath, string $json, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathNotSame($jsonPath, $json), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, bool $strict = true, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathContains($jsonPath, $json, $strict), $message);
}

/**
* @throws ExpectationFailedException
*/
final public static function assertJsonPathNotContains(mixed $expectedValue, JsonPath|string $jsonPath, string $json, bool $strict = true, string $message = ''): void
{
Assert::assertThat($expectedValue, new JsonPathNotContains($jsonPath, $json, $strict), $message);
}
}
43 changes: 43 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathContains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathContains extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
private bool $strict = true,
) {
}

public function toString(): string
{
return \sprintf('is found in elements at JSON path "%s"', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
$result = (new JsonCrawler($this->json))->find($this->jsonPath);

return \in_array($other, $result, $this->strict);
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathCount extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
) {
}

public function toString(): string
{
return \sprintf('matches expected count of JSON path "%s"', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
return $other === \count((new JsonCrawler($this->json))->find($this->jsonPath));
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathEquals extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
) {
}

public function toString(): string
{
return \sprintf('equals JSON path "%s" result', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
return (new JsonCrawler($this->json))->find($this->jsonPath) == $other;
}
}
43 changes: 43 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathNotContains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathNotContains extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
private bool $strict = true,
) {
}

public function toString(): string
{
return \sprintf('is not found in elements at JSON path "%s"', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
$result = (new JsonCrawler($this->json))->find($this->jsonPath);

return !\in_array($other, $result, $this->strict);
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathNotEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathNotEquals extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
) {
}

public function toString(): string
{
return \sprintf('does not equal JSON path "%s" result', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
return (new JsonCrawler($this->json))->find($this->jsonPath) != $other;
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathNotSame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathNotSame extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
) {
}

public function toString(): string
{
return \sprintf('is not identical to JSON path "%s" result', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
return (new JsonCrawler($this->json))->find($this->jsonPath) !== $other;
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/JsonPath/Test/JsonPathSame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Test;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
class JsonPathSame extends Constraint
{
public function __construct(
private JsonPath|string $jsonPath,
private string $json,
) {
}

public function toString(): string
{
return \sprintf('is identical to JSON path "%s" result', $this->jsonPath);
}

protected function matches(mixed $other): bool
{
return (new JsonCrawler($this->json))->find($this->jsonPath) === $other;
}
}
Loading
Loading