constraint_object_is_equal_with_exceptions.php 4.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
 *
 * @package    core
 * @category   phpunit
 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */


/**
 * Constraint that checks a simple object with an isEqual constrain, allowing for exceptions to be made for some fields.
 *
 * @package    core
 * @category   phpunit
 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
 * @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 {
36
37
38
39
40
41

    /**
     * @var array $keys The list of exceptions.
     */
    protected $keys = array();

42
43
44
45
46
    /**
     * @var mixed $value Need to keep it here because it became private for PHPUnit 7.x and up
     */
    protected $capturedvalue;

47
48
49
50
51
    /**
     * @var \PHPUnit\Framework\Constraint\IsEqual $isequal original constraint to be used internally.
     */
    protected $isequal;

52
53
54
    /**
     * Override constructor to capture value
     */
55
56
    public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false,
                                bool $ignoreCase = false) {
57
        $this->isequal = new \PHPUnit\Framework\Constraint\IsEqual($value, $delta, $maxDepth, $canonicalize, $ignoreCase);
58
59
60
        $this->capturedvalue = $value;
    }

61
62
    /**
     * Add an exception for the named key to use a different comparison
63
     * method. Any assertion provided by PHPUnit\Framework\Assert is
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
     * acceptable.
     *
     * @param string $key The key to except.
     * @param string $comparator The assertion to use.
     */
    public function add_exception($key, $comparator) {
        $this->keys[$key] = $comparator;
    }

    /**
     * Evaluates the constraint for parameter $other
     *
     * If $shouldreturnesult is set to false (the default), an exception is thrown
     * in case of a failure. null is returned otherwise.
     *
     * If $shouldreturnesult is true, the result of the evaluation is returned as
     * a boolean value instead: true in case of success, false in case of a
     * failure.
     *
     * @param  mixed    $other              Value or object to evaluate.
     * @param  string   $description        Additional information about the test
     * @param  bool     $shouldreturnesult  Whether to return a result or throw an exception
     * @return mixed
87
     * @throws PHPUnit\Framework\ExpectationFailedException
88
     */
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);
94
95
96

                // Unset the keys, otherwise the standard evaluation will take place.
                unset($other->$key);
97
                unset($this->capturedvalue->$key);
98
99
100
            }
        }

101
102
103
104
105
106
107
        // Run the IsEqual evaluation.
        return $this->isequal->evaluate($other, $description, $shouldreturnesult);
    }

    // \PHPUnit\Framework\Constraint\IsEqual wrapping.
    public function toString(): string {
        return $this->isequal->toString();
108
109
110
    }

}