‪TYPO3CMS  11.5
ProductionExceptionHandlerTest.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 Prophecy\PhpUnit\ProphecyTrait;
21 use Psr\Log\NullLogger;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪ProductionExceptionHandlerTest extends UnitTestCase
35 {
36  use ProphecyTrait;
37 
38  protected ‪$resetSingletonInstances = true;
39 
41 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $this->subject = new ‪ProductionExceptionHandler(new ‪Context(), new ‪Random(), new NullLogger());
49  }
50 
54  public function ‪relayPropagateResponseException(): void
55  {
56  $response = $this->getMockBuilder(HtmlResponse::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  $exception = new ‪PropagateResponseException($response, 1607328584);
60 
61  $this->expectException(PropagateResponseException::class);
62  $this->subject->handle($exception);
63  }
64 
68  public function ‪relayImmediateResponseException(): void
69  {
70  $response = $this->getMockBuilder(HtmlResponse::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $exception = new ImmediateResponseException($response, 1533939251);
74 
75  $this->expectException(ImmediateResponseException::class);
76  $this->subject->handle($exception);
77  }
78 
82  public function ‪handleReturnsMessageWithResolvedErrorCode(): void
83  {
84  $currentTimestamp = 1629993829;
85  $random = '029cca07';
86 
87  $randomProphecy = $this->prophesize(Random::class);
88  $randomProphecy->generateRandomHexString(8)->willReturn($random);
89 
90  $exceptionHandler = new ProductionExceptionHandler(
91  new ‪Context(['date' => new DateTimeAspect(new \DateTimeImmutable('@' . $currentTimestamp))]),
92  $randomProphecy->reveal(),
93  new NullLogger()
94  );
95 
96  self::assertEquals(
97  'Oops, an error occurred! Code: ' . date('YmdHis', $currentTimestamp) . $random,
98  $exceptionHandler->handle(new \‪Exception('Some exception', 1629996089))
99  );
100  }
101 
106  {
107  $currentTimestamp = 1629993829;
108  $random = '029cca07';
109 
110  $randomProphecy = $this->prophesize(Random::class);
111  $randomProphecy->generateRandomHexString(8)->willReturn($random);
112 
113  $exceptionHandler = new ProductionExceptionHandler(
114  new ‪Context(['date' => new DateTimeAspect(new \DateTimeImmutable('@' . $currentTimestamp))]),
115  $randomProphecy->reveal(),
116  new NullLogger()
117  );
118  $exceptionHandler->setConfiguration([
119  'errorMessage' => 'Custom error message: {code}',
120  ]);
121 
122  self::assertEquals(
123  'Custom error message: ' . date('YmdHis', $currentTimestamp) . $random,
124  $exceptionHandler->handle(new \‪Exception('Some exception', 1629996090))
125  );
126  }
127 
132  {
133  $currentTimestamp = 1629993829;
134  $random = '029cca07';
135 
136  $randomProphecy = $this->prophesize(Random::class);
137  $randomProphecy->generateRandomHexString(8)->willReturn($random);
138 
139  $exceptionHandler = new ProductionExceptionHandler(
140  new ‪Context(['date' => new DateTimeAspect(new \DateTimeImmutable('@' . $currentTimestamp))]),
141  $randomProphecy->reveal(),
142  new NullLogger()
143  );
144  $exceptionHandler->setConfiguration([
145  'errorMessage' => 'Custom error message: %s',
146  ]);
147 
148  self::assertEquals(
149  'Custom error message: ' . date('YmdHis', $currentTimestamp) . $random,
150  $exceptionHandler->handle(new \‪Exception('Some exception', 1629996091))
151  );
152  }
153 }
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: ProductionExceptionHandlerTest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\relayPropagateResponseException
‪relayPropagateResponseException()
Definition: ProductionExceptionHandlerTest.php:53
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest
Definition: ProductionExceptionHandlerTest.php:35
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\handleReturnsCustomErrorMessageWithResolvedErrorCodeForLegacyPlaceholder
‪handleReturnsCustomErrorMessageWithResolvedErrorCodeForLegacyPlaceholder()
Definition: ProductionExceptionHandlerTest.php:130
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\setUp
‪setUp()
Definition: ProductionExceptionHandlerTest.php:44
‪TYPO3\CMS\Frontend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\$subject
‪ProductionExceptionHandler $subject
Definition: ProductionExceptionHandlerTest.php:39
‪TYPO3\CMS\Core\Http\PropagateResponseException
Definition: PropagateResponseException.php:47
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\relayImmediateResponseException
‪relayImmediateResponseException()
Definition: ProductionExceptionHandlerTest.php:67
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:24
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception
Definition: ProductionExceptionHandlerTest.php:18
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\handleReturnsCustomErrorMessageWithResolvedErrorCode
‪handleReturnsCustomErrorMessageWithResolvedErrorCode()
Definition: ProductionExceptionHandlerTest.php:104
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:32
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Exception\ProductionExceptionHandlerTest\handleReturnsMessageWithResolvedErrorCode
‪handleReturnsMessageWithResolvedErrorCode()
Definition: ProductionExceptionHandlerTest.php:81