]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Framework/ComparisonFailure.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Framework / ComparisonFailure.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Jan Borsodi <jb@ez.no>
40  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
42  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
43
44  * @link       http://www.phpunit.de/
45  * @since      File available since Release 2.0.0
46  */
47
48 require_once 'PHPUnit/Framework.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Util/Type.php';
51
52 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53
54 if (!class_exists('PHPUnit_Framework_ComparisonFailure', FALSE)) {
55
56 /**
57  * Thrown when an assertion for string equality failed.
58  *
59  * @category   Testing
60  * @package    PHPUnit
61  * @author     Jan Borsodi <jb@ez.no>
62  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
63  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
64  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
65  * @version    Release: 3.3.17
66  * @link       http://www.phpunit.de/
67  * @since      Class available since Release 2.0.0
68  */
69 abstract class PHPUnit_Framework_ComparisonFailure extends PHPUnit_Framework_AssertionFailedError
70 {
71     /**
72      * Expected value of the retrieval which does not match $actual.
73      * @var mixed
74      */
75     protected $expected;
76
77     /**
78      * Actually retrieved value which does not match $expected.
79      * @var mixed
80      */
81     protected $actual;
82
83     /**
84      * @var boolean
85      */
86     protected $identical;
87
88     /**
89      * Optional message which is placed in front of the first line
90      * returned by toString().
91      * @var string
92      */
93     protected $message;
94
95     /**
96      * @var boolean
97      */
98     protected static $hasDiff = NULL;
99
100     /**
101      * Initialises with the expected value and the actual value.
102      *
103      * @param mixed $expected Expected value retrieved.
104      * @param mixed $actual Actual value retrieved.
105      * @param boolean $identical
106      * @param string $message A string which is prefixed on all returned lines
107      *                       in the difference output.
108      */
109     public function __construct($expected, $actual, $identical = FALSE, $message = '')
110     {
111         $this->expected  = $expected;
112         $this->actual    = $actual;
113         $this->identical = $identical;
114         $this->message   = $message;
115     }
116
117     public function getActual()
118     {
119         return $this->actual;
120     }
121
122     public function getExpected()
123     {
124         return $this->expected;
125     }
126
127     public function identical()
128     {
129         return $this->identical;
130     }
131
132     /**
133      * Figures out which diff class to use for the input types then
134      * instantiates that class and returns the object.
135      * @note The diff is type sensitive, if the type differs only the types
136      *       are shown.
137      *
138      * @param mixed $expected Expected value retrieved.
139      * @param mixed $actual Actual value retrieved.
140      * @param string $message A string which is prefixed on all returned lines
141      *                       in the difference output.
142      * @return PHPUnit_Framework_ComparisonFailure
143      */
144     public static function diffIdentical($expected, $actual, $message = '')
145     {
146         if (gettype($expected) !== gettype($actual)) {
147             return new PHPUnit_Framework_ComparisonFailure_Type($expected, $actual, TRUE, $message);
148         }
149
150         elseif (is_string($expected)) {
151             return new PHPUnit_Framework_ComparisonFailure_String($expected, $actual, TRUE, $message);
152         }
153
154         elseif (is_null($expected) || is_scalar($expected)) {
155             return new PHPUnit_Framework_ComparisonFailure_Scalar($expected, $actual, TRUE, $message);
156         }
157
158         elseif (is_array($expected)) {
159             return new PHPUnit_Framework_ComparisonFailure_Array($expected, $actual, TRUE, $message);
160         }
161
162         elseif (is_object($expected)) {
163             return new PHPUnit_Framework_ComparisonFailure_Object($expected, $actual, TRUE, $message);
164         }
165     }
166
167     /**
168      * Figures out which diff class to use for the input types then
169      * instantiates that class and returns the object.
170      * @note The diff is not type sensitive, if the type differs the $actual
171      *       value will be converted to the same type as the $expected.
172      *
173      * @param mixed $expected Expected value retrieved.
174      * @param mixed $actual Actual value retrieved.
175      * @param string $message A string which is prefixed on all returned lines
176      *                       in the difference output.
177      * @return PHPUnit_Framework_ComparisonFailure
178      */
179     public static function diffEqual($expected, $actual, $message = '')
180     {
181         if (is_string($expected) && !is_object($actual)) {
182             return new PHPUnit_Framework_ComparisonFailure_String($expected, $actual, FALSE, $message);
183         }
184
185         elseif (is_null($expected) || is_scalar($expected)) {
186             return new PHPUnit_Framework_ComparisonFailure_Scalar($expected, $actual, FALSE, $message);
187         }
188
189         elseif (is_array($expected)) {
190             return new PHPUnit_Framework_ComparisonFailure_Array($expected, $actual, FALSE, $message);
191         }
192
193         elseif (is_object($expected)) {
194             return new PHPUnit_Framework_ComparisonFailure_Object($expected, $actual, FALSE, $message);
195         }
196     }
197
198     protected function diff($expected, $actual)
199     {
200         if (defined('PHPUNIT_TMPDIR')) {
201             $tmpDir = PHPUNIT_TMPDIR;
202         }
203
204         else if (function_exists('sys_get_temp_dir')) {
205             $tmpDir = sys_get_temp_dir();
206         }
207
208         else {
209             $tmpDir = '/tmp';
210         }
211
212         $expectedFile = tempnam($tmpDir, 'expected');
213         $actualFile   = tempnam($tmpDir, 'actual');
214
215         file_put_contents($expectedFile, $expected);
216         file_put_contents($actualFile, $actual);
217
218         $buffer = shell_exec(
219           sprintf(
220             'diff -u %s %s',
221             escapeshellarg($expectedFile),
222             escapeshellarg($actualFile)
223           )
224         );
225
226         unlink($expectedFile);
227         unlink($actualFile);
228
229         if (!empty($buffer)) {
230             $buffer = explode("\n", $buffer);
231
232             $buffer[0] = "--- Expected";
233             $buffer[1] = "+++ Actual";
234
235             $buffer = implode("\n", $buffer);
236         }
237
238         return $buffer;
239     }
240
241     public static function hasDiff()
242     {
243         if (self::$hasDiff === NULL)
244         {
245             self::$hasDiff = FALSE;
246
247             $binary = 'diff';
248
249             if (substr(php_uname('s'), 0, 7) == 'Windows')
250             {
251                 $binary .= '.exe';
252             }
253
254             if (isset($_ENV['PATH'])) {
255                 $var = $_ENV['PATH'];
256             }
257
258             else if (isset($_ENV['Path'])) {
259                 $var = $_ENV['Path'];
260             }
261
262             else if (isset($_SERVER['PATH'])) {
263                 $var = $_SERVER['PATH'];
264             }
265
266             else if (isset($_SERVER['Path'])) {
267                 $var = $_SERVER['Path'];
268             }
269
270             if (isset($var)) {
271                 $paths = explode(PATH_SEPARATOR, $var);
272             } else {
273                 $paths = array();
274             }
275
276             foreach ($paths as $path) {
277                 if (file_exists($path . DIRECTORY_SEPARATOR . $binary) &&
278                     is_executable($path . DIRECTORY_SEPARATOR . $binary))
279                 {
280                     self::$hasDiff = TRUE;
281                     break;
282                 }
283             }
284         }
285
286         return self::$hasDiff;
287     }
288 }
289
290 }
291
292 require_once 'PHPUnit/Framework/ComparisonFailure/Array.php';
293 require_once 'PHPUnit/Framework/ComparisonFailure/Object.php';
294 require_once 'PHPUnit/Framework/ComparisonFailure/Scalar.php';
295 require_once 'PHPUnit/Framework/ComparisonFailure/String.php';
296 require_once 'PHPUnit/Framework/ComparisonFailure/Type.php';
297 ?>