diff --git a/src/Lunr/Halo/LunrBaseTestCase.php b/src/Lunr/Halo/LunrBaseTestCase.php index 9f0778e1..c97b05e0 100644 --- a/src/Lunr/Halo/LunrBaseTestCase.php +++ b/src/Lunr/Halo/LunrBaseTestCase.php @@ -54,12 +54,24 @@ abstract class LunrBaseTestCase extends TestCase */ private bool $isUserWarningHandlerSet = FALSE; + /** + * Whether we have an error handler set for E_WARNING. + * @var bool + */ + private bool $isWarningHandlerSet = FALSE; + /** * Whether we have an error handler set for E_USER_ERROR. * @var bool */ private bool $isUserErrorHandlerSet = FALSE; + /** + * Whether we have an error handler set for E_ERROR. + * @var bool + */ + private bool $isErrorHandlerSet = FALSE; + /** * Whether we have an error handler set for E_USER_DEPRECATED. * @var bool @@ -110,12 +122,24 @@ public function tearDown(): void $this->isUserWarningHandlerSet = FALSE; } + if ($this->isWarningHandlerSet) + { + restore_error_handler(); + $this->isWarningHandlerSet = FALSE; + } + if ($this->isUserErrorHandlerSet) { restore_error_handler(); $this->isUserErrorHandlerSet = FALSE; } + if ($this->isErrorHandlerSet) + { + restore_error_handler(); + $this->isErrorHandlerSet = FALSE; + } + if ($this->isUserDeprecatedHandlerSet) { restore_error_handler(); @@ -688,6 +712,32 @@ function (int $errno, string $errstr): bool { $this->expectOutputString(implode("\n", $this->outputStrings)); } + /** + * Expect an E_WARNING error. + * + * @param string $message The error message to expect + * + * @return void + */ + public function expectWarning(string $message): void + { + if (!$this->isUserWarningHandlerSet) + { + set_error_handler( + function (int $errno, string $errstr): bool { + echo "WARNING: $errstr\n"; + return TRUE; + }, + E_WARNING, + ); + $this->isUserWarningHandlerSet = TRUE; + } + + $this->outputStrings[] = "WARNING: $message\n"; + + $this->expectOutputString(implode("\n", $this->outputStrings)); + } + /** * Expect an E_USER_ERROR error. * @@ -714,6 +764,32 @@ function (int $errno, string $errstr): bool { $this->expectOutputString(implode("\n", $this->outputStrings)); } + /** + * Expect an E_ERROR error. + * + * @param string $message The error message to expect + * + * @return void + */ + public function expectError(string $message): void + { + if (!$this->isErrorHandlerSet) + { + set_error_handler( + function (int $errno, string $errstr): bool { + echo "ERROR: $errstr\n"; + return TRUE; + }, + E_ERROR, + ); + $this->isErrorHandlerSet = TRUE; + } + + $this->outputStrings[] = "ERROR: $message\n"; + + $this->expectOutputString(implode("\n", $this->outputStrings)); + } + /** * Expect an E_USER_DEPRECATED error. *