‪TYPO3CMS  10.4
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\MockObject\MockObject;
25 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
31 class ‪ErrorHandlerTest extends FunctionalTestCase
32 {
42  {
43  trigger_error(
44  'The first error triggers database connection to be initialized and should be caught.',
45  E_USER_DEPRECATED
46  );
47  trigger_error(
48  'The second error should be caught by ErrorHandler as well.',
49  E_USER_DEPRECATED
50  );
51  self::assertTrue(true);
52  }
53 
72  {
73  // Make sure the core error handler does not return due to error_reporting being 0
74  self::assertNotSame(0, error_reporting());
75 
76  // Make sure the core error handler does not return true due to a deprecation error
77  $logManagerMock = $this->createMock(LogManager::class);
78  $logManagerMock->expects(self::never())->method('getLogger')->with('TYPO3.CMS.deprecations');
79  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
80 
82  $logger = $this->getMockBuilder(Logger::class)
83  ->disableOriginalConstructor()
84  ->setMethods(['log'])
85  ->getMock();
86 
87  // Make sure the assigned logger does not log
88  $logger->expects(self::never())->method('log');
89 
91  $coreErrorHandler = new ‪ErrorHandler(
92  E_ALL & ~(E_STRICT | E_NOTICE | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)
93  );
94  $coreErrorHandler->setLogger($logger);
95 
96  $customErrorHandler = new class() {
97  protected $existingHandler;
98 
99  public function setExistingHandler($existingHandler)
100  {
101  $this->existingHandler = $existingHandler;
102  }
103 
112  public function handleError($code, $message, $file = '', $line = 0, $context = [])
113  {
114  // process errors
115  if ($this->existingHandler !== null) {
116  return call_user_func($this->existingHandler, $code, $message, $file, $line, $context);
117  }
118 
119  return false;
120  }
121  };
122 
123  $existingHandler = set_error_handler([$customErrorHandler, 'handleError'], E_ALL);
124  $customErrorHandler->setExistingHandler($existingHandler);
125 
126  self::assertTrue($customErrorHandler->handleError(E_NOTICE, 'Notice error message', __FILE__, __LINE__));
127  // This assertion is the base assertion but as \TYPO3\CMS\Core\Error\ErrorHandler::handleError has a few return
128  // points that return true, the expectation on dependency objects are in place. We want to be sure that the
129  // first return point is used by checking that the method does not log anything, which happens before later
130  // return points that return true.
131  }
132 }
‪TYPO3\CMS\Core\Tests\Functional\Error
Definition: ErrorHandlerTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorOnlyHandlesRegisteredErrorLevels
‪handleErrorOnlyHandlesRegisteredErrorLevels()
Definition: ErrorHandlerTest.php:71
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest\handleErrorFetchesDeprecations
‪handleErrorFetchesDeprecations()
Definition: ErrorHandlerTest.php:41
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Error\ErrorHandler
Definition: ErrorHandler.php:37
‪TYPO3\CMS\Core\Tests\Functional\Error\ErrorHandlerTest
Definition: ErrorHandlerTest.php:32