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 const PHP_VERSION_ID;
13
use function array_keys;
14
use function get_class;
15
use function spl_object_hash;
16
use PHPUnit\Util\Filter;
17
use Throwable;
18
use WeakReference;
19
 
20
/**
21
 * Wraps Exceptions thrown by code under test.
22
 *
23
 * Re-instantiates Exceptions thrown by user-space code to retain their original
24
 * class names, properties, and stack traces (but without arguments).
25
 *
26
 * Unlike PHPUnit\Framework\Exception, the complete stack of previous Exceptions
27
 * is processed.
28
 *
29
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
30
 */
31
final class ExceptionWrapper extends Exception
32
{
33
    /**
34
     * @var string
35
     */
36
    protected $className;
37
 
38
    /**
39
     * @var null|ExceptionWrapper
40
     */
41
    protected $previous;
42
 
43
    /**
44
     * @var null|WeakReference<Throwable>
45
     */
46
    private $originalException;
47
 
48
    public function __construct(Throwable $t)
49
    {
50
        // PDOException::getCode() is a string.
51
        // @see https://php.net/manual/en/class.pdoexception.php#95812
52
        parent::__construct($t->getMessage(), (int) $t->getCode());
53
 
54
        $this->setOriginalException($t);
55
    }
56
 
57
    public function __toString(): string
58
    {
59
        $string = TestFailure::exceptionToString($this);
60
 
61
        if ($trace = Filter::getFilteredStacktrace($this)) {
62
            $string .= "\n" . $trace;
63
        }
64
 
65
        if ($this->previous) {
66
            $string .= "\nCaused by\n" . $this->previous;
67
        }
68
 
69
        return $string;
70
    }
71
 
72
    public function getClassName(): string
73
    {
74
        return $this->className;
75
    }
76
 
77
    public function getPreviousWrapped(): ?self
78
    {
79
        return $this->previous;
80
    }
81
 
82
    public function setClassName(string $className): void
83
    {
84
        $this->className = $className;
85
    }
86
 
87
    public function setOriginalException(Throwable $t): void
88
    {
89
        $this->originalException($t);
90
 
91
        $this->className = get_class($t);
92
        $this->file      = $t->getFile();
93
        $this->line      = $t->getLine();
94
 
95
        $this->serializableTrace = $t->getTrace();
96
 
97
        foreach (array_keys($this->serializableTrace) as $key) {
98
            unset($this->serializableTrace[$key]['args']);
99
        }
100
 
101
        if ($t->getPrevious()) {
102
            $this->previous = new self($t->getPrevious());
103
        }
104
    }
105
 
106
    public function getOriginalException(): ?Throwable
107
    {
108
        return $this->originalException();
109
    }
110
 
111
    /**
112
     * Method to contain static originalException to exclude it from stacktrace to prevent the stacktrace contents,
113
     * which can be quite big, from being garbage-collected, thus blocking memory until shutdown.
114
     *
115
     * Approach works both for var_dump() and var_export() and print_r().
116
     */
117
    private function originalException(Throwable $exceptionToStore = null): ?Throwable
118
    {
119
        // drop once PHP 7.3 support is removed
120
        if (PHP_VERSION_ID < 70400) {
121
            static $originalExceptions;
122
 
123
            $instanceId = spl_object_hash($this);
124
 
125
            if ($exceptionToStore) {
126
                $originalExceptions[$instanceId] = $exceptionToStore;
127
            }
128
 
129
            return $originalExceptions[$instanceId] ?? null;
130
        }
131
 
132
        if ($exceptionToStore) {
133
            $this->originalException = WeakReference::create($exceptionToStore);
134
        }
135
 
136
        return $this->originalException !== null ? $this->originalException->get() : null;
137
    }
138
}