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.
14 * @since Class available since Release 2.0.0
16 class PHPUnit_Framework_TestFailure
24 * @var PHPUnit_Framework_Test|null
26 protected $failedTest;
31 protected $thrownException;
34 * Constructs a TestFailure with the given test and exception.
36 * @param PHPUnit_Framework_Test $failedTest
37 * @param Exception $thrownException
39 public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
41 if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
42 $this->testName = $failedTest->toString();
44 $this->testName = get_class($failedTest);
46 if (!$failedTest instanceof PHPUnit_Framework_TestCase || !$failedTest->isInIsolation()) {
47 $this->failedTest = $failedTest;
49 $this->thrownException = $thrownException;
53 * Returns a short description of the failure.
57 public function toString()
62 $this->thrownException->getMessage()
67 * Returns a description for the thrown exception.
71 * @since Method available since Release 3.4.0
73 public function getExceptionAsString()
75 return self::exceptionToString($this->thrownException);
79 * Returns a description for an exception.
85 * @since Method available since Release 3.2.0
87 public static function exceptionToString(Exception $e)
89 if ($e instanceof PHPUnit_Framework_SelfDescribing) {
90 $buffer = $e->toString();
92 if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
93 $buffer = $buffer . $e->getComparisonFailure()->getDiff();
96 if (!empty($buffer)) {
97 $buffer = trim($buffer) . "\n";
99 } elseif ($e instanceof PHPUnit_Framework_Error) {
100 $buffer = $e->getMessage() . "\n";
101 } elseif ($e instanceof PHPUnit_Framework_ExceptionWrapper) {
102 $buffer = $e->getClassname() . ': ' . $e->getMessage() . "\n";
104 $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
111 * Returns the name of the failing test (including data set, if any).
115 * @since Method available since Release 4.3.0
117 public function getTestName()
119 return $this->testName;
123 * Returns the failing test.
125 * Note: The test object is not set when the test is executed in process
128 * @see PHPUnit_Framework_Exception
130 * @return PHPUnit_Framework_Test|null
132 public function failedTest()
134 return $this->failedTest;
138 * Gets the thrown exception.
142 public function thrownException()
144 return $this->thrownException;
148 * Returns the exception's message.
152 public function exceptionMessage()
154 return $this->thrownException()->getMessage();
158 * Returns true if the thrown exception
159 * is of type AssertionFailedError.
163 public function isFailure()
165 return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);