]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Framework/TestFailure.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Framework / TestFailure.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.0.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
52 if (!class_exists('PHPUnit_Framework_TestFailure', FALSE)) {
53
54 /**
55  * A TestFailure collects a failed test together with the caught exception.
56  *
57  * @category   Testing
58  * @package    PHPUnit
59  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
61  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
62  * @version    Release: 3.3.17
63  * @link       http://www.phpunit.de/
64  * @since      Class available since Release 2.0.0
65  */
66 class PHPUnit_Framework_TestFailure
67 {
68     /**
69      * @var    PHPUnit_Framework_Test
70      */
71     protected $failedTest;
72
73     /**
74      * @var    Exception
75      */
76     protected $thrownException;
77
78     /**
79      * Constructs a TestFailure with the given test and exception.
80      *
81      * @param  PHPUnit_Framework_Test $failedTest
82      * @param  Exception               $thrownException
83      */
84     public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
85     {
86         $this->failedTest      = $failedTest;
87         $this->thrownException = $thrownException;
88     }
89
90     /**
91      * Returns a short description of the failure.
92      *
93      * @return string
94      */
95     public function toString()
96     {
97         return sprintf(
98           '%s: %s',
99
100           $this->failedTest,
101           $this->thrownException->getMessage()
102         );
103     }
104
105     /**
106      * Returns a verbose description of the failure.
107      *
108      * @param  bool $verbose
109      * @return string
110      * @since  Method available since Release 3.2.0
111      */
112     public function toStringVerbose($verbose = FALSE)
113     {
114         return self::exceptionToString($this->thrownException, $verbose);
115     }
116
117     /**
118      * Returns a verbose description for an exception.
119      *
120      * @param  Exception $e
121      * @param  bool      $verbose
122      * @return string
123      * @since  Method available since Release 3.2.0
124      */
125     public static function exceptionToString(Exception $e, $verbose = FALSE)
126     {
127         if ($e instanceof PHPUnit_Framework_SelfDescribing) {
128             if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
129                 $comparisonFailure = $e->getComparisonFailure();
130                 $description       = $e->getDescription();
131                 $message           = $e->getCustomMessage();
132
133                 if ($message == '') {
134                     $buffer = '';
135                 } else {
136                     $buffer = $message . "\n";
137                 }
138
139                 if ($comparisonFailure !== NULL) {
140                     if ($comparisonFailure->identical()) {
141                         if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object) {
142                             $buffer .= "Failed asserting that two variables reference the same object.\n";
143                         } else {
144                             $buffer .= $comparisonFailure->toString() . "\n";
145                         }
146                     } else {
147                         if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Scalar) {
148                             $buffer .= sprintf(
149                               "Failed asserting that %s matches expected value %s.\n",
150
151                               PHPUnit_Util_Type::toString($comparisonFailure->getActual()),
152                               PHPUnit_Util_Type::toString($comparisonFailure->getExpected())
153                             );
154                         }
155
156                         else if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array ||
157                                  $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object ||
158                                  $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
159                             $buffer .= sprintf(
160                               "Failed asserting that two %ss are equal.\n%s\n",
161
162                               strtolower(substr(get_class($comparisonFailure), 36)),
163                               $comparisonFailure->toString()
164                             );
165                         }
166
167                         if ($verbose &&
168                            !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array &&
169                            !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object &&
170                            !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
171                             $buffer .= $comparisonFailure->toString() . "\n";
172                         }
173                     }
174                 } else {
175                     $buffer .= $e->toString();
176                     $equal   = $buffer == $description;
177
178                     if (!empty($buffer)) {
179                         $buffer .= "\n";
180                     }
181
182                     if (!$equal) {
183                         $buffer .= $description . "\n";
184                     }
185                 }
186             }
187
188             else {
189                 $buffer = $e->toString();
190
191                 if (!empty($buffer)) {
192                     $buffer .= "\n";
193                 }
194             }
195         }
196
197         else if ($e instanceof PHPUnit_Framework_Error) {
198             $buffer = $e->getMessage() . "\n";
199         }
200
201         else {
202             $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
203         }
204
205         return $buffer;
206     }
207
208     /**
209      * Gets the failed test.
210      *
211      * @return Test
212      */
213     public function failedTest()
214     {
215         return $this->failedTest;
216     }
217
218     /**
219      * Gets the thrown exception.
220      *
221      * @return Exception
222      */
223     public function thrownException()
224     {
225         return $this->thrownException;
226     }
227
228     /**
229      * Returns the exception's message.
230      *
231      * @return string
232      */
233     public function exceptionMessage()
234     {
235         return $this->thrownException()->getMessage();
236     }
237
238     /**
239      * Returns TRUE if the thrown exception
240      * is of type AssertionFailedError.
241      *
242      * @return boolean
243      */
244     public function isFailure()
245     {
246         return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
247     }
248 }
249
250 }
251 ?>