diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5f6f151..d6a3f6f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,12 +9,17 @@ 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: + guzzle-psr-version: ['^2', '^3'] + steps: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.4 + php-version: 8.5 tools: composer:v2 - name: Checkout @@ -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..7b190db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ ### Unreleased +* Require PHP 8.5 +* Require ingenerator/php-utils version 3 +* 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..bed8501 100644 --- a/composer.json +++ b/composer.json @@ -4,13 +4,13 @@ "homepage": "https://github.com/ingenerator/microframework", "type": "library", "require": { - "php": "~8.4.0", - "ingenerator/php-utils": "^2.0", - "guzzlehttp/psr7": "^2.7" + "php": "~8.5.0", + "ingenerator/php-utils": "^3.0", + "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/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/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/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/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/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/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']); } 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, diff --git a/test/blackbox/docker-compose.yaml b/test/blackbox/docker-compose.yaml index c111555..1365bd5 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 @@ -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 - FROM php:8.4-apache + # 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 \ 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 +}