|
20 | 20 | use const PHP_URL_PATH;
|
21 | 21 | use function array_keys;
|
22 | 22 | use function array_merge;
|
| 23 | +use function array_reverse; |
23 | 24 | use function array_values;
|
24 | 25 | use function assert;
|
25 | 26 | use function basename;
|
|
28 | 29 | use function clearstatcache;
|
29 | 30 | use function count;
|
30 | 31 | use function defined;
|
| 32 | +use function error_clear_last; |
31 | 33 | use function explode;
|
32 | 34 | use function getcwd;
|
33 | 35 | use function implode;
|
|
49 | 51 | use function pathinfo;
|
50 | 52 | use function preg_match;
|
51 | 53 | use function preg_replace;
|
| 54 | +use function restore_error_handler; |
| 55 | +use function restore_exception_handler; |
| 56 | +use function set_error_handler; |
| 57 | +use function set_exception_handler; |
52 | 58 | use function setlocale;
|
53 | 59 | use function sprintf;
|
54 | 60 | use function str_contains;
|
@@ -125,14 +131,24 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
|
125 | 131 | */
|
126 | 132 | private array $backupStaticPropertiesExcludeList = [];
|
127 | 133 | private ?Snapshot $snapshot = null;
|
128 |
| - private ?bool $runClassInSeparateProcess = null; |
129 |
| - private ?bool $runTestInSeparateProcess = null; |
130 |
| - private bool $preserveGlobalState = false; |
131 |
| - private bool $inIsolation = false; |
132 |
| - private ?string $expectedException = null; |
133 |
| - private ?string $expectedExceptionMessage = null; |
134 |
| - private ?string $expectedExceptionMessageRegExp = null; |
135 |
| - private null|int|string $expectedExceptionCode = null; |
| 134 | + |
| 135 | + /** |
| 136 | + * @psalm-var list<callable> |
| 137 | + */ |
| 138 | + private ?array $backupGlobalErrorHandlers = null; |
| 139 | + |
| 140 | + /** |
| 141 | + * @psalm-var list<callable> |
| 142 | + */ |
| 143 | + private ?array $backupGlobalExceptionHandlers = null; |
| 144 | + private ?bool $runClassInSeparateProcess = null; |
| 145 | + private ?bool $runTestInSeparateProcess = null; |
| 146 | + private bool $preserveGlobalState = false; |
| 147 | + private bool $inIsolation = false; |
| 148 | + private ?string $expectedException = null; |
| 149 | + private ?string $expectedExceptionMessage = null; |
| 150 | + private ?string $expectedExceptionMessageRegExp = null; |
| 151 | + private null|int|string $expectedExceptionCode = null; |
136 | 152 |
|
137 | 153 | /**
|
138 | 154 | * @psalm-var list<ExecutionOrderDependency>
|
@@ -422,13 +438,16 @@ final public function runBare(): void
|
422 | 438 | {
|
423 | 439 | $emitter = Event\Facade::emitter();
|
424 | 440 |
|
| 441 | + error_clear_last(); |
| 442 | + clearstatcache(); |
| 443 | + |
425 | 444 | $emitter->testPreparationStarted(
|
426 | 445 | $this->valueObjectForEvents(),
|
427 | 446 | );
|
428 | 447 |
|
429 | 448 | $this->snapshotGlobalState();
|
| 449 | + $this->snapshotGlobalErrorExceptionHandlers(); |
430 | 450 | $this->startOutputBuffering();
|
431 |
| - clearstatcache(); |
432 | 451 |
|
433 | 452 | $hookMethods = (new HookMethods)->hookMethods(static::class);
|
434 | 453 | $hasMetRequirements = false;
|
@@ -581,6 +600,7 @@ final public function runBare(): void
|
581 | 600 | chdir($currentWorkingDirectory);
|
582 | 601 | }
|
583 | 602 |
|
| 603 | + $this->restoreGlobalErrorExceptionHandlers(); |
584 | 604 | $this->restoreGlobalState();
|
585 | 605 | $this->unregisterCustomComparators();
|
586 | 606 | $this->cleanupIniSettings();
|
@@ -1795,6 +1815,116 @@ private function stopOutputBuffering(): bool
|
1795 | 1815 | return true;
|
1796 | 1816 | }
|
1797 | 1817 |
|
| 1818 | + private function snapshotGlobalErrorExceptionHandlers(): void |
| 1819 | + { |
| 1820 | + $this->backupGlobalErrorHandlers = $this->getActiveErrorHandlers(); |
| 1821 | + $this->backupGlobalExceptionHandlers = $this->getActiveExceptionHandlers(); |
| 1822 | + } |
| 1823 | + |
| 1824 | + private function restoreGlobalErrorExceptionHandlers(): void |
| 1825 | + { |
| 1826 | + $activeErrorHandlers = $this->getActiveErrorHandlers(); |
| 1827 | + $activeExceptionHandlers = $this->getActiveExceptionHandlers(); |
| 1828 | + |
| 1829 | + $message = null; |
| 1830 | + |
| 1831 | + if ($activeErrorHandlers !== $this->backupGlobalErrorHandlers) { |
| 1832 | + if (count($activeErrorHandlers) > count($this->backupGlobalErrorHandlers)) { |
| 1833 | + $message = 'Test code or tested code did not remove its own error handlers'; |
| 1834 | + } else { |
| 1835 | + $message = 'Test code or tested code removed error handlers other than its own'; |
| 1836 | + } |
| 1837 | + |
| 1838 | + foreach ($activeErrorHandlers as $handler) { |
| 1839 | + restore_error_handler(); |
| 1840 | + } |
| 1841 | + |
| 1842 | + foreach ($this->backupGlobalErrorHandlers as $handler) { |
| 1843 | + set_error_handler($handler); |
| 1844 | + } |
| 1845 | + } |
| 1846 | + |
| 1847 | + if ($activeExceptionHandlers !== $this->backupGlobalExceptionHandlers) { |
| 1848 | + if (count($activeExceptionHandlers) > count($this->backupGlobalExceptionHandlers)) { |
| 1849 | + $message = 'Test code or tested code did not remove its own exception handlers'; |
| 1850 | + } else { |
| 1851 | + $message = 'Test code or tested code removed exception handlers other than its own'; |
| 1852 | + } |
| 1853 | + |
| 1854 | + foreach ($activeExceptionHandlers as $handler) { |
| 1855 | + restore_exception_handler(); |
| 1856 | + } |
| 1857 | + |
| 1858 | + foreach ($this->backupGlobalExceptionHandlers as $handler) { |
| 1859 | + set_exception_handler($handler); |
| 1860 | + } |
| 1861 | + } |
| 1862 | + |
| 1863 | + $this->backupGlobalErrorHandlers = null; |
| 1864 | + $this->backupGlobalExceptionHandlers = null; |
| 1865 | + |
| 1866 | + if ($message !== null) { |
| 1867 | + Event\Facade::emitter()->testConsideredRisky( |
| 1868 | + $this->valueObjectForEvents(), |
| 1869 | + $message, |
| 1870 | + ); |
| 1871 | + |
| 1872 | + $this->status = TestStatus::risky($message); |
| 1873 | + } |
| 1874 | + } |
| 1875 | + |
| 1876 | + /** |
| 1877 | + * @return list<callable> |
| 1878 | + */ |
| 1879 | + private function getActiveErrorHandlers(): array |
| 1880 | + { |
| 1881 | + $res = []; |
| 1882 | + |
| 1883 | + while (true) { |
| 1884 | + $previousHandler = set_error_handler(static fn () => false); |
| 1885 | + restore_error_handler(); |
| 1886 | + |
| 1887 | + if ($previousHandler === null) { |
| 1888 | + break; |
| 1889 | + } |
| 1890 | + $res[] = $previousHandler; |
| 1891 | + restore_error_handler(); |
| 1892 | + } |
| 1893 | + $res = array_reverse($res); |
| 1894 | + |
| 1895 | + foreach ($res as $handler) { |
| 1896 | + set_error_handler($handler); |
| 1897 | + } |
| 1898 | + |
| 1899 | + return $res; |
| 1900 | + } |
| 1901 | + |
| 1902 | + /** |
| 1903 | + * @return list<callable> |
| 1904 | + */ |
| 1905 | + private function getActiveExceptionHandlers(): array |
| 1906 | + { |
| 1907 | + $res = []; |
| 1908 | + |
| 1909 | + while (true) { |
| 1910 | + $previousHandler = set_exception_handler(static fn () => null); |
| 1911 | + restore_exception_handler(); |
| 1912 | + |
| 1913 | + if ($previousHandler === null) { |
| 1914 | + break; |
| 1915 | + } |
| 1916 | + $res[] = $previousHandler; |
| 1917 | + restore_exception_handler(); |
| 1918 | + } |
| 1919 | + $res = array_reverse($res); |
| 1920 | + |
| 1921 | + foreach ($res as $handler) { |
| 1922 | + set_exception_handler($handler); |
| 1923 | + } |
| 1924 | + |
| 1925 | + return $res; |
| 1926 | + } |
| 1927 | + |
1798 | 1928 | private function snapshotGlobalState(): void
|
1799 | 1929 | {
|
1800 | 1930 | if ($this->runTestInSeparateProcess || $this->inIsolation ||
|
|
0 commit comments