5
* Copyright (c) 2002-2010, 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 Sebastian Bergmann <sb@sebastian-bergmann.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 2.0.0
46
require_once 'PHPUnit/Framework.php';
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
* A TestFailure collects a failed test together with the caught exception.
55
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
56
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
57
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
58
* @version Release: 3.4.14
59
* @link http://www.phpunit.de/
60
* @since Class available since Release 2.0.0
62
class PHPUnit_Framework_TestFailure
65
* @var PHPUnit_Framework_Test
67
protected $failedTest;
72
protected $thrownException;
75
* Constructs a TestFailure with the given test and exception.
77
* @param PHPUnit_Framework_Test $failedTest
78
* @param Exception $thrownException
80
public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
82
$this->failedTest = $failedTest;
83
$this->thrownException = $thrownException;
87
* Returns a short description of the failure.
91
public function toString()
97
$this->thrownException->getMessage()
102
* Returns a description for the thrown exception.
105
* @since Method available since Release 3.4.0
107
public function getExceptionAsString()
109
return self::exceptionToString($this->thrownException);
113
* Returns a description for an exception.
115
* @param Exception $e
117
* @since Method available since Release 3.2.0
119
public static function exceptionToString(Exception $e)
121
if ($e instanceof PHPUnit_Framework_SelfDescribing) {
122
if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
123
$comparisonFailure = $e->getComparisonFailure();
124
$description = $e->getDescription();
125
$message = $e->getCustomMessage();
127
if ($message == '') {
130
$buffer = $message . "\n";
133
if ($comparisonFailure !== NULL) {
134
if ($comparisonFailure->identical()) {
135
if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object) {
136
$buffer .= 'Failed asserting that two variables ' .
137
"reference the same object.\n";
139
$buffer .= $comparisonFailure->toString() . "\n";
142
if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Scalar) {
144
"Failed asserting that %s matches expected %s.\n",
146
PHPUnit_Util_Type::toString(
147
$comparisonFailure->getActual()
149
PHPUnit_Util_Type::toString(
150
$comparisonFailure->getExpected()
155
else if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array ||
156
$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object ||
157
$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
159
"Failed asserting that two %ss are equal.\n%s\n",
162
substr(get_class($comparisonFailure), 36)
164
$comparisonFailure->toString()
169
$buffer .= $e->toString();
171
if (!empty($buffer)) {
175
if (strpos($buffer, $description) === FALSE) {
176
$buffer .= $description . "\n";
182
$buffer = $e->toString();
184
if (!empty($buffer)) {
190
else if ($e instanceof PHPUnit_Framework_Error) {
191
$buffer = $e->getMessage() . "\n";
195
$buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
202
* Gets the failed test.
206
public function failedTest()
208
return $this->failedTest;
212
* Gets the thrown exception.
216
public function thrownException()
218
return $this->thrownException;
222
* Returns the exception's message.
226
public function exceptionMessage()
228
return $this->thrownException()->getMessage();
232
* Returns TRUE if the thrown exception
233
* is of type AssertionFailedError.
237
public function isFailure()
239
return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
b'\\ No newline at end of file'