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
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

return RectorConfig::configure()
->withPaths([
__DIR__.'/src',
__DIR__ . '/test',
])
->withRootFiles()
->withSkipPath(__DIR__.'/test/blackbox/implementation/htdocs/dynamic')
->withComposerBased(phpunit: true)
->withPhpSets()
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
typeDeclarationDocblocks: true,
phpunitCodeQuality: true,
phpunitNarrowAsserts: true,
phpunitMockToStub: true,
)
->withImportNames();
2 changes: 1 addition & 1 deletion src/DefaultStackdriverLoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
2 changes: 2 additions & 0 deletions src/LoggerProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ingenerator\MicroFramework;

use Psr\Log\LoggerInterface;
Expand Down
3 changes: 1 addition & 2 deletions src/MicroFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use function date_default_timezone_set;
use function error_reporting;
use function get_class;
use function header;
use function headers_sent;
use function ob_get_clean;
Expand Down Expand Up @@ -153,7 +152,7 @@ private function logUncaughtException(LoggerInterface $logger, Throwable $e): vo
$logger->emergency(
sprintf(
'Uncaught %s: %s at %s:%s',
get_class($e),
$e::class,
$e->getMessage(),
$e->getFile(),
$e->getFile(),
Expand Down
2 changes: 2 additions & 0 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ingenerator\MicroFramework;

use Psr\Http\Message\ResponseInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/RequestLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ingenerator\MicroFramework;

use Psr\Log\LoggerInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/StackdriverRequestLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ingenerator\MicroFramework;

use Ingenerator\PHPUtils\Logging\StackdriverApplicationLogger;
Expand Down
2 changes: 2 additions & 0 deletions test/blackbox/BaseBlackboxTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

use GuzzleHttp\Client;
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/BlackboxSmokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace test\blackbox;

class BlackboxSmokeTest extends BaseBlackboxTestCase
final class BlackboxSmokeTest extends BaseBlackboxTestCase
{
public function testTestSubjectRespondsFromDefaultHandler(): void
{
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/EnvironmentConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace test\blackbox;

class EnvironmentConfigurationTest extends BaseBlackboxTestCase
final class EnvironmentConfigurationTest extends BaseBlackboxTestCase
{
public function testCanConfigureCustomTimezoneInBootstrap(): void
{
Expand Down
49 changes: 24 additions & 25 deletions test/blackbox/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,53 @@

namespace test\blackbox;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;

class ErrorHandlingTest extends BaseBlackboxTestCase
final class ErrorHandlingTest extends BaseBlackboxTestCase
{
/**
* @return array<string, list<string>>
* @return Iterator<string, list<string>>
*/
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
{
return new Response(200, body: 'Should not get here, it is the wrong instance type');
}
}
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
{
throw new \RuntimeException('You broke it');
}
}
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
{
Expand All @@ -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
{
Expand All @@ -73,7 +73,6 @@ private function doThings(string $uhoh): ResponseInterface {
}
}
PHP,
],
];
}

Expand Down
63 changes: 31 additions & 32 deletions test/blackbox/HttpHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,42 +29,40 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

/**
* @return array<string, array{array<string,string>, array<string,list<string>>}>
* @return Iterator<string, array{array<string, string>, array<string, list<string>>}>
*/
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'],
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/HttpResponseCodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\Attributes\TestWith;

class HttpResponseCodesTest extends BaseBlackboxTestCase
final class HttpResponseCodesTest extends BaseBlackboxTestCase
{
private static string $handler_url;

Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/OutputBufferingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use test\blackbox\BaseBlackboxTestCase;

class OutputBufferingTest extends BaseBlackboxTestCase
final class OutputBufferingTest extends BaseBlackboxTestCase
{
public function testItHandlesErrorEvenIfOutputBeganBeforeFramework(): void
{
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @phpstan-type TGuzzleRequestOptions array{form_params?: array<string, array<int|string>|scalar|null>, json?: mixed}
*/
class RequestParsingTest extends BaseBlackboxTestCase
final class RequestParsingTest extends BaseBlackboxTestCase
{
private static string $handler_url;

Expand Down
21 changes: 10 additions & 11 deletions test/blackbox/StackdriverLoggingTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

use Ingenerator\PHPUtils\StringEncoding\JSON;

class StackdriverLoggingTest extends BaseBlackboxTestCase
final class StackdriverLoggingTest extends BaseBlackboxTestCase
{
// Note, these tests are necessarily verbose and fairly tightly coupled to the code structure, because I think for
// a blackbox test we should be as explicit as we can be about the detail of the log messages that are written -
Expand Down Expand Up @@ -71,8 +73,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
unset($request_log_msg['logging.googleapis.com/trace']);

// 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(
Expand Down Expand Up @@ -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(
Expand All @@ -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',
],
],
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/DefaultStackdriverLoggerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading
Loading