9
9
*/
10
10
namespace PHPUnit \Framework ;
11
11
12
+ use const PHP_VERSION_ID ;
12
13
use function array_keys ;
13
14
use function get_class ;
14
15
use function spl_object_hash ;
15
16
use PHPUnit \Util \Filter ;
16
17
use Throwable ;
18
+ use WeakReference ;
17
19
18
20
/**
19
21
* Wraps Exceptions thrown by code under test.
@@ -38,6 +40,11 @@ final class ExceptionWrapper extends Exception
38
40
*/
39
41
protected $ previous ;
40
42
43
+ /**
44
+ * @var null|WeakReference<Throwable>
45
+ */
46
+ private $ originalException ;
47
+
41
48
public function __construct (Throwable $ t )
42
49
{
43
50
// PDOException::getCode() is a string.
@@ -109,14 +116,23 @@ public function getOriginalException(): ?Throwable
109
116
*/
110
117
private function originalException (Throwable $ exceptionToStore = null ): ?Throwable
111
118
{
112
- static $ originalExceptions ;
119
+ // drop once PHP 7.3 support is removed
120
+ if (PHP_VERSION_ID < 70400 ) {
121
+ static $ originalExceptions ;
113
122
114
- $ instanceId = spl_object_hash ($ this );
123
+ $ instanceId = spl_object_hash ($ this );
124
+
125
+ if ($ exceptionToStore ) {
126
+ $ originalExceptions [$ instanceId ] = $ exceptionToStore ;
127
+ }
128
+
129
+ return $ originalExceptions [$ instanceId ] ?? null ;
130
+ }
115
131
116
132
if ($ exceptionToStore ) {
117
- $ originalExceptions [ $ instanceId ] = $ exceptionToStore ;
133
+ $ this -> originalException = WeakReference:: create ( $ exceptionToStore) ;
118
134
}
119
135
120
- return $ originalExceptions [ $ instanceId ] ?? null ;
136
+ return $ this -> originalException !== null ? $ this -> originalException -> get () : null ;
121
137
}
122
138
}
0 commit comments