Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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