Skip to content

Commit

Permalink
add deep matching
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrelka committed Aug 3, 2021
1 parent 8fb8564 commit 02404a1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/Constraint/JsonValueMatchesManyDeep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
namespace Helmich\JsonAssert\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;

/**
* Constraint that asserts that a JSON document matches an entire set of JSON
* value constraints.
*
* @package Helmich\JsonAssert
* @subpackage Constraint
*/
class JsonValueMatchesManyDeep extends Constraint
{

/** @var JsonValueMatches[] */
private $constraints = array();

/**
* JsonValueMatchesMany constructor.
*
* @param array $constraints A set of constraints. This is a key-value map
* where each key is a JSON path expression,
* associated with a constraint that all values
* matched by that expression must fulfill.
*/
public function __construct(array $constraints)
{
foreach ($constraints as $key => $constraint) {
if (is_array($constraint)) {
$constraint = new JsonValueMatchesManyDeep($constraint);
} elseif (!$constraint instanceof Constraint) {
$constraint = new IsEqual($constraint);
}

$this->constraints[] = new JsonValueMatches($key, $constraint);
}
}

/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString(): string
{
return implode(
' and ',
array_map(
function (Constraint $constraint) {
return $constraint->toString();
},
$this->constraints
)
);
}

/**
* @inheritdoc
*/
protected function matches($other): bool
{
foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, '', true)) {
return false;
}
}
return true;
}

/**
* Returns a string representation of matches that evaluate to false.
*
* @return string
*/
protected function additionalFailureDescription($other): string
{
/** @var string[] */
$failedConstraints = array();

foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, '', true)) {
$failedConstraints[] = $constraint->toString();
}
}

return "\n" . implode("\n", $failedConstraints);
}
}
17 changes: 17 additions & 0 deletions src/JsonAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Helmich\JsonAssert\Constraint\JsonValueMatches;
use Helmich\JsonAssert\Constraint\JsonValueMatchesMany;
use Helmich\JsonAssert\Constraint\JsonValueMatchesManyDeep;
use Helmich\JsonAssert\Constraint\JsonValueMatchesSchema;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
Expand Down Expand Up @@ -111,6 +112,22 @@ public static function assertJsonDocumentMatches($jsonDocument, array $constrain
Assert::assertThat($jsonDocument, new JsonValueMatchesMany($constraints));
}

/**
* Asserts that a JSON document matches an entire set of constraints, recursively expanding arrays.
*
* @param mixed $jsonDocument A JSON document. If this is a string, it will
* be assumed to be an encoded JSON document
* @param array $constraints A set of constraints. This is a key-value map
* where each key is a JSON path expression,
* associated with a constraint that all values
* matched by that expression must fulfill.
* @return void
*/
public static function assertJsonDocumentMatchesDeep($jsonDocument, array $constraints)
{
Assert::assertThat($jsonDocument, new JsonValueMatchesManyDeep($constraints));
}

/**
* Assert that a JSON document matches a given JSON schema.
*
Expand Down

0 comments on commit 02404a1

Please sign in to comment.