[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpunit/classes/ -> constraint_object_is_equal_with_exceptions.php (source)

   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       * Add an exception for the named key to use a different comparison
  44       * method. Any assertion provided by PHPUnit_Framework_Assert is
  45       * acceptable.
  46       *
  47       * @param string $key The key to except.
  48       * @param string $comparator The assertion to use.
  49       */
  50      public function add_exception($key, $comparator) {
  51          $this->keys[$key] = $comparator;
  52      }
  53  
  54      /**
  55       * Evaluates the constraint for parameter $other
  56       *
  57       * If $shouldreturnesult is set to false (the default), an exception is thrown
  58       * in case of a failure. null is returned otherwise.
  59       *
  60       * If $shouldreturnesult is true, the result of the evaluation is returned as
  61       * a boolean value instead: true in case of success, false in case of a
  62       * failure.
  63       *
  64       * @param  mixed    $other              Value or object to evaluate.
  65       * @param  string   $description        Additional information about the test
  66       * @param  bool     $shouldreturnesult  Whether to return a result or throw an exception
  67       * @return mixed
  68       * @throws PHPUnit_Framework_ExpectationFailedException
  69       */
  70      public function evaluate($other, $description = '', $shouldreturnesult = false) {
  71          foreach ($this->keys as $key => $comparison) {
  72              if (isset($other->$key) || isset($this->value->$key)) {
  73                  // One of the keys is present, therefore run the comparison.
  74                  PHPUnit_Framework_Assert::$comparison($this->value->$key, $other->$key);
  75  
  76                  // Unset the keys, otherwise the standard evaluation will take place.
  77                  unset($other->$key);
  78                  unset($this->value->$key);
  79              }
  80          }
  81  
  82          // Run the parent evaluation (isEqual).
  83          return parent::evaluate($other, $description, $shouldreturnesult);
  84      }
  85  
  86  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1