MDL-65204 phpunit: tweak constraint_object_is_equal_with_exceptions
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 28 Mar 2019 00:37:28 +0000 (01:37 +0100)
committerJun Pataleta <jun@moodle.com>
Wed, 3 Apr 2019 02:39:19 +0000 (10:39 +0800)
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.

lib/phpunit/classes/constraint_object_is_equal_with_exceptions.php

index 9407b7a..44dda06 100644 (file)
@@ -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);
             }
         }