Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / IsEqual.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 checks if one value is equal to another.
13  *
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.
17  *
18  * The expected value is passed in the constructor.
19  *
20  * @since Class available since Release 3.0.0
21  */
22 class PHPUnit_Framework_Constraint_IsEqual extends PHPUnit_Framework_Constraint
23 {
24     /**
25      * @var mixed
26      */
27     protected $value;
28
29     /**
30      * @var float
31      */
32     protected $delta = 0.0;
33
34     /**
35      * @var int
36      */
37     protected $maxDepth = 10;
38
39     /**
40      * @var bool
41      */
42     protected $canonicalize = false;
43
44     /**
45      * @var bool
46      */
47     protected $ignoreCase = false;
48
49     /**
50      * @var SebastianBergmann\Comparator\ComparisonFailure
51      */
52     protected $lastFailure;
53
54     /**
55      * @param mixed $value
56      * @param float $delta
57      * @param int   $maxDepth
58      * @param bool  $canonicalize
59      * @param bool  $ignoreCase
60      *
61      * @throws PHPUnit_Framework_Exception
62      */
63     public function __construct($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
64     {
65         parent::__construct();
66
67         if (!is_numeric($delta)) {
68             throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'numeric');
69         }
70
71         if (!is_int($maxDepth)) {
72             throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'integer');
73         }
74
75         if (!is_bool($canonicalize)) {
76             throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'boolean');
77         }
78
79         if (!is_bool($ignoreCase)) {
80             throw PHPUnit_Util_InvalidArgumentHelper::factory(5, 'boolean');
81         }
82
83         $this->value        = $value;
84         $this->delta        = $delta;
85         $this->maxDepth     = $maxDepth;
86         $this->canonicalize = $canonicalize;
87         $this->ignoreCase   = $ignoreCase;
88     }
89
90     /**
91      * Evaluates the constraint for parameter $other
92      *
93      * If $returnResult is set to false (the default), an exception is thrown
94      * in case of a failure. null is returned otherwise.
95      *
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
98      * failure.
99      *
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
103      *
104      * @return mixed
105      *
106      * @throws PHPUnit_Framework_ExpectationFailedException
107      */
108     public function evaluate($other, $description = '', $returnResult = false)
109     {
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) {
114             return true;
115         }
116
117         $comparatorFactory = SebastianBergmann\Comparator\Factory::getInstance();
118
119         try {
120             $comparator = $comparatorFactory->getComparatorFor(
121                 $this->value,
122                 $other
123             );
124
125             $comparator->assertEquals(
126                 $this->value,
127                 $other,
128                 $this->delta,
129                 $this->canonicalize,
130                 $this->ignoreCase
131             );
132         } catch (SebastianBergmann\Comparator\ComparisonFailure $f) {
133             if ($returnResult) {
134                 return false;
135             }
136
137             throw new PHPUnit_Framework_ExpectationFailedException(
138                 trim($description . "\n" . $f->getMessage()),
139                 $f
140             );
141         }
142
143         return true;
144     }
145
146     /**
147      * Returns a string representation of the constraint.
148      *
149      * @return string
150      */
151     public function toString()
152     {
153         $delta = '';
154
155         if (is_string($this->value)) {
156             if (strpos($this->value, "\n") !== false) {
157                 return 'is equal to <text>';
158             } else {
159                 return sprintf(
160                     'is equal to <string:%s>',
161                     $this->value
162                 );
163             }
164         } else {
165             if ($this->delta != 0) {
166                 $delta = sprintf(
167                     ' with delta <%F>',
168                     $this->delta
169                 );
170             }
171
172             return sprintf(
173                 'is equal to %s%s',
174                 $this->exporter->export($this->value),
175                 $delta
176             );
177         }
178     }
179 }