3
* This file is part of PHPUnit.
5
* (c) Sebastian Bergmann <sebastian@phpunit.de>
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
12
* A TestFailure collects a failed test together with the caught exception.
15
* @subpackage Framework
16
* @author Sebastian Bergmann <sebastian@phpunit.de>
17
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
18
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
19
* @link http://www.phpunit.de/
20
* @since Class available since Release 2.0.0
22
class PHPUnit_Framework_TestFailure
30
* @var PHPUnit_Framework_Test|null
32
protected $failedTest;
37
protected $thrownException;
40
* Constructs a TestFailure with the given test and exception.
42
* @param PHPUnit_Framework_Test $failedTest
43
* @param Exception $thrownException
45
public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
47
if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
48
$this->testName = $failedTest->toString();
50
$this->testName = get_class($failedTest);
52
if (!$failedTest instanceof PHPUnit_Framework_TestCase || !$failedTest->isInIsolation()) {
53
$this->failedTest = $failedTest;
55
$this->thrownException = $thrownException;
59
* Returns a short description of the failure.
63
public function toString()
68
$this->thrownException->getMessage()
73
* Returns a description for the thrown exception.
76
* @since Method available since Release 3.4.0
78
public function getExceptionAsString()
80
return self::exceptionToString($this->thrownException);
84
* Returns a description for an exception.
88
* @since Method available since Release 3.2.0
90
public static function exceptionToString(Exception $e)
92
if ($e instanceof PHPUnit_Framework_SelfDescribing) {
93
$buffer = $e->toString();
95
if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
96
$buffer = $buffer . $e->getComparisonFailure()->getDiff();
99
if (!empty($buffer)) {
100
$buffer = trim($buffer) . "\n";
102
} elseif ($e instanceof PHPUnit_Framework_Error) {
103
$buffer = $e->getMessage() . "\n";
104
} elseif ($e instanceof PHPUnit_Framework_ExceptionWrapper) {
105
$buffer = $e->getClassname() . ': ' . $e->getMessage() . "\n";
107
$buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
114
* Returns the name of the failing test (including data set, if any).
117
* @since Method available since Release 4.3.0
119
public function getTestName()
121
return $this->testName;
125
* Returns the failing test.
127
* Note: The test object is not set when the test is executed in process
130
* @see PHPUnit_Framework_Exception
132
* @return PHPUnit_Framework_Test|null
134
public function failedTest()
136
return $this->failedTest;
140
* Gets the thrown exception.
144
public function thrownException()
146
return $this->thrownException;
150
* Returns the exception's message.
154
public function exceptionMessage()
156
return $this->thrownException()->getMessage();
160
* Returns true if the thrown exception
161
* is of type AssertionFailedError.
165
public function isFailure()
167
return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);