~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Framework/TestFailure.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * @package    PHPUnit
 
15
 * @subpackage Framework
 
16
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
17
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
18
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
19
 * @link       http://www.phpunit.de/
 
20
 * @since      Class available since Release 2.0.0
 
21
 */
 
22
class PHPUnit_Framework_TestFailure
 
23
{
 
24
    /**
 
25
     * @var string
 
26
     */
 
27
    private $testName;
 
28
 
 
29
    /**
 
30
     * @var PHPUnit_Framework_Test|null
 
31
     */
 
32
    protected $failedTest;
 
33
 
 
34
    /**
 
35
     * @var    Exception
 
36
     */
 
37
    protected $thrownException;
 
38
 
 
39
    /**
 
40
     * Constructs a TestFailure with the given test and exception.
 
41
     *
 
42
     * @param PHPUnit_Framework_Test $failedTest
 
43
     * @param Exception              $thrownException
 
44
     */
 
45
    public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
 
46
    {
 
47
        if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
 
48
            $this->testName = $failedTest->toString();
 
49
        } else {
 
50
            $this->testName = get_class($failedTest);
 
51
        }
 
52
        if (!$failedTest instanceof PHPUnit_Framework_TestCase || !$failedTest->isInIsolation()) {
 
53
            $this->failedTest = $failedTest;
 
54
        }
 
55
        $this->thrownException = $thrownException;
 
56
    }
 
57
 
 
58
    /**
 
59
     * Returns a short description of the failure.
 
60
     *
 
61
     * @return string
 
62
     */
 
63
    public function toString()
 
64
    {
 
65
        return sprintf(
 
66
            '%s: %s',
 
67
            $this->testName,
 
68
            $this->thrownException->getMessage()
 
69
        );
 
70
    }
 
71
 
 
72
    /**
 
73
     * Returns a description for the thrown exception.
 
74
     *
 
75
     * @return string
 
76
     * @since  Method available since Release 3.4.0
 
77
     */
 
78
    public function getExceptionAsString()
 
79
    {
 
80
        return self::exceptionToString($this->thrownException);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Returns a description for an exception.
 
85
     *
 
86
     * @param  Exception $e
 
87
     * @return string
 
88
     * @since  Method available since Release 3.2.0
 
89
     */
 
90
    public static function exceptionToString(Exception $e)
 
91
    {
 
92
        if ($e instanceof PHPUnit_Framework_SelfDescribing) {
 
93
            $buffer = $e->toString();
 
94
 
 
95
            if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
 
96
                $buffer = $buffer . $e->getComparisonFailure()->getDiff();
 
97
            }
 
98
 
 
99
            if (!empty($buffer)) {
 
100
                $buffer = trim($buffer) . "\n";
 
101
            }
 
102
        } elseif ($e instanceof PHPUnit_Framework_Error) {
 
103
            $buffer = $e->getMessage() . "\n";
 
104
        } elseif ($e instanceof PHPUnit_Framework_ExceptionWrapper) {
 
105
            $buffer = $e->getClassname() . ': ' . $e->getMessage() . "\n";
 
106
        } else {
 
107
            $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
 
108
        }
 
109
 
 
110
        return $buffer;
 
111
    }
 
112
 
 
113
    /**
 
114
     * Returns the name of the failing test (including data set, if any).
 
115
     *
 
116
     * @return string
 
117
     * @since  Method available since Release 4.3.0
 
118
     */
 
119
    public function getTestName()
 
120
    {
 
121
        return $this->testName;
 
122
    }
 
123
 
 
124
    /**
 
125
     * Returns the failing test.
 
126
     *
 
127
     * Note: The test object is not set when the test is executed in process
 
128
     * isolation.
 
129
     *
 
130
     * @see PHPUnit_Framework_Exception
 
131
     *
 
132
     * @return PHPUnit_Framework_Test|null
 
133
     */
 
134
    public function failedTest()
 
135
    {
 
136
        return $this->failedTest;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Gets the thrown exception.
 
141
     *
 
142
     * @return Exception
 
143
     */
 
144
    public function thrownException()
 
145
    {
 
146
        return $this->thrownException;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Returns the exception's message.
 
151
     *
 
152
     * @return string
 
153
     */
 
154
    public function exceptionMessage()
 
155
    {
 
156
        return $this->thrownException()->getMessage();
 
157
    }
 
158
 
 
159
    /**
 
160
     * Returns true if the thrown exception
 
161
     * is of type AssertionFailedError.
 
162
     *
 
163
     * @return boolean
 
164
     */
 
165
    public function isFailure()
 
166
    {
 
167
        return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
 
168
    }
 
169
}