‪TYPO3CMS  9.5
ErrorHandlerTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use PHPUnit\Framework\MockObject\MockObject;
23 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
29 class ‪ErrorHandlerTest extends FunctionalTestCase
30 {
35  'DB' => [
36  'Connections' => [
37  'Default' => [
38  'initCommands' => 'SET NAMES \'UTF8\';',
39  ],
40  ],
41  ],
42  ];
43 
52  public function ‪handleErrorFetchesDeprecations()
53  {
54  trigger_error(
55  'The first error triggers database connection to be initialized and should be caught.',
56  E_USER_DEPRECATED
57  );
58  trigger_error(
59  'The second error should be caught by ErrorHandler as well.',
60  E_USER_DEPRECATED
61  );
62  $this->assertTrue(true);
63  }
64 
83  {
84  // Make sure the core error handler does not return due to error_reporting being 0
85  static::assertNotSame(0, error_reporting());
86 
87  // Make sure the core error handler does not return true due to a deprecation error
88  $logManagerMock = $this->createMock(LogManager::class);
89  $logManagerMock->expects($this->never())->method('getLogger')->with('TYPO3.CMS.deprecations');
90  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
91 
93  $logger = $this->getMockBuilder(Logger::class)
94  ->disableOriginalConstructor()
95  ->setMethods(['log'])
96  ->getMock();
97 
98  // Make sure the assigned logger does not log
99  $logger->expects($this->never())->method('log');
100 
102  $coreErrorHandler = new ‪ErrorHandler(
103  E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)
104  );
105  $coreErrorHandler->setLogger($logger);
106 
107  $customErrorHandler = new class {
108  protected $existingHandler;
109 
110  public function setExistingHandler($existingHandler)
111  {
112  $this->existingHandler = $existingHandler;
113  }
114 
123  public function handleError($code, $message, $file = '', $line = 0, $context = [])
124  {
125  // process errors
126  if ($this->existingHandler !== null) {
127  return call_user_func($this->existingHandler, $code, $message, $file, $line, $context);
128  }
129 
130  return false;
131  }
132  };
133 
134  $existingHandler = set_error_handler([$customErrorHandler, 'handleError'], E_ALL);
135  $customErrorHandler->setExistingHandler($existingHandler);
136 
137  static::assertTrue($customErrorHandler->handleError(E_NOTICE, 'Notice error message', __FILE__, __LINE__));
138  // This assertion is the base assertion but as \TYPO3\CMS\Core\Error\ErrorHandler::handleError has a few return
139  // points that return true, the expectation on dependency objects are in place. We want to be sure that the
140  // first return point is used by checking that the method does not log anything, which happens before later
141  // return points that return true.
142  }
143 }
‪TYPO3\CMS\Core\Tests\Functional\Error
Definition: ErrorHandlerTest.php:3
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: ErrorHandlerTest.php:33
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorOnlyHandlesRegisteredErrorLevels
‪handleErrorOnlyHandlesRegisteredErrorLevels()
Definition: ErrorHandlerTest.php:81
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorFetchesDeprecations
‪handleErrorFetchesDeprecations()
Definition: ErrorHandlerTest.php:51
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:25
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Error\ErrorHandler
Definition: ErrorHandler.php:32
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest
Definition: ErrorHandlerTest.php:30