From 00aecc65b46644c7a0c5fca2873162cd751dd389 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 08:58:48 +0000 Subject: [PATCH 1/6] feat: Support guzzzlehttp/psr7 version 3.x Optionally support either 2.x or 3.x. Note that one of the tests was coupled to Guzzle's default user-agent header, which we don't control. This now uses a hardcoded string in the test instead. --- .github/workflows/ci.yaml | 7 ++++++- CHANGELOG.md | 2 ++ composer.json | 4 ++-- test/blackbox/RequestParsingTest.php | 9 ++++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5f6f151..8accdb5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,6 +10,11 @@ jobs: run-tests: runs-on: ubuntu-latest name: Run tests + strategy: + fail-fast: false + matrix: + guzzle-psr-version: ['^2', '^3'] + steps: - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -23,7 +28,7 @@ jobs: show-progress: false - name: Composer install - uses: ingenerator/github-composite-actions/cached-composer-install@v1 + run: composer update --with "guzzlehttp/psr7:${{ matrix.guzzle-psr-version }}" - name: Run unit tests run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index 886809c..a53c6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ### Unreleased +* Support Guzzle PSR-7 3.x (as well as 2.x) + ### v1.2.0 (2025-07-23) * Require PHP 8.4 diff --git a/composer.json b/composer.json index eeb8376..5bbd5d1 100644 --- a/composer.json +++ b/composer.json @@ -6,11 +6,11 @@ "require": { "php": "~8.4.0", "ingenerator/php-utils": "^2.0", - "guzzlehttp/psr7": "^2.7" + "guzzlehttp/psr7": "^2.7 | ^3.2" }, "require-dev": { "fig/log-test": "^1.1", - "guzzlehttp/guzzle": "^7.9.1", + "guzzlehttp/guzzle": "^7.9.1 | ^8.3", "phpunit/phpunit": "^10.5.28", "symfony/filesystem": "^7.1.2" }, diff --git a/test/blackbox/RequestParsingTest.php b/test/blackbox/RequestParsingTest.php index d51411d..02486c3 100644 --- a/test/blackbox/RequestParsingTest.php +++ b/test/blackbox/RequestParsingTest.php @@ -127,7 +127,7 @@ public static function provider_request_headers(): array [], [ 'Host' => ['test_subject'], - 'User-Agent' => ['GuzzleHttp/7'], + 'User-Agent' => [__CLASS__], ], ], 'with custom headers' => [ @@ -147,7 +147,7 @@ public static function provider_request_headers(): array ], [ 'Host' => ['test_subject'], - 'User-Agent' => ['GuzzleHttp/7'], + 'User-Agent' => [__CLASS__], 'X-My-Header' => ['test1, test2'], ], ], @@ -159,7 +159,10 @@ public static function provider_request_headers(): array */ public function test_it_can_receive_incoming_headers($send_headers, $expect): void { - $response = $this->guzzle->get(self::$handler_url, ['headers' => $send_headers]); + $response = $this->guzzle->get(self::$handler_url, ['headers' => [ + 'User-Agent' => __CLASS__, + ...$send_headers + ]]); $received_payload = JSON::decodeArray($response->getBody()->getContents()); $this->assertSame($expect, $received_payload['headers']); } From 1087ac95126498d0d9910cfa0232547e65f6dfe5 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 09:10:37 +0000 Subject: [PATCH 2/6] feat: Upgrade to ingenerator/php-utils v3 For stricter typing and new PHP compatibility. --- CHANGELOG.md | 1 + composer.json | 2 +- src/DefaultStackdriverLoggerProvider.php | 4 ++-- src/StackdriverRequestLogger.php | 2 +- test/unit/StackdriverRequestLoggerTest.php | 9 +++++---- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a53c6dd..0afb7a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### Unreleased +* Require ingenerator/php-utils version 3 * Support Guzzle PSR-7 3.x (as well as 2.x) ### v1.2.0 (2025-07-23) diff --git a/composer.json b/composer.json index 5bbd5d1..6b7edf2 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "type": "library", "require": { "php": "~8.4.0", - "ingenerator/php-utils": "^2.0", + "ingenerator/php-utils": "^3.0", "guzzlehttp/psr7": "^2.7 | ^3.2" }, "require-dev": { diff --git a/src/DefaultStackdriverLoggerProvider.php b/src/DefaultStackdriverLoggerProvider.php index 274fcd3..8f5a840 100644 --- a/src/DefaultStackdriverLoggerProvider.php +++ b/src/DefaultStackdriverLoggerProvider.php @@ -35,8 +35,8 @@ protected function getMetadataProviders(): array { return [ DefaultLogMetadata::serviceContext($this->service_name, $this->version_file_path), - DefaultLogMetadata::requestTrace($_SERVER), - DefaultLogMetadata::httpContext($_SERVER), + DefaultLogMetadata::requestTrace(), + DefaultLogMetadata::httpContext(), ]; } diff --git a/src/StackdriverRequestLogger.php b/src/StackdriverRequestLogger.php index 9fea9f9..40e610a 100644 --- a/src/StackdriverRequestLogger.php +++ b/src/StackdriverRequestLogger.php @@ -34,7 +34,7 @@ public function logRequest(LoggerInterface $logger, float|int $start_hr_time): v $elapsed_nanos = $this->getHrTime() - $start_hr_time; $request_start_time = $this->getMicrotime() - ($elapsed_nanos / 1_000_000_000); - $logger->logRequest($_SERVER, $request_start_time); + $logger->logRequest(request_start_time: $request_start_time); } protected function getHrTime(): int|float diff --git a/test/unit/StackdriverRequestLoggerTest.php b/test/unit/StackdriverRequestLoggerTest.php index bd55c35..03b2dcf 100644 --- a/test/unit/StackdriverRequestLoggerTest.php +++ b/test/unit/StackdriverRequestLoggerTest.php @@ -5,6 +5,7 @@ use Ingenerator\MicroFramework\RequestLogger; use Ingenerator\MicroFramework\StackdriverRequestLogger; use Ingenerator\PHPUtils\Logging\StackdriverApplicationLogger; +use Ingenerator\PHPUtils\Superglobals\ServerGlobal; use PHPUnit\Framework\TestCase; use Psr\Log\Test\TestLogger; @@ -56,8 +57,8 @@ public function test_it_calculates_request_start_time_from_elapsed_high_res_time $subject->logRequest($logger, $hrtime_start); $this->assertSame( - ['server' => $_SERVER, 'request_start_time' => $expect_start_time], - $logger->request_log_calls + $expect_start_time, + $logger->request_log_calls['request_start_time'] ); } @@ -127,7 +128,7 @@ public function makeLoggerSpy(): StackdriverApplicationLogger public function __construct() { } - public function logRequest(?array $server, ?float $request_start_time = null): void + public function logRequest(ServerGlobal $server = new ServerGlobal(), ?float $request_start_time = null): void { $this->request_log_calls = get_defined_vars(); } @@ -140,4 +141,4 @@ private function newSubject(): StackdriverRequestLogger } -} \ No newline at end of file +} From 2bbda6842ab39bebbd34b1455eca7b5d3d152bc8 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 09:14:33 +0000 Subject: [PATCH 3/6] ci: Customise matrix job names for clarity --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8accdb5..ed3d2b8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ on: jobs: run-tests: runs-on: ubuntu-latest - name: Run tests + name: Run tests (guzzle/psr7 ${{matrix.guzzle-psr-version}}) strategy: fail-fast: false matrix: From e65049d3ed44816234dbeede9dd41d8551514be3 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 09:18:03 +0000 Subject: [PATCH 4/6] feat: Require PHP 8.5 --- .github/workflows/ci.yaml | 2 +- CHANGELOG.md | 1 + composer.json | 2 +- test/blackbox/README.md | 2 +- test/blackbox/docker-compose.yaml | 4 ++-- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ed3d2b8..d6a3f6f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,7 +19,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.4 + php-version: 8.5 tools: composer:v2 - name: Checkout diff --git a/CHANGELOG.md b/CHANGELOG.md index 0afb7a2..7b190db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### Unreleased +* Require PHP 8.5 * Require ingenerator/php-utils version 3 * Support Guzzle PSR-7 3.x (as well as 2.x) diff --git a/composer.json b/composer.json index 6b7edf2..bed8501 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/ingenerator/microframework", "type": "library", "require": { - "php": "~8.4.0", + "php": "~8.5.0", "ingenerator/php-utils": "^3.0", "guzzlehttp/psr7": "^2.7 | ^3.2" }, diff --git a/test/blackbox/README.md b/test/blackbox/README.md index 6ade738..275d706 100644 --- a/test/blackbox/README.md +++ b/test/blackbox/README.md @@ -12,7 +12,7 @@ The tests are run against the **built version of the package**. To do this, we: matching the download archive that github will produce for that commit - including that it respects `export-ignore` and similar instructions in the .gitattributes. * Use composer to install that packaged tar, together with its dependencies, into a docker filesystem -* Then copies the entire built project into a php:8.4-apache image which runs as the test_subject. +* Then copies the entire built project into a php:8.5-apache image which runs as the test_subject. Note that this means when working locally, changes to the application code **will not be picked up by the test_subject automatically**. To update the test_subject you will need to **commit your changes** (so that they are included in the diff --git a/test/blackbox/docker-compose.yaml b/test/blackbox/docker-compose.yaml index c111555..9d6148b 100644 --- a/test/blackbox/docker-compose.yaml +++ b/test/blackbox/docker-compose.yaml @@ -3,7 +3,7 @@ services: # The test_runner is the outside of the black box, it has a mounted copy of the full # codebase for running tests etc. test_runner: - image: php:8.4-apache + image: php:8.5-apache environment: - TEST_SUBJECT_BASE_URI=http://test_subject working_dir: /workspace @@ -30,7 +30,7 @@ services: # Then build the packaged library into an HTTP server with our expected config and environment # Note that we need to include composer to ensure that it will - FROM php:8.4-apache + FROM php:8.5-apache WORKDIR /var/www COPY --from=composer-builder /workspace/test/blackbox/implementation /var/www COPY --from=composer:2 /usr/bin/composer /usr/bin/composer From e3d97c5242e14a5cb3db79a03f5605306c92c447 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 09:26:56 +0000 Subject: [PATCH 5/6] fix: Setting 500 status code when handling error after send begins If the response has begun to be sent before the exception (for example, when checking for multiple response header values) then the `HTTP/{proto} {status} {reason}` header might already have been queued. Calling http_response_code after this has no effect, and causes a warning on >= PHP 8.5. Since we are already setting a Content-Type header in this handler, the simplest solution is to set the response_code in that same call, which will update the HTTP header string that's already been set. Note that this change alters the line numbers in the main microframework file, which breaks the (probably too brittle) test for the log output as the stack trace line numbers have changed. --- src/MicroFramework.php | 4 +--- test/blackbox/StackdriverLoggingTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/MicroFramework.php b/src/MicroFramework.php index e5b1f1b..7d0b633 100644 --- a/src/MicroFramework.php +++ b/src/MicroFramework.php @@ -16,7 +16,6 @@ use function get_class; use function header; use function headers_sent; -use function http_response_code; use function ob_get_clean; use function ob_get_level; use function set_error_handler; @@ -166,8 +165,7 @@ private function renderErrorResponse(): void { if ( ! headers_sent()) { // We can't send these if they've already been sent... - http_response_code(500); - header('Content-Type: text/plain'); + header('Content-Type: text/plain', response_code: 500); } echo "Unexpected fatal error\n"; } diff --git a/test/blackbox/StackdriverLoggingTest.php b/test/blackbox/StackdriverLoggingTest.php index 8a47cec..366dfde 100644 --- a/test/blackbox/StackdriverLoggingTest.php +++ b/test/blackbox/StackdriverLoggingTest.php @@ -186,7 +186,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface ], 'reportLocation' => [ 'filePath' => '/var/www/vendor/ingenerator/microframework/src/MicroFramework.php', - 'lineNumber' => 153, + 'lineNumber' => 152, 'functionName' => 'Ingenerator\MicroFramework\MicroFramework->logUncaughtException', ], ], @@ -198,21 +198,21 @@ public function handle(ServerRequestInterface $request): ResponseInterface 'file' => '/var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php', 'line' => 17, 'trace' => <<<'TEXT' - #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(80): MyHandler->handle() #1 /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php(21): Ingenerator\MicroFramework\MicroFramework->execute() #2 {main} TEXT, ], 'logging.googleapis.com/sourceLocation' => [ 'file' => '/var/www/vendor/ingenerator/microframework/src/MicroFramework.php', - 'line' => 153, + 'line' => 152, 'function' => 'Ingenerator\MicroFramework\MicroFramework->logUncaughtException', ], '@type' => 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent', 'stack_trace' => <<<'TEXT' PHP Warning: RuntimeException: I broke in /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php:17 Stack trace: - #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(80): MyHandler->handle() #1 /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php(21): Ingenerator\MicroFramework\MicroFramework->execute() #2 {main} TEXT, From c64986b808b7a25dae73a920d1b805a79fe47994 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 10:07:47 +0000 Subject: [PATCH 6/6] test: Prove apps can configure a locale Previously this was not tested, because the base image we're using for tests does not include locales other than the default `C` (POSIX) locales, and therefore `setlocale` would not apply any custom locale. --- .../blackbox/EnvironmentConfigurationTest.php | 34 +++++++++++++++++-- test/blackbox/docker-compose.yaml | 8 ++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/test/blackbox/EnvironmentConfigurationTest.php b/test/blackbox/EnvironmentConfigurationTest.php index 6eb21c3..fb635db 100644 --- a/test/blackbox/EnvironmentConfigurationTest.php +++ b/test/blackbox/EnvironmentConfigurationTest.php @@ -44,8 +44,38 @@ public function handle(ServerRequestInterface $request): ResponseInterface { public function test_can_configure_locale_in_bootstrap() { - $this->markTestIncomplete( - 'Seems to be hard to get the value you passed into setlocale back out again for assertion...', + $handler_url = $this->provisionDynamicHandler( + <<<'PHP' + execute( + logger_provider_factory: fn () => new DefaultStackdriverLoggerProvider( + service_name: 'ig-micro', + version_file_path: '/var/www/version.php', + ), + locale: 'fr_FR.utf8', + handler_factory: fn () => new class implements RequestHandler { + public function handle(ServerRequestInterface $request): ResponseInterface { + return new Response(200, body: setlocale(LC_ALL, 0)); + } + }, + ); + PHP, + ); + + $this->assertResponseMatches( + 200, + 'fr_FR.utf8', + $this->guzzle->get($handler_url), ); } diff --git a/test/blackbox/docker-compose.yaml b/test/blackbox/docker-compose.yaml index 9d6148b..1365bd5 100644 --- a/test/blackbox/docker-compose.yaml +++ b/test/blackbox/docker-compose.yaml @@ -29,12 +29,18 @@ services: RUN /workspace/test/blackbox/package-for-blackbox-tests.sh # Then build the packaged library into an HTTP server with our expected config and environment - # Note that we need to include composer to ensure that it will + # Note that we need to include composer to ensure that it will be able to run check-platform reqs + # And we need to install a locale to be able to test setting locales FROM php:8.5-apache WORKDIR /var/www COPY --from=composer-builder /workspace/test/blackbox/implementation /var/www COPY --from=composer:2 /usr/bin/composer /usr/bin/composer RUN composer check-platform-reqs \ + && apt update \ + && apt install -y locales \ + && echo "fr_FR.UTF-8 UTF-8" > /etc/locale.gen \ + && locale-gen \ + && apt clean \ && a2enmod \ expires \ headers \