diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 268fb38c..2ec0395a 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -10,6 +10,7 @@ use stdClass; use Yii; use yii\base\{InvalidConfigException, Security}; +use yii\base\UserException; use yii\di\NotInstantiableException; use yii\helpers\Json; use yii\i18n\{Formatter, I18N}; @@ -31,6 +32,7 @@ use function gc_status; use function ini_get; use function ini_set; +use function is_string; use function memory_get_usage; use function ob_get_level; use function ob_start; @@ -844,6 +846,53 @@ public function testReturnFalseFromCleanWhenMemoryUsageIsBelowThreshold(): void ini_set('memory_limit', $originalLimit); } + public function testReturnHtmlErrorResponseForUserException(): void + { + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + + $app = $this->statelessApplication( + [ + 'components' => [ + 'errorHandler' => [ + 'errorAction' => 'stub/error', + ], + ], + ], + ); + + $app->handle($request); + + $errorHandler = $app->errorHandler; + $errorHandler->discardExistingOutput = false; + + $exception = new UserException('User-friendly error message.'); + + $response = $errorHandler->handleException($exception); + $data = is_string($response->data) ? $response->data : ''; + + self::assertSame( + 200, + $response->getStatusCode(), + "Response 'status code' should be '200' when handling a 'UserException' with a configured 'errorAction' " . + "that executes successfully in 'ErrorHandler', confirming proper error action delegation.", + ); + self::assertSame( + Response::FORMAT_HTML, + $response->format, + "Response 'format' should be 'html' for 'UserException' handled by 'ErrorHandler'.", + ); + self::assertNotEmpty( + $data, + "Response 'data' should not be empty when 'ErrorHandler' handles a 'UserException', ensuring error " . + 'details are present.', + ); + self::assertStringContainsString( + 'User-friendly error message.', + $data, + "Response 'data' should contain the exception message when 'ErrorHandler' handles a 'UserException'.", + ); + } + /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */