Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: |
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions src/DefaultStackdriverLoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}

Expand Down
4 changes: 1 addition & 3 deletions src/MicroFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion src/StackdriverRequestLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions test/blackbox/EnvironmentConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php
use GuzzleHttp\Psr7\Response;
use Ingenerator\MicroFramework\DefaultStackdriverLoggerProvider;
use Ingenerator\MicroFramework\MicroFramework;
use Ingenerator\MicroFramework\RequestHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

require_once('/var/www/vendor/autoload.php');

(new MicroFramework)->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),
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions test/blackbox/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function provider_request_headers(): array
[],
[
'Host' => ['test_subject'],
'User-Agent' => ['GuzzleHttp/7'],
'User-Agent' => [__CLASS__],
],
],
'with custom headers' => [
Expand All @@ -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'],
],
],
Expand All @@ -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']);
}
Expand Down
8 changes: 4 additions & 4 deletions test/blackbox/StackdriverLoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
Expand All @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions test/blackbox/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
9 changes: 5 additions & 4 deletions test/unit/StackdriverRequestLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']
);
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -140,4 +141,4 @@ private function newSubject(): StackdriverRequestLogger
}


}
}
Loading