5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
44 * @link http://www.phpunit.de/
45 * @since File available since Release 2.0.0
48 require_once 'PHPUnit/Framework.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Util/Type.php';
52 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54 if (!class_exists('PHPUnit_Framework_ComparisonFailure', FALSE)) {
57 * Thrown when an assertion for string equality failed.
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
69 abstract class PHPUnit_Framework_ComparisonFailure extends PHPUnit_Framework_AssertionFailedError
72 * Expected value of the retrieval which does not match $actual.
78 * Actually retrieved value which does not match $expected.
89 * Optional message which is placed in front of the first line
90 * returned by toString().
98 protected static $hasDiff = NULL;
101 * Initialises with the expected value and the actual value.
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.
109 public function __construct($expected, $actual, $identical = FALSE, $message = '')
111 $this->expected = $expected;
112 $this->actual = $actual;
113 $this->identical = $identical;
114 $this->message = $message;
117 public function getActual()
119 return $this->actual;
122 public function getExpected()
124 return $this->expected;
127 public function identical()
129 return $this->identical;
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
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
144 public static function diffIdentical($expected, $actual, $message = '')
146 if (gettype($expected) !== gettype($actual)) {
147 return new PHPUnit_Framework_ComparisonFailure_Type($expected, $actual, TRUE, $message);
150 elseif (is_string($expected)) {
151 return new PHPUnit_Framework_ComparisonFailure_String($expected, $actual, TRUE, $message);
154 elseif (is_null($expected) || is_scalar($expected)) {
155 return new PHPUnit_Framework_ComparisonFailure_Scalar($expected, $actual, TRUE, $message);
158 elseif (is_array($expected)) {
159 return new PHPUnit_Framework_ComparisonFailure_Array($expected, $actual, TRUE, $message);
162 elseif (is_object($expected)) {
163 return new PHPUnit_Framework_ComparisonFailure_Object($expected, $actual, TRUE, $message);
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.
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
179 public static function diffEqual($expected, $actual, $message = '')
181 if (is_string($expected) && !is_object($actual)) {
182 return new PHPUnit_Framework_ComparisonFailure_String($expected, $actual, FALSE, $message);
185 elseif (is_null($expected) || is_scalar($expected)) {
186 return new PHPUnit_Framework_ComparisonFailure_Scalar($expected, $actual, FALSE, $message);
189 elseif (is_array($expected)) {
190 return new PHPUnit_Framework_ComparisonFailure_Array($expected, $actual, FALSE, $message);
193 elseif (is_object($expected)) {
194 return new PHPUnit_Framework_ComparisonFailure_Object($expected, $actual, FALSE, $message);
198 protected function diff($expected, $actual)
200 if (defined('PHPUNIT_TMPDIR')) {
201 $tmpDir = PHPUNIT_TMPDIR;
204 else if (function_exists('sys_get_temp_dir')) {
205 $tmpDir = sys_get_temp_dir();
212 $expectedFile = tempnam($tmpDir, 'expected');
213 $actualFile = tempnam($tmpDir, 'actual');
215 file_put_contents($expectedFile, $expected);
216 file_put_contents($actualFile, $actual);
218 $buffer = shell_exec(
221 escapeshellarg($expectedFile),
222 escapeshellarg($actualFile)
226 unlink($expectedFile);
229 if (!empty($buffer)) {
230 $buffer = explode("\n", $buffer);
232 $buffer[0] = "--- Expected";
233 $buffer[1] = "+++ Actual";
235 $buffer = implode("\n", $buffer);
241 public static function hasDiff()
243 if (self::$hasDiff === NULL)
245 self::$hasDiff = FALSE;
249 if (substr(php_uname('s'), 0, 7) == 'Windows')
254 if (isset($_ENV['PATH'])) {
255 $var = $_ENV['PATH'];
258 else if (isset($_ENV['Path'])) {
259 $var = $_ENV['Path'];
262 else if (isset($_SERVER['PATH'])) {
263 $var = $_SERVER['PATH'];
266 else if (isset($_SERVER['Path'])) {
267 $var = $_SERVER['Path'];
271 $paths = explode(PATH_SEPARATOR, $var);
276 foreach ($paths as $path) {
277 if (file_exists($path . DIRECTORY_SEPARATOR . $binary) &&
278 is_executable($path . DIRECTORY_SEPARATOR . $binary))
280 self::$hasDiff = TRUE;
286 return self::$hasDiff;
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';