3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Constraint that checks if one value is equal to another.
14 * Equality is checked with PHP's == operator, the operator is explained in
15 * detail at {@url http://www.php.net/manual/en/types.comparisons.php}.
16 * Two values are equal if they have the same value disregarding type.
18 * The expected value is passed in the constructor.
20 * @since Class available since Release 3.0.0
22 class PHPUnit_Framework_Constraint_IsEqual extends PHPUnit_Framework_Constraint
32 protected $delta = 0.0;
37 protected $maxDepth = 10;
42 protected $canonicalize = false;
47 protected $ignoreCase = false;
50 * @var SebastianBergmann\Comparator\ComparisonFailure
52 protected $lastFailure;
57 * @param int $maxDepth
58 * @param bool $canonicalize
59 * @param bool $ignoreCase
61 * @throws PHPUnit_Framework_Exception
63 public function __construct($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
65 parent::__construct();
67 if (!is_numeric($delta)) {
68 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'numeric');
71 if (!is_int($maxDepth)) {
72 throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'integer');
75 if (!is_bool($canonicalize)) {
76 throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'boolean');
79 if (!is_bool($ignoreCase)) {
80 throw PHPUnit_Util_InvalidArgumentHelper::factory(5, 'boolean');
83 $this->value = $value;
84 $this->delta = $delta;
85 $this->maxDepth = $maxDepth;
86 $this->canonicalize = $canonicalize;
87 $this->ignoreCase = $ignoreCase;
91 * Evaluates the constraint for parameter $other
93 * If $returnResult is set to false (the default), an exception is thrown
94 * in case of a failure. null is returned otherwise.
96 * If $returnResult is true, the result of the evaluation is returned as
97 * a boolean value instead: true in case of success, false in case of a
100 * @param mixed $other Value or object to evaluate.
101 * @param string $description Additional information about the test
102 * @param bool $returnResult Whether to return a result or throw an exception
106 * @throws PHPUnit_Framework_ExpectationFailedException
108 public function evaluate($other, $description = '', $returnResult = false)
110 // If $this->value and $other are identical, they are also equal.
111 // This is the most common path and will allow us to skip
112 // initialization of all the comparators.
113 if ($this->value === $other) {
117 $comparatorFactory = SebastianBergmann\Comparator\Factory::getInstance();
120 $comparator = $comparatorFactory->getComparatorFor(
125 $comparator->assertEquals(
132 } catch (SebastianBergmann\Comparator\ComparisonFailure $f) {
137 throw new PHPUnit_Framework_ExpectationFailedException(
138 trim($description . "\n" . $f->getMessage()),
147 * Returns a string representation of the constraint.
151 public function toString()
155 if (is_string($this->value)) {
156 if (strpos($this->value, "\n") !== false) {
157 return 'is equal to <text>';
160 'is equal to <string:%s>',
165 if ($this->delta != 0) {
174 $this->exporter->export($this->value),