Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  /**
  28   * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
  29   *
  30   * @package    core
  31   * @category   phpunit
  32   * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class phpunit_constraint_object_is_equal_with_exceptions extends PHPUnit\Framework\Constraint\IsEqual {
  36  
  37      /**
  38       * @var array $keys The list of exceptions.
  39       */
  40      protected $keys = array();
  41  
  42      /**
  43       * @var mixed $value Need to keep it here because it became private for PHPUnit 7.x and up
  44       */
  45      protected $capturedvalue;
  46  
  47      /**
  48       * Override constructor to capture value
  49       */
  50      public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false,
  51                                  bool $ignoreCase = false) {
  52          parent::__construct($value, $delta, $maxDepth, $canonicalize, $ignoreCase);
  53          $this->capturedvalue = $value;
  54      }
  55  
  56      /**
  57       * Add an exception for the named key to use a different comparison
  58       * method. Any assertion provided by PHPUnit\Framework\Assert is
  59       * acceptable.
  60       *
  61       * @param string $key The key to except.
  62       * @param string $comparator The assertion to use.
  63       */
  64      public function add_exception($key, $comparator) {
  65          $this->keys[$key] = $comparator;
  66      }
  67  
  68      /**
  69       * Evaluates the constraint for parameter $other
  70       *
  71       * If $shouldreturnesult is set to false (the default), an exception is thrown
  72       * in case of a failure. null is returned otherwise.
  73       *
  74       * If $shouldreturnesult is true, the result of the evaluation is returned as
  75       * a boolean value instead: true in case of success, false in case of a
  76       * failure.
  77       *
  78       * @param  mixed    $other              Value or object to evaluate.
  79       * @param  string   $description        Additional information about the test
  80       * @param  bool     $shouldreturnesult  Whether to return a result or throw an exception
  81       * @return mixed
  82       * @throws PHPUnit\Framework\ExpectationFailedException
  83       */
  84      public function evaluate($other, $description = '', $shouldreturnesult = false) {
  85          foreach ($this->keys as $key => $comparison) {
  86              if (isset($other->$key) || isset($this->capturedvalue->$key)) {
  87                  // One of the keys is present, therefore run the comparison.
  88                  PHPUnit\Framework\Assert::$comparison($this->capturedvalue->$key, $other->$key);
  89  
  90                  // Unset the keys, otherwise the standard evaluation will take place.
  91                  unset($other->$key);
  92                  unset($this->capturedvalue->$key);
  93              }
  94          }
  95  
  96          // Run the parent evaluation (isEqual).
  97          return parent::evaluate($other, $description, $shouldreturnesult);
  98      }
  99  
 100  }