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 asserts that one value is identical to another.
14 * Identical check is performed with PHP's === operator, the operator is
15 * explained in detail at
16 * {@url http://www.php.net/manual/en/types.comparisons.php}.
17 * Two values are identical if they have the same value and are of the same
20 * The expected value is passed in the constructor.
22 * @since Class available since Release 3.0.0
24 class PHPUnit_Framework_Constraint_IsIdentical extends PHPUnit_Framework_Constraint
29 const EPSILON = 0.0000000001;
39 public function __construct($value)
41 parent::__construct();
42 $this->value = $value;
46 * Evaluates the constraint for parameter $other
48 * If $returnResult is set to false (the default), an exception is thrown
49 * in case of a failure. null is returned otherwise.
51 * If $returnResult is true, the result of the evaluation is returned as
52 * a boolean value instead: true in case of success, false in case of a
55 * @param mixed $other Value or object to evaluate.
56 * @param string $description Additional information about the test
57 * @param bool $returnResult Whether to return a result or throw an exception
61 * @throws PHPUnit_Framework_ExpectationFailedException
63 public function evaluate($other, $description = '', $returnResult = false)
65 if (is_double($this->value) && is_double($other) &&
66 !is_infinite($this->value) && !is_infinite($other) &&
67 !is_nan($this->value) && !is_nan($other)) {
68 $success = abs($this->value - $other) < self::EPSILON;
70 $success = $this->value === $other;
80 // if both values are strings, make sure a diff is generated
81 if (is_string($this->value) && is_string($other)) {
82 $f = new SebastianBergmann\Comparator\ComparisonFailure(
90 $this->fail($other, $description, $f);
95 * Returns the description of the failure
97 * The beginning of failure messages is "Failed asserting that" in most
98 * cases. This method should return the second part of that sentence.
100 * @param mixed $other Evaluated value or object.
104 protected function failureDescription($other)
106 if (is_object($this->value) && is_object($other)) {
107 return 'two variables reference the same object';
110 if (is_string($this->value) && is_string($other)) {
111 return 'two strings are identical';
114 return parent::failureDescription($other);
118 * Returns a string representation of the constraint.
122 public function toString()
124 if (is_object($this->value)) {
125 return 'is identical to an object of class "' .
126 get_class($this->value) . '"';
128 return 'is identical to ' .
129 $this->exporter->export($this->value);