‪TYPO3CMS  ‪main
ErrorHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 final class ‪ErrorHandlerTest extends FunctionalTestCase
28 {
29  protected bool ‪$initializeDatabase = false;
30 
32  'SYS' => [
33  'errorHandler' => ErrorHandler::class,
34  ],
35  ];
36 
37  public function ‪tearDown(): void
38  {
39  // Unset errorHandler instance configured for this test case
40  restore_error_handler();
41  parent::tearDown();
42  }
43 
44  #[Test]
45  public function ‪handleErrorFetchesDeprecations(): void
46  {
47  trigger_error(
48  'The first error triggers database connection to be initialized and should be caught.',
49  E_USER_DEPRECATED
50  );
51  trigger_error(
52  'The second error should be caught by ErrorHandler as well.',
53  E_USER_DEPRECATED
54  );
55  self::assertTrue(true);
56  }
57 
71  #[Test]
73  {
74  // Make sure the core error handler does not return due to error_reporting being 0
75  self::assertNotSame(0, error_reporting());
76 
77  // Make sure the core error handler does not return true due to a deprecation error
78  $logManagerMock = $this->createMock(LogManager::class);
79  $logManagerMock->expects(self::never())->method('getLogger')->with('TYPO3.CMS.deprecations');
80  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
81 
82  $logger = $this->getMockBuilder(Logger::class)
83  ->disableOriginalConstructor()
84  ->onlyMethods(['log'])
85  ->getMock();
86 
87  // Make sure the assigned logger does not log
88  $logger->expects(self::never())->method('log');
89 
90  $coreErrorHandler = new ‪ErrorHandler(
91  E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)
92  );
93  $coreErrorHandler->setLogger($logger);
94 
95  $customErrorHandler = new class () {
96  protected $existingHandler;
97 
98  public function setExistingHandler($existingHandler): void
99  {
100  $this->existingHandler = $existingHandler;
101  }
102 
103  public function handleError(int $code, string $message, string $file = '', int $line = 0, array $context = []): mixed
104  {
105  // process errors
106  if ($this->existingHandler !== null) {
107  return ($this->existingHandler)($code, $message, $file, $line, $context);
108  }
109 
110  return false;
111  }
112  };
113 
114  $existingHandler = set_error_handler([$customErrorHandler, 'handleError'], E_ALL);
115  $customErrorHandler->setExistingHandler($existingHandler);
116 
117  // This assertion is the base assertion but as \TYPO3\CMS\Core\Error\ErrorHandler::handleError has a few return
118  // points that return true, the expectation on dependency objects are in place. We want to be sure that the
119  // first return point is used by checking that the method does not log anything, which happens before later
120  // return points that return true.
121  self::assertTrue($customErrorHandler->handleError(E_NOTICE, 'Notice error message', __FILE__, __LINE__));
122 
123  // Unset the closure error handler again
124  restore_error_handler();
125  }
126 }
‪TYPO3\CMS\Core\Tests\Functional\Error
Definition: ErrorHandlerTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: ErrorHandlerTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorOnlyHandlesRegisteredErrorLevels
‪handleErrorOnlyHandlesRegisteredErrorLevels()
Definition: ErrorHandlerTest.php:72
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorFetchesDeprecations
‪handleErrorFetchesDeprecations()
Definition: ErrorHandlerTest.php:45
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\tearDown
‪tearDown()
Definition: ErrorHandlerTest.php:37
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\$initializeDatabase
‪bool $initializeDatabase
Definition: ErrorHandlerTest.php:29
‪TYPO3\CMS\Core\Error\ErrorHandler
Definition: ErrorHandler.php:41
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest
Definition: ErrorHandlerTest.php:28