|
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;
|
|
48 | 50 | use function parse_url;
|
49 | 51 | use function pathinfo;
|
50 | 52 | use function preg_replace;
|
| 53 | +use function restore_error_handler; |
| 54 | +use function restore_exception_handler; |
| 55 | +use function set_error_handler; |
| 56 | +use function set_exception_handler; |
51 | 57 | use function setlocale;
|
52 | 58 | use function sprintf;
|
53 | 59 | use function str_contains;
|
@@ -126,14 +132,24 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
|
126 | 132 | */
|
127 | 133 | private array $backupStaticPropertiesExcludeList = [];
|
128 | 134 | private ?Snapshot $snapshot = null;
|
129 |
| - private ?bool $runClassInSeparateProcess = null; |
130 |
| - private ?bool $runTestInSeparateProcess = null; |
131 |
| - private bool $preserveGlobalState = false; |
132 |
| - private bool $inIsolation = false; |
133 |
| - private ?string $expectedException = null; |
134 |
| - private ?string $expectedExceptionMessage = null; |
135 |
| - private ?string $expectedExceptionMessageRegExp = null; |
136 |
| - private null|int|string $expectedExceptionCode = null; |
| 135 | + |
| 136 | + /** |
| 137 | + * @psalm-var list<callable> |
| 138 | + */ |
| 139 | + private ?array $backupGlobalErrorHandlers = null; |
| 140 | + |
| 141 | + /** |
| 142 | + * @psalm-var list<callable> |
| 143 | + */ |
| 144 | + private ?array $backupGlobalExceptionHandlers = null; |
| 145 | + private ?bool $runClassInSeparateProcess = null; |
| 146 | + private ?bool $runTestInSeparateProcess = null; |
| 147 | + private bool $preserveGlobalState = false; |
| 148 | + private bool $inIsolation = false; |
| 149 | + private ?string $expectedException = null; |
| 150 | + private ?string $expectedExceptionMessage = null; |
| 151 | + private ?string $expectedExceptionMessageRegExp = null; |
| 152 | + private null|int|string $expectedExceptionCode = null; |
137 | 153 |
|
138 | 154 | /**
|
139 | 155 | * @psalm-var list<ExecutionOrderDependency>
|
@@ -618,13 +634,16 @@ final public function runBare(): void
|
618 | 634 | {
|
619 | 635 | $emitter = Event\Facade::emitter();
|
620 | 636 |
|
| 637 | + error_clear_last(); |
| 638 | + clearstatcache(); |
| 639 | + |
621 | 640 | $emitter->testPreparationStarted(
|
622 | 641 | $this->valueObjectForEvents(),
|
623 | 642 | );
|
624 | 643 |
|
625 | 644 | $this->snapshotGlobalState();
|
| 645 | + $this->snapshotGlobalErrorExceptionHandlers(); |
626 | 646 | $this->startOutputBuffering();
|
627 |
| - clearstatcache(); |
628 | 647 |
|
629 | 648 | $hookMethods = (new HookMethods)->hookMethods(static::class);
|
630 | 649 | $hasMetRequirements = false;
|
@@ -776,6 +795,7 @@ final public function runBare(): void
|
776 | 795 | chdir($currentWorkingDirectory);
|
777 | 796 | }
|
778 | 797 |
|
| 798 | + $this->restoreGlobalErrorExceptionHandlers(); |
779 | 799 | $this->restoreGlobalState();
|
780 | 800 | $this->unregisterCustomComparators();
|
781 | 801 | $this->cleanupIniSettings();
|
@@ -1683,6 +1703,119 @@ private function stopOutputBuffering(): bool
|
1683 | 1703 | return true;
|
1684 | 1704 | }
|
1685 | 1705 |
|
| 1706 | + private function snapshotGlobalErrorExceptionHandlers(): void |
| 1707 | + { |
| 1708 | + $this->backupGlobalErrorHandlers = $this->getActiveErrorHandlers(); |
| 1709 | + $this->backupGlobalExceptionHandlers = $this->getActiveExceptionHandlers(); |
| 1710 | + } |
| 1711 | + |
| 1712 | + /** |
| 1713 | + * @throws MoreThanOneDataSetFromDataProviderException |
| 1714 | + */ |
| 1715 | + private function restoreGlobalErrorExceptionHandlers(): void |
| 1716 | + { |
| 1717 | + $activeErrorHandlers = $this->getActiveErrorHandlers(); |
| 1718 | + $activeExceptionHandlers = $this->getActiveExceptionHandlers(); |
| 1719 | + |
| 1720 | + $message = null; |
| 1721 | + |
| 1722 | + if ($activeErrorHandlers !== $this->backupGlobalErrorHandlers) { |
| 1723 | + if (count($activeErrorHandlers) > count($this->backupGlobalErrorHandlers)) { |
| 1724 | + $message = 'Test code or tested code did not remove its own error handlers'; |
| 1725 | + } else { |
| 1726 | + $message = 'Test code or tested code removed error handlers other than its own'; |
| 1727 | + } |
| 1728 | + |
| 1729 | + foreach ($activeErrorHandlers as $handler) { |
| 1730 | + restore_error_handler(); |
| 1731 | + } |
| 1732 | + |
| 1733 | + foreach ($this->backupGlobalErrorHandlers as $handler) { |
| 1734 | + set_error_handler($handler); |
| 1735 | + } |
| 1736 | + } |
| 1737 | + |
| 1738 | + if ($activeExceptionHandlers !== $this->backupGlobalExceptionHandlers) { |
| 1739 | + if (count($activeExceptionHandlers) > count($this->backupGlobalExceptionHandlers)) { |
| 1740 | + $message = 'Test code or tested code did not remove its own exception handlers'; |
| 1741 | + } else { |
| 1742 | + $message = 'Test code or tested code removed exception handlers other than its own'; |
| 1743 | + } |
| 1744 | + |
| 1745 | + foreach ($activeExceptionHandlers as $handler) { |
| 1746 | + restore_exception_handler(); |
| 1747 | + } |
| 1748 | + |
| 1749 | + foreach ($this->backupGlobalExceptionHandlers as $handler) { |
| 1750 | + set_exception_handler($handler); |
| 1751 | + } |
| 1752 | + } |
| 1753 | + |
| 1754 | + $this->backupGlobalErrorHandlers = null; |
| 1755 | + $this->backupGlobalExceptionHandlers = null; |
| 1756 | + |
| 1757 | + if ($message !== null) { |
| 1758 | + Event\Facade::emitter()->testConsideredRisky( |
| 1759 | + $this->valueObjectForEvents(), |
| 1760 | + $message, |
| 1761 | + ); |
| 1762 | + |
| 1763 | + $this->status = TestStatus::risky($message); |
| 1764 | + } |
| 1765 | + } |
| 1766 | + |
| 1767 | + /** |
| 1768 | + * @return list<callable> |
| 1769 | + */ |
| 1770 | + private function getActiveErrorHandlers(): array |
| 1771 | + { |
| 1772 | + $res = []; |
| 1773 | + |
| 1774 | + while (true) { |
| 1775 | + $previousHandler = set_error_handler(static fn () => false); |
| 1776 | + restore_error_handler(); |
| 1777 | + |
| 1778 | + if ($previousHandler === null) { |
| 1779 | + break; |
| 1780 | + } |
| 1781 | + $res[] = $previousHandler; |
| 1782 | + restore_error_handler(); |
| 1783 | + } |
| 1784 | + $res = array_reverse($res); |
| 1785 | + |
| 1786 | + foreach ($res as $handler) { |
| 1787 | + set_error_handler($handler); |
| 1788 | + } |
| 1789 | + |
| 1790 | + return $res; |
| 1791 | + } |
| 1792 | + |
| 1793 | + /** |
| 1794 | + * @return list<callable> |
| 1795 | + */ |
| 1796 | + private function getActiveExceptionHandlers(): array |
| 1797 | + { |
| 1798 | + $res = []; |
| 1799 | + |
| 1800 | + while (true) { |
| 1801 | + $previousHandler = set_exception_handler(static fn () => null); |
| 1802 | + restore_exception_handler(); |
| 1803 | + |
| 1804 | + if ($previousHandler === null) { |
| 1805 | + break; |
| 1806 | + } |
| 1807 | + $res[] = $previousHandler; |
| 1808 | + restore_exception_handler(); |
| 1809 | + } |
| 1810 | + $res = array_reverse($res); |
| 1811 | + |
| 1812 | + foreach ($res as $handler) { |
| 1813 | + set_exception_handler($handler); |
| 1814 | + } |
| 1815 | + |
| 1816 | + return $res; |
| 1817 | + } |
| 1818 | + |
1686 | 1819 | private function snapshotGlobalState(): void
|
1687 | 1820 | {
|
1688 | 1821 | if ($this->runTestInSeparateProcess || $this->inIsolation ||
|
|
0 commit comments