MDL-65204 phpunit: tweak constraint_object_is_equal_with_exceptions

In PHPUnit 7.x and above, IsEqual->value became private and, as far
as our with exceptions class inherits from it, we cannot access to
that anymore.

So, in order to avoid that, we are overriding the constructor, capturing
the original value for own use and forgetting.

A more formal, alternative, solution would be to make our
exceptional class to inherit from Constraint and make the
class a pure dispatcher to different constraints, with IsEqual being
just one of them.

But we followed the easiest path here. Not ideal, but efective.
This commit is contained in:
Eloy Lafuente (stronk7)
2019-04-03 10:39:19 +08:00
committed by Jun Pataleta
parent 85f47bae7f
commit 032c75ec43
@@ -39,6 +39,19 @@ class phpunit_constraint_object_is_equal_with_exceptions extends PHPUnit\Framewo
*/
protected $keys = array();
/**
* @var mixed $value Need to keep it here because it became private for PHPUnit 7.x and up
*/
protected $capturedvalue;
/**
* Override constructor to capture value
*/
public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false) {
parent::__construct($value, $delta, $maxDepth, $canonicalize, $ignoreCase);
$this->capturedvalue = $value;
}
/**
* Add an exception for the named key to use a different comparison
* method. Any assertion provided by PHPUnit\Framework\Assert is
@@ -69,13 +82,13 @@ class phpunit_constraint_object_is_equal_with_exceptions extends PHPUnit\Framewo
*/
public function evaluate($other, $description = '', $shouldreturnesult = false) {
foreach ($this->keys as $key => $comparison) {
if (isset($other->$key) || isset($this->value->$key)) {
if (isset($other->$key) || isset($this->capturedvalue->$key)) {
// One of the keys is present, therefore run the comparison.
PHPUnit\Framework\Assert::$comparison($this->value->$key, $other->$key);
PHPUnit\Framework\Assert::$comparison($this->capturedvalue->$key, $other->$key);
// Unset the keys, otherwise the standard evaluation will take place.
unset($other->$key);
unset($this->value->$key);
unset($this->capturedvalue->$key);
}
}