Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / TestFailure.php
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  *
14  * @since Class available since Release 2.0.0
15  */
16 class PHPUnit_Framework_TestFailure
17 {
18     /**
19      * @var string
20      */
21     private $testName;
22
23     /**
24      * @var PHPUnit_Framework_Test|null
25      */
26     protected $failedTest;
27
28     /**
29      * @var Exception
30      */
31     protected $thrownException;
32
33     /**
34      * Constructs a TestFailure with the given test and exception.
35      *
36      * @param PHPUnit_Framework_Test $failedTest
37      * @param Exception              $thrownException
38      */
39     public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
40     {
41         if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
42             $this->testName = $failedTest->toString();
43         } else {
44             $this->testName = get_class($failedTest);
45         }
46         if (!$failedTest instanceof PHPUnit_Framework_TestCase || !$failedTest->isInIsolation()) {
47             $this->failedTest = $failedTest;
48         }
49         $this->thrownException = $thrownException;
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      * @since  Method available since Release 3.4.0
72      */
73     public function getExceptionAsString()
74     {
75         return self::exceptionToString($this->thrownException);
76     }
77
78     /**
79      * Returns a description for an exception.
80      *
81      * @param Exception $e
82      *
83      * @return string
84      *
85      * @since  Method available since Release 3.2.0
86      */
87     public static function exceptionToString(Exception $e)
88     {
89         if ($e instanceof PHPUnit_Framework_SelfDescribing) {
90             $buffer = $e->toString();
91
92             if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
93                 $buffer = $buffer . $e->getComparisonFailure()->getDiff();
94             }
95
96             if (!empty($buffer)) {
97                 $buffer = trim($buffer) . "\n";
98             }
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";
103         } else {
104             $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
105         }
106
107         return $buffer;
108     }
109
110     /**
111      * Returns the name of the failing test (including data set, if any).
112      *
113      * @return string
114      *
115      * @since  Method available since Release 4.3.0
116      */
117     public function getTestName()
118     {
119         return $this->testName;
120     }
121
122     /**
123      * Returns the failing test.
124      *
125      * Note: The test object is not set when the test is executed in process
126      * isolation.
127      *
128      * @see PHPUnit_Framework_Exception
129      *
130      * @return PHPUnit_Framework_Test|null
131      */
132     public function failedTest()
133     {
134         return $this->failedTest;
135     }
136
137     /**
138      * Gets the thrown exception.
139      *
140      * @return Exception
141      */
142     public function thrownException()
143     {
144         return $this->thrownException;
145     }
146
147     /**
148      * Returns the exception's message.
149      *
150      * @return string
151      */
152     public function exceptionMessage()
153     {
154         return $this->thrownException()->getMessage();
155     }
156
157     /**
158      * Returns true if the thrown exception
159      * is of type AssertionFailedError.
160      *
161      * @return bool
162      */
163     public function isFailure()
164     {
165         return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
166     }
167 }