diff --git a/tests/http/ErrorHandlerTest.php b/tests/http/ErrorHandlerTest.php index e339a648..0d5795df 100644 --- a/tests/http/ErrorHandlerTest.php +++ b/tests/http/ErrorHandlerTest.php @@ -106,7 +106,7 @@ public function testHandleExceptionWithGenericException(): void ); } - #[TestWith(['apache'])] + #[TestWith(['apache2handler'])] #[TestWith(['cli'])] public function testHandleExceptionWithHttpException(string $sapi): void { diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 2497bcc5..29a5b83c 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -19,6 +19,7 @@ use yii2\extensions\psrbridge\http\{ErrorHandler, Request, Response}; use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider; use yii2\extensions\psrbridge\tests\support\FactoryHelper; +use yii2\extensions\psrbridge\tests\support\stub\HTTPFunctions; use yii2\extensions\psrbridge\tests\TestCase; use function array_fill; @@ -50,6 +51,8 @@ protected function tearDown(): void { $this->closeApplication(); + HTTPFunctions::reset(); + parent::tearDown(); } @@ -724,6 +727,57 @@ public function testRecalculateMemoryLimitAfterResetAndIniChange(): void ini_set('memory_limit', $originalLimit); } + public function testRenderExceptionWithRawFormat(): void + { + HTTPFunctions::set_sapi('apache2handler'); + + $_SERVER = [ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => 'site/trigger-exception', + ]; + + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + + $app = $this->statelessApplication( + [ + 'components' => [ + 'response' => [ + 'format' => Response::FORMAT_RAW, + ], + 'errorHandler' => [ + 'errorAction' => null, + ], + ], + ], + ); + + $response = $app->handle($request); + + self::assertSame( + 500, + $response->getStatusCode(), + "Response 'status code' should be '500' for exception with RAW format.", + ); + + $body = $response->getBody()->getContents(); + + self::assertStringContainsString( + 'yii\base\Exception', + $body, + 'RAW format response should contain exception class name.', + ); + self::assertStringContainsString( + 'Exception error message.', + $body, + 'RAW format response should contain exception message.', + ); + self::assertStringNotContainsString( + '
',
+            $body,
+            'RAW format response should not contain HTML tags.',
+        );
+    }
+
     /**
      * @throws InvalidConfigException if the configuration is invalid or incomplete.
      */