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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

### Unreleased

* [BREAKING]: Added strict types and array shapes throughout.
* [BREAKING]: FactoryFunction::call() no longer accepts or passes arguments to the callable.
* [Internal] Bump dev dependencies
* Require PHP 8.5
* Require ingenerator/php-utils version 3
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 10
paths:
- src
- test
excludePaths:
- test/blackbox/implementation/htdocs/dynamic
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
->withSkipPath(__DIR__.'/test/blackbox/implementation/htdocs/dynamic')
->withComposerBased(phpunit: true)
->withPhpSets()
->withPreparedSets(
codeQuality: true,
typeDeclarations: true,
typeDeclarationDocblocks: true,
)
->withImportNames();
4 changes: 4 additions & 0 deletions src/DefaultStackdriverLoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ingenerator\MicroFramework;

use Ingenerator\PHPUtils\Logging\DefaultLogMetadata;
use Ingenerator\PHPUtils\Logging\LogMetadataProvider;
use Ingenerator\PHPUtils\Logging\StackdriverApplicationLogger;
use Psr\Log\LoggerInterface;

Expand All @@ -29,6 +30,9 @@ public function getLogger(): LoggerInterface
return StackdriverApplicationLogger::instance();
}

/**
* @return list<LogMetadataProvider|array<string,mixed>>
*/
protected function getMetadataProviders(): array
{
return [
Expand Down
13 changes: 6 additions & 7 deletions src/FactoryFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ class FactoryFunction
* // Whereas this will throw an UnexpectedValueException
* $stdclass = FactoryFunction::call(fn() => new DateTimeImmutable, stdClass::class)
*
* @psalm-template T
* @template T of object
*
* @psalm-param callable():T $callable
* @psalm-param class-string<T> $expect_class
* @psalm-param array $arguments that will be passed to the function
* @param callable():T $factory
* @param class-string<T> $expect_class
*
* @psalm-return T
* @return T
*/
public static function call(callable $factory, string $expect_class, array $arguments = []): object
public static function call(callable $factory, string $expect_class): object
{
$object = $factory(...$arguments);
$object = $factory();

if (!$object instanceof $expect_class) {
throw new UnexpectedValueException(
Expand Down
4 changes: 2 additions & 2 deletions src/MicroFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class MicroFramework
{
public static function throwingErrorHandler($code, $error, $file = null, $line = null): bool
public static function throwingErrorHandler(int $code, string $error, ?string $file = null, ?int $line = null): bool
{
if (error_reporting() & $code) {
// This error is not suppressed by current error reporting settings
Expand Down Expand Up @@ -78,7 +78,7 @@ public function execute(
: ServerRequest::fromGlobals();

// Hand off to the application for execution
$response = FactoryFunction::call($handler_factory, RequestHandler::class, [$logger])
$response = FactoryFunction::call(fn () => $handler_factory($logger), RequestHandler::class)
->handle($request);

// Render the application result
Expand Down
29 changes: 26 additions & 3 deletions test/blackbox/BaseBlackboxTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use UnexpectedValueException;

Expand Down Expand Up @@ -40,7 +40,12 @@ protected function setUp(): void
private function getTestSubjectBaseUrl(): string
{
$subject_url = getenv('TEST_SUBJECT_BASE_URI');
Assert::assertNotEmpty($subject_url, 'Expect an environment variable TEST_SUBJECT_BASE_URI');
if (!is_string($subject_url)) {
throw new RuntimeException('Expect an environment variable TEST_SUBJECT_BASE_URI as a string');
}
if (empty($subject_url)) {
throw new RuntimeException('Expect an environment variable TEST_SUBJECT_BASE_URI not to be empty string');
}

return $subject_url;
}
Expand All @@ -55,6 +60,9 @@ protected function truncateTestSubjectLogs(): void
}
}

/**
* @return list<string>
*/
protected function getTestSubjectLogEntriesDuringTest(): array
{
// Wait a moment for logs to be written - they aren't always absolutely immediate
Expand All @@ -67,9 +75,14 @@ protected function getTestSubjectLogEntriesDuringTest(): array
// Each line in the file is in syslog RFC3164 format. The details of the message header not relevant to us, we
// are only using syslog format to be able to capture the underlying container STDOUT/STDERR in a docker compose
// environment. So we just split the lines on the end of the HEADER and return the CONTENT
$lines = file(self::$test_subject_logs_file, FILE_IGNORE_NEW_LINES);
if ($lines === false) {
throw new RuntimeException('Could not read '.self::$test_subject_logs_file);
}

return array_map(
self::extractSyslogRFC3164MessageBody(...),
file(self::$test_subject_logs_file, FILE_IGNORE_NEW_LINES),
$lines,
);
}

Expand Down Expand Up @@ -138,4 +151,14 @@ protected static function provisionDynamicHandler(string $code): string

return '/dynamic/'.$id.'/';
}

final protected function getLocalIp(): string
{
$hostname = gethostname();
if ($hostname === false) {
throw new RuntimeException('Could not get hostname');
}

return gethostbyname($hostname);
}
}
4 changes: 3 additions & 1 deletion test/blackbox/BlackboxSmokeTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

class BlackboxSmokeTest extends BaseBlackboxTestCase
{
public function testTestSubjectRespondsFromDefaultHandler()
public function testTestSubjectRespondsFromDefaultHandler(): void
{
$this->assertResponseMatches(
200,
Expand Down
6 changes: 4 additions & 2 deletions test/blackbox/EnvironmentConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

class EnvironmentConfigurationTest extends BaseBlackboxTestCase
{
public function testCanConfigureCustomTimezoneInBootstrap()
public function testCanConfigureCustomTimezoneInBootstrap(): void
{
$handler_url = $this->provisionDynamicHandler(
<<<'PHP'
Expand Down Expand Up @@ -41,7 +43,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface {
);
}

public function testCanConfigureLocaleInBootstrap()
public function testCanConfigureLocaleInBootstrap(): void
{
$handler_url = $this->provisionDynamicHandler(
<<<'PHP'
Expand Down
7 changes: 6 additions & 1 deletion test/blackbox/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

use PHPUnit\Framework\Attributes\DataProvider;

class ErrorHandlingTest extends BaseBlackboxTestCase
{
/**
* @return array<string, list<string>>
*/
public static function provider_error_handling(): array
{
return [
Expand Down Expand Up @@ -73,7 +78,7 @@ private function doThings(string $uhoh): ResponseInterface {
}

#[DataProvider('provider_error_handling')]
public function testItCapturesErrorsAndExceptions(string $handler_code)
public function testItCapturesErrorsAndExceptions(string $handler_code): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap($handler_code);

Expand Down
13 changes: 11 additions & 2 deletions test/blackbox/HttpHeadersTest.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\RequestOptions;
Expand All @@ -25,6 +27,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

/**
* @return array<string, array{array<string,string>, array<string,list<string>>}>
*/
public static function provider_headers(): array
{
return [
Expand Down Expand Up @@ -63,8 +68,12 @@ public static function provider_headers(): array
];
}

/**
* @param array<string,string> $specified_headers
* @param array<string,list<string>> $expected_headers
*/
#[DataProvider('provider_headers')]
public function testItReturnsExpectedHeaders(array $specified_headers, array $expected_headers)
public function testItReturnsExpectedHeaders(array $specified_headers, array $expected_headers): void
{
$response = $this->guzzle->post(
self::$handler_url,
Expand All @@ -82,7 +91,7 @@ public function testItReturnsExpectedHeaders(array $specified_headers, array $ex
$this->assertSame($expected_headers, $all_headers);
}

public function testItGivesErrorOnUnsupportedUseOfDuplicateOutputHeaders()
public function testItGivesErrorOnUnsupportedUseOfDuplicateOutputHeaders(): void
{
// We can't support multiples for the same header because we can't reliably guess whether to pass
// replace => true or replace => false into the `header()` call and it's more important that we
Expand Down
6 changes: 4 additions & 2 deletions test/blackbox/HttpResponseCodesTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace test\blackbox;

use PHPUnit\Framework\Attributes\TestWith;
Expand Down Expand Up @@ -29,7 +31,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
#[TestWith([404])]
#[TestWith([500])]
#[TestWith([502])]
public function testItReturnsStatusCodeFromHandler(int $status_code)
public function testItReturnsStatusCodeFromHandler(int $status_code): void
{
$this->assertResponseMatches(
$status_code,
Expand All @@ -38,7 +40,7 @@ public function testItReturnsStatusCodeFromHandler(int $status_code)
);
}

public function testItCanOptionallyUseCustomStatusCodesIfReasonPhraseProvided()
public function testItCanOptionallyUseCustomStatusCodesIfReasonPhraseProvided(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand Down
14 changes: 8 additions & 6 deletions test/blackbox/OutputBufferingTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);

namespace blackbox;

use test\blackbox\BaseBlackboxTestCase;

class OutputBufferingTest extends BaseBlackboxTestCase
{
public function testItHandlesErrorEvenIfOutputBeganBeforeFramework()
public function testItHandlesErrorEvenIfOutputBeganBeforeFramework(): void
{
$handler_url = $this->provisionDynamicHandler(
<<<'PHP'
Expand Down Expand Up @@ -44,7 +46,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

public function testItReturnsErrorWithoutOutputIfAnyCodeWritesOutputDuringRequestHandling()
public function testItReturnsErrorWithoutOutputIfAnyCodeWritesOutputDuringRequestHandling(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand All @@ -65,7 +67,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

public function testItTreatsAsErrorIfUserLeavesOutputBuffersOpen()
public function testItTreatsAsErrorIfUserLeavesOutputBuffersOpen(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand All @@ -87,7 +89,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

public function testItTreatsAsErrorIfUserFlushesOutputBuffers()
public function testItTreatsAsErrorIfUserFlushesOutputBuffers(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand All @@ -110,7 +112,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

public function testItDoesNotTreatAsErrorIfUserHandlesOutputBufferingCorrectly()
public function testItDoesNotTreatAsErrorIfUserHandlesOutputBufferingCorrectly(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand All @@ -132,7 +134,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

public function testItDoesNotTreatAsErrorIfUserHandlesOutputBufferingCorrectlyWithoutRendering()
public function testItDoesNotTreatAsErrorIfUserHandlesOutputBufferingCorrectlyWithoutRendering(): void
{
$handler_url = self::provisionDynamicHandlerFactoryWithDefaultBootstrap(
<<<'PHP'
Expand Down
Loading
Loading