From 0a75d6af925a29c4657fe6fc1fbe0ca31e9d0ed6 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 13:12:12 +0000 Subject: [PATCH 1/4] test: Apply Rector improvements to unit tests Tighten assertions, add stricter types, make test classes final and use iterators instead of arrays for data providers. --- rector.php | 3 + test/blackbox/BaseBlackboxTestCase.php | 2 + test/blackbox/BlackboxSmokeTest.php | 2 +- .../blackbox/EnvironmentConfigurationTest.php | 2 +- test/blackbox/ErrorHandlingTest.php | 49 +++++++-------- test/blackbox/HttpHeadersTest.php | 63 +++++++++---------- test/blackbox/HttpResponseCodesTest.php | 2 +- test/blackbox/OutputBufferingTest.php | 2 +- test/blackbox/RequestParsingTest.php | 2 +- test/blackbox/StackdriverLoggingTest.php | 13 ++-- .../DefaultStackdriverLoggerProviderTest.php | 2 +- test/unit/FactoryFunctionTest.php | 4 +- test/unit/StackdriverRequestLoggerTest.php | 33 +++++----- 13 files changed, 92 insertions(+), 87 deletions(-) diff --git a/rector.php b/rector.php index 7907d00..a46c616 100644 --- a/rector.php +++ b/rector.php @@ -16,5 +16,8 @@ codeQuality: true, typeDeclarations: true, typeDeclarationDocblocks: true, + phpunitCodeQuality: true, + phpunitNarrowAsserts: true, + phpunitMockToStub: true, ) ->withImportNames(); diff --git a/test/blackbox/BaseBlackboxTestCase.php b/test/blackbox/BaseBlackboxTestCase.php index 4603d0d..28cd4a9 100644 --- a/test/blackbox/BaseBlackboxTestCase.php +++ b/test/blackbox/BaseBlackboxTestCase.php @@ -1,5 +1,7 @@ > + * @return Iterator> */ - public static function provider_error_handling(): array + public static function provider_error_handling(): Iterator { - return [ - 'exception in handler factory' => [ - <<<'PHP' + yield 'exception in handler factory' => [ + <<<'PHP' fn () => throw new RuntimeException('No handler') PHP, - ], - 'error in handler factory' => [ - <<<'PHP' + ]; + yield 'error in handler factory' => [ + <<<'PHP' fn () => ['no'=> 'thing']['undefined'] PHP, - ], - 'factory does not return a handler instance' => [ - <<<'PHP' + ]; + yield 'factory does not return a handler instance' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class extends stdClass { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -33,14 +33,14 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'factory references undefined class' => [ - <<<'PHP' + ]; + yield 'factory references undefined class' => [ + <<<'PHP' fn (LoggerInterface $logger) => new \My\Undefined\RequestHandlerClass() PHP, - ], - 'uncaught exception during handler' => [ - <<<'PHP' + ]; + yield 'uncaught exception during handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -48,9 +48,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'php error in handler' => [ - <<<'PHP' + ]; + yield 'php error in handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -59,9 +59,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'TypeError in handler' => [ - <<<'PHP' + ]; + yield 'TypeError in handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -73,7 +73,6 @@ private function doThings(string $uhoh): ResponseInterface { } } PHP, - ], ]; } diff --git a/test/blackbox/HttpHeadersTest.php b/test/blackbox/HttpHeadersTest.php index 540b925..1949521 100644 --- a/test/blackbox/HttpHeadersTest.php +++ b/test/blackbox/HttpHeadersTest.php @@ -5,9 +5,10 @@ namespace test\blackbox; use GuzzleHttp\RequestOptions; +use Iterator; use PHPUnit\Framework\Attributes\DataProvider; -class HttpHeadersTest extends BaseBlackboxTestCase +final class HttpHeadersTest extends BaseBlackboxTestCase { private static string $handler_url; @@ -28,42 +29,40 @@ public function handle(ServerRequestInterface $request): ResponseInterface } /** - * @return array, array>}> + * @return Iterator, array>}> */ - public static function provider_headers(): array + public static function provider_headers(): Iterator { - return [ - 'no headers specified, just the defaults' => [ - [], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['text/html; charset=UTF-8'], - ], + yield 'no headers specified, just the defaults' => [ + [], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['text/html; charset=UTF-8'], ], - 'custom content type' => [ - ['Content-Type' => 'application/json'], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - ], + ]; + yield 'custom content type' => [ + ['Content-Type' => 'application/json'], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], ], - 'custom content type in different case' => [ - ['content-type' => 'application/json'], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - ], + ]; + yield 'custom content type in different case' => [ + ['content-type' => 'application/json'], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], ], - 'custom headers' => [ - [ - 'Content-Type' => 'application/json', - 'X-My-Custom' => 'Value', - ], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - 'X-My-Custom' => ['Value'], - ], + ]; + yield 'custom headers' => [ + [ + 'Content-Type' => 'application/json', + 'X-My-Custom' => 'Value', + ], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], + 'X-My-Custom' => ['Value'], ], ]; } diff --git a/test/blackbox/HttpResponseCodesTest.php b/test/blackbox/HttpResponseCodesTest.php index 338b1bd..6f1b24b 100644 --- a/test/blackbox/HttpResponseCodesTest.php +++ b/test/blackbox/HttpResponseCodesTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\Attributes\TestWith; -class HttpResponseCodesTest extends BaseBlackboxTestCase +final class HttpResponseCodesTest extends BaseBlackboxTestCase { private static string $handler_url; diff --git a/test/blackbox/OutputBufferingTest.php b/test/blackbox/OutputBufferingTest.php index 1699d0a..ed10957 100644 --- a/test/blackbox/OutputBufferingTest.php +++ b/test/blackbox/OutputBufferingTest.php @@ -6,7 +6,7 @@ use test\blackbox\BaseBlackboxTestCase; -class OutputBufferingTest extends BaseBlackboxTestCase +final class OutputBufferingTest extends BaseBlackboxTestCase { public function testItHandlesErrorEvenIfOutputBeganBeforeFramework(): void { diff --git a/test/blackbox/RequestParsingTest.php b/test/blackbox/RequestParsingTest.php index 096c952..b409132 100644 --- a/test/blackbox/RequestParsingTest.php +++ b/test/blackbox/RequestParsingTest.php @@ -10,7 +10,7 @@ /** * @phpstan-type TGuzzleRequestOptions array{form_params?: array|scalar|null>, json?: mixed} */ -class RequestParsingTest extends BaseBlackboxTestCase +final class RequestParsingTest extends BaseBlackboxTestCase { private static string $handler_url; diff --git a/test/blackbox/StackdriverLoggingTest.php b/test/blackbox/StackdriverLoggingTest.php index 8dc1c23..0e2c5db 100644 --- a/test/blackbox/StackdriverLoggingTest.php +++ b/test/blackbox/StackdriverLoggingTest.php @@ -1,10 +1,12 @@ assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( @@ -177,8 +178,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface unset($request_log_msg['httpRequest']['latency']); // Currently the test handler is taking around 2.1Mb, just assert it's close to that - $this->assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( @@ -338,8 +338,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface unset($request_log_msg['httpRequest']['latency']); // Currently the test handler is taking around 2.1Mb, just assert it's close to that - $this->assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( diff --git a/test/integration/DefaultStackdriverLoggerProviderTest.php b/test/integration/DefaultStackdriverLoggerProviderTest.php index 50cbc54..3794260 100644 --- a/test/integration/DefaultStackdriverLoggerProviderTest.php +++ b/test/integration/DefaultStackdriverLoggerProviderTest.php @@ -10,7 +10,7 @@ use Ingenerator\PHPUtils\Logging\StackdriverApplicationLogger; use PHPUnit\Framework\TestCase; -class DefaultStackdriverLoggerProviderTest extends TestCase +final class DefaultStackdriverLoggerProviderTest extends TestCase { public function testItIsInitialisableLoggerProvider(): void { diff --git a/test/unit/FactoryFunctionTest.php b/test/unit/FactoryFunctionTest.php index f94499d..5158d88 100644 --- a/test/unit/FactoryFunctionTest.php +++ b/test/unit/FactoryFunctionTest.php @@ -1,5 +1,7 @@ > + * @return Iterator> */ - public static function provider_calculate_request_start(): array + public static function provider_calculate_request_start(): Iterator { - return [ - '64-bit with int hrtime' => [ - 'hrtime_start' => 2993548241208, - 'hrtime_now' => 2993548244583, - 'microtime_now' => 1709456239.781800000, - 'expect_start_time' => 1709456239.781796625, - ], - '32-bit with float hrtime (and longer latency)' => [ - 'hrtime_start' => 2992348244583.0, - 'hrtime_now' => 2993548244583.0, - 'microtime_now' => 1709456239.7818, - 'expect_start_time' => 1709456238.5818, - ], + yield '64-bit with int hrtime' => [ + 'hrtime_start' => 2993548241208, + 'hrtime_now' => 2993548244583, + 'microtime_now' => 1709456239.781800000, + 'expect_start_time' => 1709456239.781796625, + ]; + yield '32-bit with float hrtime (and longer latency)' => [ + 'hrtime_start' => 2992348244583.0, + 'hrtime_now' => 2993548244583.0, + 'microtime_now' => 1709456239.7818, + 'expect_start_time' => 1709456238.5818, ]; } From 1e3c55f512b4e01e5ee8d00f6942ba730e4a7cd6 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 13:14:00 +0000 Subject: [PATCH 2/4] ci: Run Rector dead code rules (no changes) --- rector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rector.php b/rector.php index a46c616..2d29340 100644 --- a/rector.php +++ b/rector.php @@ -13,6 +13,7 @@ ->withComposerBased(phpunit: true) ->withPhpSets() ->withPreparedSets( + deadCode: true, codeQuality: true, typeDeclarations: true, typeDeclarationDocblocks: true, From bdc4eaaa45de31adacfb9801519c5d050b063c26 Mon Sep 17 00:00:00 2001 From: acoulton Date: Wed, 4 Nov 2026 13:15:10 +0000 Subject: [PATCH 3/4] refactor: Run Rector on source files Now that we know phpstan and tests etc are all happy, we can safely run Rector on the source files as well. --- rector.php | 1 + src/DefaultStackdriverLoggerProvider.php | 2 +- src/LoggerProvider.php | 2 ++ src/MicroFramework.php | 3 +-- src/RequestHandler.php | 2 ++ src/RequestLogger.php | 2 ++ src/StackdriverRequestLogger.php | 2 ++ 7 files changed, 11 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index 2d29340..27716b7 100644 --- a/rector.php +++ b/rector.php @@ -6,6 +6,7 @@ return RectorConfig::configure() ->withPaths([ + __DIR__.'/src', __DIR__ . '/test', ]) ->withRootFiles() diff --git a/src/DefaultStackdriverLoggerProvider.php b/src/DefaultStackdriverLoggerProvider.php index 471108c..4a26813 100644 --- a/src/DefaultStackdriverLoggerProvider.php +++ b/src/DefaultStackdriverLoggerProvider.php @@ -20,7 +20,7 @@ public function getLogger(): LoggerInterface { if (!StackdriverApplicationLogger::isInitialised()) { StackdriverApplicationLogger::initialise( - fn () => new StackdriverApplicationLogger( + fn (): StackdriverApplicationLogger => new StackdriverApplicationLogger( $this->log_destination, ...$this->getMetadataProviders(), ) diff --git a/src/LoggerProvider.php b/src/LoggerProvider.php index 03298e5..aeaf938 100644 --- a/src/LoggerProvider.php +++ b/src/LoggerProvider.php @@ -1,5 +1,7 @@ emergency( sprintf( 'Uncaught %s: %s at %s:%s', - get_class($e), + $e::class, $e->getMessage(), $e->getFile(), $e->getFile(), diff --git a/src/RequestHandler.php b/src/RequestHandler.php index fe53d06..dd73b4c 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -1,5 +1,7 @@ Date: Wed, 4 Nov 2026 13:22:11 +0000 Subject: [PATCH 4/4] test: Another change to line-numbers in assertions :sigh: --- test/blackbox/StackdriverLoggingTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/blackbox/StackdriverLoggingTest.php b/test/blackbox/StackdriverLoggingTest.php index 0e2c5db..f207143 100644 --- a/test/blackbox/StackdriverLoggingTest.php +++ b/test/blackbox/StackdriverLoggingTest.php @@ -199,7 +199,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', ], ], @@ -211,21 +211,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(82): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): 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(82): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): MyHandler->handle() #1 /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php(21): Ingenerator\MicroFramework\MicroFramework->execute() #2 {main} TEXT,