2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
22 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
32 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class phpunit_constraint_object_is_equal_with_exceptions
extends PHPUnit\Framework\Constraint\Constraint
{
38 * @var array $keys The list of exceptions.
40 protected $keys = array();
43 * @var mixed $value Need to keep it here because it became private for PHPUnit 7.x and up
45 protected $capturedvalue;
48 * @var \PHPUnit\Framework\Constraint\IsEqual $isequal original constraint to be used internally.
53 * Override constructor to capture value
55 public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false,
56 bool $ignoreCase = false) {
57 $this->isequal
= new \PHPUnit\Framework\Constraint\
IsEqual($value, $delta, $maxDepth, $canonicalize, $ignoreCase);
58 $this->capturedvalue
= $value;
62 * Add an exception for the named key to use a different comparison
63 * method. Any assertion provided by PHPUnit\Framework\Assert is
66 * @param string $key The key to except.
67 * @param string $comparator The assertion to use.
69 public function add_exception($key, $comparator) {
70 $this->keys
[$key] = $comparator;
74 * Evaluates the constraint for parameter $other
76 * If $shouldreturnesult is set to false (the default), an exception is thrown
77 * in case of a failure. null is returned otherwise.
79 * If $shouldreturnesult is true, the result of the evaluation is returned as
80 * a boolean value instead: true in case of success, false in case of a
83 * @param mixed $other Value or object to evaluate.
84 * @param string $description Additional information about the test
85 * @param bool $shouldreturnesult Whether to return a result or throw an exception
87 * @throws PHPUnit\Framework\ExpectationFailedException
89 public function evaluate($other, string $description = '', bool $shouldreturnesult = false): ?
bool {
90 foreach ($this->keys
as $key => $comparison) {
91 if (isset($other->$key) ||
isset($this->capturedvalue
->$key)) {
92 // One of the keys is present, therefore run the comparison.
93 PHPUnit\Framework\Assert
::$comparison($this->capturedvalue
->$key, $other->$key);
95 // Unset the keys, otherwise the standard evaluation will take place.
97 unset($this->capturedvalue
->$key);
101 // Run the IsEqual evaluation.
102 return $this->isequal
->evaluate($other, $description, $shouldreturnesult);
105 // \PHPUnit\Framework\Constraint\IsEqual wrapping.
106 public function toString(): string {
107 return $this->isequal
->toString();