Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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
namespace PHPUnit\Framework;
11
 
12
use function get_class;
13
use function sprintf;
14
use function trim;
15
use PHPUnit\Framework\Error\Error;
16
use Throwable;
17
 
18
/**
19
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
20
 */
21
final class TestFailure
22
{
23
    /**
24
     * @var null|Test
25
     */
26
    private $failedTest;
27
 
28
    /**
29
     * @var Throwable
30
     */
31
    private $thrownException;
32
 
33
    /**
34
     * @var string
35
     */
36
    private $testName;
37
 
38
    /**
39
     * Returns a description for an exception.
40
     */
41
    public static function exceptionToString(Throwable $e): string
42
    {
43
        if ($e instanceof SelfDescribing) {
44
            $buffer = $e->toString();
45
 
46
            if ($e instanceof ExpectationFailedException && $e->getComparisonFailure()) {
47
                $buffer .= $e->getComparisonFailure()->getDiff();
48
            }
49
 
50
            if ($e instanceof PHPTAssertionFailedError) {
51
                $buffer .= $e->getDiff();
52
            }
53
 
54
            if (!empty($buffer)) {
55
                $buffer = trim($buffer) . "\n";
56
            }
57
 
58
            return $buffer;
59
        }
60
 
61
        if ($e instanceof Error) {
62
            return $e->getMessage() . "\n";
63
        }
64
 
65
        if ($e instanceof ExceptionWrapper) {
66
            return $e->getClassName() . ': ' . $e->getMessage() . "\n";
67
        }
68
 
69
        return get_class($e) . ': ' . $e->getMessage() . "\n";
70
    }
71
 
72
    /**
73
     * Constructs a TestFailure with the given test and exception.
74
     */
75
    public function __construct(Test $failedTest, Throwable $t)
76
    {
77
        if ($failedTest instanceof SelfDescribing) {
78
            $this->testName = $failedTest->toString();
79
        } else {
80
            $this->testName = get_class($failedTest);
81
        }
82
 
83
        if (!$failedTest instanceof TestCase || !$failedTest->isInIsolation()) {
84
            $this->failedTest = $failedTest;
85
        }
86
 
87
        $this->thrownException = $t;
88
    }
89
 
90
    /**
91
     * Returns a short description of the failure.
92
     */
93
    public function toString(): string
94
    {
95
        return sprintf(
96
            '%s: %s',
97
            $this->testName,
98
            $this->thrownException->getMessage()
99
        );
100
    }
101
 
102
    /**
103
     * Returns a description for the thrown exception.
104
     */
105
    public function getExceptionAsString(): string
106
    {
107
        return self::exceptionToString($this->thrownException);
108
    }
109
 
110
    /**
111
     * Returns the name of the failing test (including data set, if any).
112
     */
113
    public function getTestName(): string
114
    {
115
        return $this->testName;
116
    }
117
 
118
    /**
119
     * Returns the failing test.
120
     *
121
     * Note: The test object is not set when the test is executed in process
122
     * isolation.
123
     *
124
     * @see Exception
125
     */
126
    public function failedTest(): ?Test
127
    {
128
        return $this->failedTest;
129
    }
130
 
131
    /**
132
     * Gets the thrown exception.
133
     */
134
    public function thrownException(): Throwable
135
    {
136
        return $this->thrownException;
137
    }
138
 
139
    /**
140
     * Returns the exception's message.
141
     */
142
    public function exceptionMessage(): string
143
    {
144
        return $this->thrownException()->getMessage();
145
    }
146
 
147
    /**
148
     * Returns true if the thrown exception
149
     * is of type AssertionFailedError.
150
     */
151
    public function isFailure(): bool
152
    {
153
        return $this->thrownException() instanceof AssertionFailedError;
154
    }
155
}