From bfec88ff8d07497ed587f9a72c9308ad7d9ab6d2 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 17:37:27 -0400 Subject: [PATCH 1/9] refactor(ErrorHandler): replace `PHP_SAPI` with `php_sapi_name` for consistency in `SAPI` tests. --- src/http/ErrorHandler.php | 2 +- tests/http/ErrorHandlerTest.php | 14 +++++++++++++- tests/support/MockerExtension.php | 5 +++++ tests/support/stub/HTTPFunctions.php | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 872e70d0..56df7d3d 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (PHP_SAPI !== 'cli') { + if (php_sapi_name() !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { diff --git a/tests/http/ErrorHandlerTest.php b/tests/http/ErrorHandlerTest.php index 683f95dd..c4531805 100644 --- a/tests/http/ErrorHandlerTest.php +++ b/tests/http/ErrorHandlerTest.php @@ -5,11 +5,13 @@ namespace yii2\extensions\psrbridge\tests\http; use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\TestWith; use RuntimeException; use Throwable; use yii\base\{Exception, UserException}; use yii\web\HttpException; use yii2\extensions\psrbridge\http\{ErrorHandler, Response}; +use yii2\extensions\psrbridge\tests\support\stub\HTTPFunctions; use yii2\extensions\psrbridge\tests\TestCase; use function str_repeat; @@ -17,6 +19,12 @@ #[Group('http')] final class ErrorHandlerTest extends TestCase { + protected function tearDown(): void + { + HTTPFunctions::reset(); + + parent::tearDown(); + } public function testHandleExceptionResetsState(): void { $errorHandler = new ErrorHandler(); @@ -97,8 +105,12 @@ public function testHandleExceptionWithGenericException(): void ); } - public function testHandleExceptionWithHttpException(): void + #[TestWith(['apache'])] + #[TestWith(['cli'])] + public function testHandleExceptionWithHttpException(string $sapi): void { + HTTPFunctions::set_sapi($sapi); + $errorHandler = new ErrorHandler(); $errorHandler->discardExistingOutput = false; diff --git a/tests/support/MockerExtension.php b/tests/support/MockerExtension.php index e4c21b8b..9cd9b5b0 100644 --- a/tests/support/MockerExtension.php +++ b/tests/support/MockerExtension.php @@ -73,6 +73,11 @@ public static function load(): void $line, ), ], + [ + 'namespace' => 'yii2\extensions\psrbridge\http', + 'name' => 'php_sapi_name', + 'function' => static fn(): string => HTTPFunctions::php_sapi_name(), + ], [ 'namespace' => 'yii2\extensions\psrbridge\adapter', 'name' => 'stream_get_contents', diff --git a/tests/support/stub/HTTPFunctions.php b/tests/support/stub/HTTPFunctions.php index 85f9c3a8..6854e4d6 100644 --- a/tests/support/stub/HTTPFunctions.php +++ b/tests/support/stub/HTTPFunctions.php @@ -67,6 +67,11 @@ 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. */ @@ -153,6 +158,10 @@ 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 { @@ -162,6 +171,7 @@ public static function reset(): void self::$headersSentFile = ''; self::$headersSentLine = 0; self::$responseCode = 200; + self::$sapi = 'cli'; self::$streamGetContentsShouldFail = false; } @@ -172,6 +182,11 @@ 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; From 5f61bd75e93874bc2e1c00d9310659e4a3dbc39c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 5 Aug 2025 21:38:01 +0000 Subject: [PATCH 2/9] Apply fixes from StyleCI --- src/http/ErrorHandler.php | 2 +- tests/http/ErrorHandlerTest.php | 1 + tests/support/stub/HTTPFunctions.php | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 56df7d3d..872e70d0 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (php_sapi_name() !== 'cli') { + if (PHP_SAPI !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { diff --git a/tests/http/ErrorHandlerTest.php b/tests/http/ErrorHandlerTest.php index c4531805..a84cd312 100644 --- a/tests/http/ErrorHandlerTest.php +++ b/tests/http/ErrorHandlerTest.php @@ -25,6 +25,7 @@ protected function tearDown(): void parent::tearDown(); } + public function testHandleExceptionResetsState(): void { $errorHandler = new ErrorHandler(); diff --git a/tests/support/stub/HTTPFunctions.php b/tests/support/stub/HTTPFunctions.php index 6854e4d6..6e81e758 100644 --- a/tests/support/stub/HTTPFunctions.php +++ b/tests/support/stub/HTTPFunctions.php @@ -158,6 +158,7 @@ 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; From c282a8c04d264339c88b803eb4fedc963bde3b2e Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 17:41:23 -0400 Subject: [PATCH 3/9] refactor(ErrorHandler): replace `PHP_SAPI` with `php_sapi_name` for consistency in `ErrorHandler` class checks. --- .styleci.yml | 1 - src/http/ErrorHandler.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index 6cf29b27..c4fa2a2e 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -23,7 +23,6 @@ enabled: - integer_literal_case - is_null - logical_operators - - magic_constant_casing - magic_method_casing - method_separation - modernize_types_casting diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 872e70d0..56df7d3d 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (PHP_SAPI !== 'cli') { + if (php_sapi_name() !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { From 5b0609fd01f1a340414919a9696df93e8186a44b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 5 Aug 2025 21:41:47 +0000 Subject: [PATCH 4/9] Apply fixes from StyleCI --- src/http/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 56df7d3d..872e70d0 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (php_sapi_name() !== 'cli') { + if (PHP_SAPI !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { From 9ca6dd247bfd1a4f53067cf0629b8a3ac3786d02 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 17:45:09 -0400 Subject: [PATCH 5/9] refactor(ErrorHandler): replace `PHP_SAPI` with `php_sapi_name` for consistency in CLI checks. --- .styleci.yml | 2 +- src/http/ErrorHandler.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index c4fa2a2e..5ed33d26 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -23,7 +23,7 @@ enabled: - integer_literal_case - is_null - logical_operators - - magic_method_casing + - magic_constant_casing - method_separation - modernize_types_casting - native_function_casing diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 872e70d0..56df7d3d 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (PHP_SAPI !== 'cli') { + if (php_sapi_name() !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { From 97e98f8409108a0fe4d0967a0d0f8a457a6cc493 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 5 Aug 2025 21:45:33 +0000 Subject: [PATCH 6/9] Apply fixes from StyleCI --- src/http/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 56df7d3d..872e70d0 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (php_sapi_name() !== 'cli') { + if (PHP_SAPI !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { From f506014bbea130dcd8c8d6583204c2bc0a1289f7 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 17:47:08 -0400 Subject: [PATCH 7/9] refactor(ErrorHandler): replace `PHP_SAPI` with `php_sapi_name` for consistency in CLI checks. --- .styleci.yml | 2 +- src/http/ErrorHandler.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index 5ed33d26..f6dbf717 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -18,12 +18,12 @@ enabled: - declare_strict_types - dir_constant - empty_loop_body_braces - - function_to_constant - hash_to_slash_comment - integer_literal_case - is_null - logical_operators - magic_constant_casing + - magic_method_casing - method_separation - modernize_types_casting - native_function_casing diff --git a/src/http/ErrorHandler.php b/src/http/ErrorHandler.php index 872e70d0..56df7d3d 100644 --- a/src/http/ErrorHandler.php +++ b/src/http/ErrorHandler.php @@ -119,7 +119,7 @@ public function handleException($exception): Response $this->unregister(); - if (PHP_SAPI !== 'cli') { + if (php_sapi_name() !== 'cli') { $statusCode = 500; if ($exception instanceof HttpException) { From 0a0beca8cd348f7558df26785588ba827ea149f4 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 17:54:41 -0400 Subject: [PATCH 8/9] refactor(HTTPFunctions): Enhance documentation for clarity on `SAPI` simulation and retrieval. --- tests/support/stub/HTTPFunctions.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/support/stub/HTTPFunctions.php b/tests/support/stub/HTTPFunctions.php index 6e81e758..4d5f187c 100644 --- a/tests/support/stub/HTTPFunctions.php +++ b/tests/support/stub/HTTPFunctions.php @@ -15,19 +15,20 @@ * Provides controlled replacements for core PHP HTTP header and response functions to facilitate testing of HTTP * emitter and response-related code without actual header output or side effects. * - * This class allows tests to simulate and inspect HTTP header operations, response codes, and output flushing by - * maintaining internal state and exposing methods to manipulate and query that state. + * This class allows tests to simulate and inspect HTTP header operations, response codes, output flushing, + * and SAPI name retrieval by maintaining internal state and exposing methods to manipulate and query that state. * - * It enables validation of emitter logic, header management, and response code handling in isolation from PHP's global - * state. + * It enables validation of emitter logic, header management, response code handling, and SAPI simulation in isolation + * from PHP global state. * * Key features. * - Complete simulation of {@see \header()}, {@see \headers_sent()}, {@see \header_remove()}, {@see \headers_list()}, - * and {@see \http_response_code()} + * {@see \http_response_code()}, and {@see php_sapi_name()} for test reliability. * - Consistent behavior matching PHP's native functions for test reliability. * - File and line tracking for headers_sent simulation. * - Header inspection and manipulation for assertions. * - Simulated output flushing and flush count tracking. + * - SAPI name simulation for emitter and environment testing. * - State reset capability for test isolation and repeatability. * * @copyright Copyright (C) 2025 Terabytesoftw. From 3d7e6442e00e91522f8c49803ef53a445a30659d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Aug 2025 18:07:28 -0400 Subject: [PATCH 9/9] test(ErrorHandlerTest): Add assertion for correct `SAPI` name in HTTP exception handling. --- tests/http/ErrorHandlerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/http/ErrorHandlerTest.php b/tests/http/ErrorHandlerTest.php index a84cd312..e339a648 100644 --- a/tests/http/ErrorHandlerTest.php +++ b/tests/http/ErrorHandlerTest.php @@ -129,6 +129,11 @@ public function testHandleExceptionWithHttpException(string $sapi): void $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