From ad8cc861f60d728424af4aaa0b365e433de4769d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 16:58:00 -0400 Subject: [PATCH 1/2] test(StatelessApplicationTest): Add test for handling `UserException` in `ErrorHandler` class. --- tests/http/StatelessApplicationTest.php | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 268fb38c..72bbf7df 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 '500' when handling a 'UserException' in 'ErrorHandler', confirming " . + 'proper error handling.', + ); + 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. */ From d8eadc7a41a38ce185fd2a7d5733a3a0d55d8c63 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 17:12:17 -0400 Subject: [PATCH 2/2] test(StatelessApplicationTest): Update assertion message for `UserException` handling in `ErrorHandler` class. --- tests/http/StatelessApplicationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 72bbf7df..2ec0395a 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -873,8 +873,8 @@ public function testReturnHtmlErrorResponseForUserException(): void self::assertSame( 200, $response->getStatusCode(), - "Response 'status code' should be '500' when handling a 'UserException' in 'ErrorHandler', confirming " . - 'proper error handling.', + "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,