PHPLIB-462: Add compatibility layer for constraint changes

parent d7aed71c
...@@ -9,6 +9,7 @@ use ArrayObject; ...@@ -9,6 +9,7 @@ use ArrayObject;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use stdClass; use stdClass;
use Symfony\Bridge\PhpUnit\ConstraintTrait;
/** /**
* Constraint that checks if one document matches another. * Constraint that checks if one document matches another.
...@@ -17,6 +18,8 @@ use stdClass; ...@@ -17,6 +18,8 @@ use stdClass;
*/ */
class DocumentsMatchConstraint extends Constraint class DocumentsMatchConstraint extends Constraint
{ {
use ConstraintTrait;
private $ignoreExtraKeysInRoot = false; private $ignoreExtraKeysInRoot = false;
private $ignoreExtraKeysInEmbedded = false; private $ignoreExtraKeysInEmbedded = false;
private $placeholders = []; private $placeholders = [];
...@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint ...@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
*/ */
public function __construct($value, $ignoreExtraKeysInRoot = false, $ignoreExtraKeysInEmbedded = false, array $placeholders = []) public function __construct($value, $ignoreExtraKeysInRoot = false, $ignoreExtraKeysInEmbedded = false, array $placeholders = [])
{ {
parent::__construct();
$this->value = $this->prepareBSON($value, true, $this->sortKeys); $this->value = $this->prepareBSON($value, true, $this->sortKeys);
$this->ignoreExtraKeysInRoot = $ignoreExtraKeysInRoot; $this->ignoreExtraKeysInRoot = $ignoreExtraKeysInRoot;
$this->ignoreExtraKeysInEmbedded = $ignoreExtraKeysInEmbedded; $this->ignoreExtraKeysInEmbedded = $ignoreExtraKeysInEmbedded;
$this->placeholders = $placeholders; $this->placeholders = $placeholders;
} }
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
return 'matches ' . json_encode($this->value);
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other
* @return boolean
*/
protected function matches($other)
{
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
* able to skip preparation, convert both documents to extended JSON,
* and compare strings.
*
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
* compare JSON strings but will still require preparation to sort keys
* in all documents and sub-documents. */
$other = $this->prepareBSON($other, true, $this->sortKeys);
try {
$this->assertEquals($this->value, $other, $this->ignoreExtraKeysInRoot);
} catch (RuntimeException $e) {
return false;
}
return true;
}
/** /**
* Compares two documents recursively. * Compares two documents recursively.
* *
...@@ -131,6 +96,31 @@ class DocumentsMatchConstraint extends Constraint ...@@ -131,6 +96,31 @@ class DocumentsMatchConstraint extends Constraint
} }
} }
private function doMatches($other)
{
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
* able to skip preparation, convert both documents to extended JSON,
* and compare strings.
*
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
* compare JSON strings but will still require preparation to sort keys
* in all documents and sub-documents. */
$other = $this->prepareBSON($other, true, $this->sortKeys);
try {
$this->assertEquals($this->value, $other, $this->ignoreExtraKeysInRoot);
} catch (RuntimeException $e) {
return false;
}
return true;
}
private function doToString()
{
return 'matches ' . json_encode($this->value);
}
/** /**
* Prepare a BSON document or array for comparison. * Prepare a BSON document or array for comparison.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment