diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 56df7d3d..26f25e30 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -6,15 +6,12 @@ use Throwable; use Yii; -use yii\base\{InvalidRouteException, UserException}; -use yii\console\Exception; +use yii\base\{Exception, InvalidRouteException, UserException}; use yii\helpers\VarDumper; -use yii\web\HttpException; use function array_diff_key; use function array_flip; use function htmlspecialchars; -use function http_response_code; use function ini_set; /** @@ -119,16 +116,6 @@ public function handleException($exception): Response $this->unregister(); - if (php_sapi_name() !== 'cli') { - $statusCode = 500; - - if ($exception instanceof HttpException) { - $statusCode = $exception->statusCode; - } - - http_response_code($statusCode); - } - try { $this->logException($exception); @@ -175,7 +162,7 @@ public function setResponse(Response $response): void */ protected function handleFallbackExceptionMessage($exception, $previousException): Response { - $response = $this->createErrorResponse(); + $response = $this->createErrorResponse()->setStatusCode(500); $msg = "An Error occurred while handling another error:\n"; $msg .= $exception; @@ -185,7 +172,10 @@ protected function handleFallbackExceptionMessage($exception, $previousException $response->data = 'An internal server error occurred.'; if (YII_DEBUG) { - $response->data = '
' . htmlspecialchars($msg, ENT_QUOTES, Yii::$app->charset) . '
'; + $message = htmlspecialchars($msg, ENT_QUOTES, Yii::$app->charset); + + $response->data = "
{$message}
"; + $safeServerVars = array_diff_key( $_SERVER, array_flip( diff --git a/tests/http/ErrorHandlerTest.php b/tests/http/ErrorHandlerTest.php index 8a351a64..6659b4a0 100644 --- a/tests/http/ErrorHandlerTest.php +++ b/tests/http/ErrorHandlerTest.php @@ -4,7 +4,7 @@ namespace yii2\extensions\psrbridge\tests\http; -use PHPUnit\Framework\Attributes\{Group, RequiresPhpExtension, TestWith}; +use PHPUnit\Framework\Attributes\{Group, RequiresPhpExtension}; use RuntimeException; use Throwable; use yii\base\{Exception, UserException}; @@ -148,36 +148,6 @@ public function testHandleExceptionWithGenericException(): void ); } - #[TestWith(['apache2handler'])] - #[TestWith(['cli'])] - public function testHandleExceptionWithHttpException(string $sapi): void - { - HTTPFunctions::set_sapi($sapi); - - $errorHandler = new ErrorHandler(); - - $errorHandler->discardExistingOutput = false; - - $exception = new HttpException(404, 'Page not found'); - - $response = $errorHandler->handleException($exception); - - self::assertSame( - 404, - $response->getStatusCode(), - "Should preserve HTTP status code from 'HttpException'.", - ); - self::assertNotEmpty( - $response->data, - 'Should set response data for HTTP exception.', - ); - self::assertSame( - $sapi, - HTTPFunctions::php_sapi_name(), - "Should return correct SAPI name '{$sapi}' for 'HttpException'.", - ); - } - public function testHandleExceptionWithLongMessage(): void { $errorHandler = new ErrorHandler(); diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 25262fbc..4fcf5d99 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -10,7 +10,8 @@ use Psr\Http\Message\{ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface}; use stdClass; use Yii; -use yii\base\{Exception, InvalidConfigException, Security}; +use yii\base\Exception; +use yii\base\{InvalidConfigException, Security}; use yii\di\NotInstantiableException; use yii\helpers\Json; use yii\i18n\{Formatter, I18N}; @@ -741,8 +742,6 @@ public function testRenderExceptionSetsDisplayErrorsInDebugMode(): void $initialBufferLevel = ob_get_level(); - HTTPFunctions::set_sapi('apache2handler'); - $_SERVER = [ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => 'site/trigger-exception', @@ -795,8 +794,6 @@ public function testRenderExceptionWithErrorActionReturningResponseObject(): voi { @runkit_constant_redefine('YII_DEBUG', false); - HTTPFunctions::set_sapi('apache2handler'); - $_SERVER = [ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => 'site/trigger-exception', @@ -844,8 +841,6 @@ public function testRenderExceptionWithErrorActionReturningResponseObject(): voi */ public function testRenderExceptionWithRawFormat(): void { - HTTPFunctions::set_sapi('apache2handler'); - $_SERVER = [ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => 'site/trigger-exception', @@ -889,7 +884,12 @@ public function testRenderExceptionWithRawFormat(): void self::assertStringNotContainsString( '
',
             $body,
-            'RAW format response should not contain HTML tags.',
+            "RAW format response should not contain HTML tag '
'.",
+        );
+        self::assertStringNotContainsString(
+            '
', + $body, + "RAW format response should not contain HTML tag '
'.", ); } @@ -1086,9 +1086,9 @@ public function testReturnHtmlErrorResponseWhenErrorHandlerActionIsInvalid(): vo $response = $app->handle($request); self::assertSame( - 200, + 500, $response->getStatusCode(), - "Response 'status code' should be '200' when 'ErrorHandler' is misconfigured and a nonexistent action is " . + "Response 'status code' should be '500' when 'ErrorHandler' is misconfigured and a nonexistent action is " . "requested in 'StatelessApplication'.", ); self::assertSame( diff --git a/tests/support/MockerExtension.php b/tests/support/MockerExtension.php index 9cd9b5b0..087d8dc2 100644 --- a/tests/support/MockerExtension.php +++ b/tests/support/MockerExtension.php @@ -75,8 +75,10 @@ public static function load(): void ], [ 'namespace' => 'yii2\extensions\psrbridge\http', - 'name' => 'php_sapi_name', - 'function' => static fn(): string => HTTPFunctions::php_sapi_name(), + 'name' => 'http_response_code', + 'function' => static fn(int|null $response_code = null): int => HTTPFunctions::http_response_code( + $response_code, + ), ], [ 'namespace' => 'yii2\extensions\psrbridge\adapter', diff --git a/tests/support/stub/HTTPFunctions.php b/tests/support/stub/HTTPFunctions.php index 4d5f187c..9d44e303 100644 --- a/tests/support/stub/HTTPFunctions.php +++ b/tests/support/stub/HTTPFunctions.php @@ -68,11 +68,6 @@ final class HTTPFunctions */ private static int $responseCode = 200; - /** - * Tracks the current SAPI name for simulation. - */ - private static string $sapi = 'cli'; - /** * Controls whether stream_get_contents should fail. */ @@ -160,11 +155,6 @@ public static function http_response_code(int|null $response_code = 0): int return self::$responseCode; } - public static function php_sapi_name(): string - { - return self::$sapi; - } - public static function reset(): void { self::$flushedTimes = 0; @@ -173,7 +163,6 @@ public static function reset(): void self::$headersSentFile = ''; self::$headersSentLine = 0; self::$responseCode = 200; - self::$sapi = 'cli'; self::$streamGetContentsShouldFail = false; } @@ -184,11 +173,6 @@ public static function set_headers_sent(bool $value = false, string $file = '', self::$headersSentLine = $line; } - public static function set_sapi(string $sapi): void - { - self::$sapi = $sapi; - } - public static function set_stream_get_contents_should_fail(bool $shouldFail = true): void { self::$streamGetContentsShouldFail = $shouldFail;