1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * A TestFailure collects a failed test together with the caught exception.
13 */
14class PHPUnit_Framework_TestFailure
15{
16    /**
17     * @var string
18     */
19    private $testName;
20
21    /**
22     * @var PHPUnit_Framework_Test|null
23     */
24    protected $failedTest;
25
26    /**
27     * @var Exception
28     */
29    protected $thrownException;
30
31    /**
32     * Constructs a TestFailure with the given test and exception.
33     *
34     * @param PHPUnit_Framework_Test $failedTest
35     * @param Throwable              $t
36     */
37    public function __construct(PHPUnit_Framework_Test $failedTest, $t)
38    {
39        if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
40            $this->testName = $failedTest->toString();
41        } else {
42            $this->testName = get_class($failedTest);
43        }
44
45        if (!$failedTest instanceof PHPUnit_Framework_TestCase || !$failedTest->isInIsolation()) {
46            $this->failedTest = $failedTest;
47        }
48
49        $this->thrownException = $t;
50    }
51
52    /**
53     * Returns a short description of the failure.
54     *
55     * @return string
56     */
57    public function toString()
58    {
59        return sprintf(
60            '%s: %s',
61            $this->testName,
62            $this->thrownException->getMessage()
63        );
64    }
65
66    /**
67     * Returns a description for the thrown exception.
68     *
69     * @return string
70     */
71    public function getExceptionAsString()
72    {
73        return self::exceptionToString($this->thrownException);
74    }
75
76    /**
77     * Returns a description for an exception.
78     *
79     * @param Exception $e
80     *
81     * @return string
82     */
83    public static function exceptionToString(Exception $e)
84    {
85        if ($e instanceof PHPUnit_Framework_SelfDescribing) {
86            $buffer = $e->toString();
87
88            if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
89                $buffer = $buffer . $e->getComparisonFailure()->getDiff();
90            }
91
92            if (!empty($buffer)) {
93                $buffer = trim($buffer) . "\n";
94            }
95        } elseif ($e instanceof PHPUnit_Framework_Error) {
96            $buffer = $e->getMessage() . "\n";
97        } elseif ($e instanceof PHPUnit_Framework_ExceptionWrapper) {
98            $buffer = $e->getClassName() . ': ' . $e->getMessage() . "\n";
99        } else {
100            $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
101        }
102
103        return $buffer;
104    }
105
106    /**
107     * Returns the name of the failing test (including data set, if any).
108     *
109     * @return string
110     */
111    public function getTestName()
112    {
113        return $this->testName;
114    }
115
116    /**
117     * Returns the failing test.
118     *
119     * Note: The test object is not set when the test is executed in process
120     * isolation.
121     *
122     * @see PHPUnit_Framework_Exception
123     *
124     * @return PHPUnit_Framework_Test|null
125     */
126    public function failedTest()
127    {
128        return $this->failedTest;
129    }
130
131    /**
132     * Gets the thrown exception.
133     *
134     * @return Exception
135     */
136    public function thrownException()
137    {
138        return $this->thrownException;
139    }
140
141    /**
142     * Returns the exception's message.
143     *
144     * @return string
145     */
146    public function exceptionMessage()
147    {
148        return $this->thrownException()->getMessage();
149    }
150
151    /**
152     * Returns true if the thrown exception
153     * is of type AssertionFailedError.
154     *
155     * @return bool
156     */
157    public function isFailure()
158    {
159        return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
160    }
161}
162