Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / IsIdentical.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Constraint that asserts that one value is identical to another.
13  *
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
18  * type.
19  *
20  * The expected value is passed in the constructor.
21  *
22  * @since Class available since Release 3.0.0
23  */
24 class PHPUnit_Framework_Constraint_IsIdentical extends PHPUnit_Framework_Constraint
25 {
26     /**
27      * @var float
28      */
29     const EPSILON = 0.0000000001;
30
31     /**
32      * @var mixed
33      */
34     protected $value;
35
36     /**
37      * @param mixed $value
38      */
39     public function __construct($value)
40     {
41         parent::__construct();
42         $this->value = $value;
43     }
44
45     /**
46      * Evaluates the constraint for parameter $other
47      *
48      * If $returnResult is set to false (the default), an exception is thrown
49      * in case of a failure. null is returned otherwise.
50      *
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
53      * failure.
54      *
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
58      *
59      * @return mixed
60      *
61      * @throws PHPUnit_Framework_ExpectationFailedException
62      */
63     public function evaluate($other, $description = '', $returnResult = false)
64     {
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;
69         } else {
70             $success = $this->value === $other;
71         }
72
73         if ($returnResult) {
74             return $success;
75         }
76
77         if (!$success) {
78             $f = null;
79
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(
83                     $this->value,
84                     $other,
85                     $this->value,
86                     $other
87                 );
88             }
89
90             $this->fail($other, $description, $f);
91         }
92     }
93
94     /**
95      * Returns the description of the failure
96      *
97      * The beginning of failure messages is "Failed asserting that" in most
98      * cases. This method should return the second part of that sentence.
99      *
100      * @param mixed $other Evaluated value or object.
101      *
102      * @return string
103      */
104     protected function failureDescription($other)
105     {
106         if (is_object($this->value) && is_object($other)) {
107             return 'two variables reference the same object';
108         }
109
110         if (is_string($this->value) && is_string($other)) {
111             return 'two strings are identical';
112         }
113
114         return parent::failureDescription($other);
115     }
116
117     /**
118      * Returns a string representation of the constraint.
119      *
120      * @return string
121      */
122     public function toString()
123     {
124         if (is_object($this->value)) {
125             return 'is identical to an object of class "' .
126                    get_class($this->value) . '"';
127         } else {
128             return 'is identical to ' .
129                    $this->exporter->export($this->value);
130         }
131     }
132 }