diff --git a/lib/Error/DebugErrorHandler.php b/lib/Error/DebugErrorHandler.php index 0bd388f13..9c270ded2 100644 --- a/lib/Error/DebugErrorHandler.php +++ b/lib/Error/DebugErrorHandler.php @@ -32,12 +32,7 @@ public function handleError(Throwable $throwable, ?string $message = null, array } } - /** - * Logs the error. - * - * @param array $context - */ - private function logError(Throwable $throwable, ?string $message = null, array $context = []): void + public function logError(Throwable $throwable, ?string $message = null, array $context = []): void { $context['error'] = $throwable; diff --git a/lib/Error/ErrorHandlerInterface.php b/lib/Error/ErrorHandlerInterface.php index 6768e98c7..b2a742079 100644 --- a/lib/Error/ErrorHandlerInterface.php +++ b/lib/Error/ErrorHandlerInterface.php @@ -14,4 +14,11 @@ interface ErrorHandlerInterface * @param array $context */ public function handleError(Throwable $throwable, ?string $message = null, array $context = []): void; + + /** + * Logs the error. Context can be arbitrary data relevant to the error. + * + * @param array $context + */ + public function logError(Throwable $throwable, ?string $message = null, array $context = []): void; } diff --git a/tests/lib/Error/DebugErrorHandlerTest.php b/tests/lib/Error/DebugErrorHandlerTest.php index a0dab512f..af03ecdfa 100644 --- a/tests/lib/Error/DebugErrorHandlerTest.php +++ b/tests/lib/Error/DebugErrorHandlerTest.php @@ -198,4 +198,81 @@ public function testHandleErrorThrowsErrorWithContext(): void $this->errorHandler->handleError($exception, null, ['value' => 42]); } + + /** + * @covers \Netgen\Layouts\Error\DebugErrorHandler::__construct + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + */ + public function testLogError(): void + { + $exception = new Exception('Test message'); + + $this->loggerMock + ->expects(self::once()) + ->method('critical') + ->with( + self::identicalTo('Test message'), + self::identicalTo(['error' => $exception]), + ); + + $this->errorHandler->logError($exception); + } + + /** + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + */ + public function testLogErrorWithCustomMessage(): void + { + $exception = new Exception('Test message'); + + $this->loggerMock + ->expects(self::once()) + ->method('critical') + ->with( + self::identicalTo('Custom message'), + self::identicalTo(['error' => $exception]), + ); + + $this->errorHandler->logError($exception, 'Custom message'); + } + + /** + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + */ + public function testLogErrorWithEmptyMessage(): void + { + $exception = new Exception('Test message'); + + $this->loggerMock + ->expects(self::once()) + ->method('critical') + ->with( + self::identicalTo(''), + self::identicalTo(['error' => $exception]), + ); + + $this->errorHandler->logError($exception, ''); + } + + /** + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + * @covers \Netgen\Layouts\Error\DebugErrorHandler::logError + */ + public function testLogErrorWithContext(): void + { + $exception = new Exception('Test message'); + + $this->loggerMock + ->expects(self::once()) + ->method('critical') + ->with( + self::identicalTo('Test message'), + self::identicalTo(['value' => 42, 'error' => $exception]), + ); + + $this->errorHandler->logError($exception, null, ['value' => 42]); + } } diff --git a/tests/lib/Stubs/ErrorHandler.php b/tests/lib/Stubs/ErrorHandler.php index 5d2eba498..4e2a99106 100644 --- a/tests/lib/Stubs/ErrorHandler.php +++ b/tests/lib/Stubs/ErrorHandler.php @@ -22,4 +22,8 @@ public function handleError(Throwable $throwable, ?string $message = null, array throw $throwable; } } + + public function logError(Throwable $throwable, ?string $message = null, array $context = []): void + { + } }