‪TYPO3CMS  11.5
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 
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪ErrorHandlerTest extends FunctionalTestCase
27 {
31  protected ‪$initializeDatabase = false;
32 
34  'SYS' => [
35  'errorHandler' => ErrorHandler::class,
36  ],
37  ];
38 
39  public function ‪tearDown(): void
40  {
41  // Unset errorHandler instance configured for this test case
42  restore_error_handler();
43  parent::tearDown();
44  }
45 
49  public function ‪handleErrorFetchesDeprecations(): void
50  {
51  trigger_error(
52  'The first error triggers database connection to be initialized and should be caught.',
53  E_USER_DEPRECATED
54  );
55  trigger_error(
56  'The second error should be caught by ErrorHandler as well.',
57  E_USER_DEPRECATED
58  );
59  self::assertTrue(true);
60  }
61 
78  {
79  // Make sure the core error handler does not return due to error_reporting being 0
80  self::assertNotSame(0, error_reporting());
81 
82  // Make sure the core error handler does not return true due to a deprecation error
83  $logManagerMock = $this->createMock(LogManager::class);
84  $logManagerMock->expects(self::never())->method('getLogger')->with('TYPO3.CMS.deprecations');
85  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
86 
87  $logger = $this->getMockBuilder(Logger::class)
88  ->disableOriginalConstructor()
89  ->onlyMethods(['log'])
90  ->getMock();
91 
92  // Make sure the assigned logger does not log
93  $logger->expects(self::never())->method('log');
94 
95  $coreErrorHandler = new ErrorHandler(
96  E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)
97  );
98  $coreErrorHandler->setLogger($logger);
99 
100  $customErrorHandler = new class () {
101  protected $existingHandler;
102 
103  public function setExistingHandler($existingHandler): void
104  {
105  $this->existingHandler = $existingHandler;
106  }
107 
116  public function handleError(int $code, string $message, string $file = '', int $line = 0, array $context = [])
117  {
118  // process errors
119  if ($this->existingHandler !== null) {
120  return ($this->existingHandler)($code, $message, $file, $line, $context);
121  }
122 
123  return false;
124  }
125  };
126 
127  $existingHandler = set_error_handler([$customErrorHandler, 'handleError'], E_ALL);
128  $customErrorHandler->setExistingHandler($existingHandler);
129 
130  // This assertion is the base assertion but as \TYPO3\CMS\Core\Error\ErrorHandler::handleError has a few return
131  // points that return true, the expectation on dependency objects are in place. We want to be sure that the
132  // first return point is used by checking that the method does not log anything, which happens before later
133  // return points that return true.
134  self::assertTrue($customErrorHandler->handleError(E_NOTICE, 'Notice error message', __FILE__, __LINE__));
135 
136  // Unset the closure error handler again
137  restore_error_handler();
138  }
139 }
‪TYPO3\CMS\Core\Tests\Functional\Error
Definition: ErrorHandlerTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorOnlyHandlesRegisteredErrorLevels
‪handleErrorOnlyHandlesRegisteredErrorLevels()
Definition: ErrorHandlerTest.php:76
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorFetchesDeprecations
‪handleErrorFetchesDeprecations()
Definition: ErrorHandlerTest.php:48
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\tearDown
‪tearDown()
Definition: ErrorHandlerTest.php:38
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\$configurationToUseInTestInstance
‪$configurationToUseInTestInstance
Definition: ErrorHandlerTest.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\$initializeDatabase
‪bool $initializeDatabase
Definition: ErrorHandlerTest.php:30
‪TYPO3\CMS\Core\Error\ErrorHandler
Definition: ErrorHandler.php:40
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest
Definition: ErrorHandlerTest.php:27