From bbdcfd3de0f1e10a826ebe60f56c0fae259dcb8f Mon Sep 17 00:00:00 2001 From: Filis Futsarov Date: Fri, 21 Mar 2025 20:10:19 +0100 Subject: [PATCH 1/2] php8.4 support among other improvements --- .gitattributes | 16 ++++++++-------- .gitignore | 2 ++ .php-cs-fixer.php | 10 ++++++++++ .phpstan.neon | 7 +++++++ LICENSE | 2 +- composer.json | 18 +++++++++--------- phpcs.xml | 16 ++++++++++++++++ phpunit.xml | 33 +++++++++++++++++++++++++++++++++ src/RequestHandler.php | 2 +- tests/Controller.php | 1 + tests/RequestHandlerTest.php | 21 +++++++++++---------- 11 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 .php-cs-fixer.php create mode 100644 .phpstan.neon create mode 100644 phpcs.xml create mode 100644 phpunit.xml diff --git a/.gitattributes b/.gitattributes index c540470..b67c7ca 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,10 +1,10 @@ * text=auto eol=lf -/tests export-ignore -.editorconfig export-ignore -.gitattributes export-ignore -.gitignore export-ignore -.php_cs export-ignore -.travis.yml export-ignore -phpcs.xml.dist export-ignore -phpunit.xml.dist export-ignore +/tests export-ignore +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.php-cs-fixer.php export-ignore +phpcs.xml.dist export-ignore +phpunit.xml.dist export-ignore +.phpstan.neon export-ignore diff --git a/.gitignore b/.gitignore index 364d1a4..5ae8693 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor composer.lock coverage *.cache +.idea +kit diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..90b12a3 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,10 @@ +setFinder( + PhpCsFixer\Finder::create() + ->files() + ->name('*.php') + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ); \ No newline at end of file diff --git a/.phpstan.neon b/.phpstan.neon new file mode 100644 index 0000000..e0b2499 --- /dev/null +++ b/.phpstan.neon @@ -0,0 +1,7 @@ +parameters: + level: 8 + paths: + - src + - tests + excludePaths: + - tests/RequestHandlerTest.php diff --git a/LICENSE b/LICENSE index 017c0cd..374fb13 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 +Copyright (c) 2019-2025 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index a71bb18..edbff16 100644 --- a/composer.json +++ b/composer.json @@ -20,17 +20,17 @@ }, "require": { "php": "^7.2 || ^8.0", - "middlewares/utils": "^3.0 || ^4.0", - "psr/http-server-middleware": "^1.0", + "middlewares/utils": "^2 || ^3 || ^4", + "psr/http-server-middleware": "^1", "psr/container": "^1.0||^2.0" }, "require-dev": { - "phpunit/phpunit": "^8|^9", - "friendsofphp/php-cs-fixer": "^2.0", - "oscarotero/php-cs-fixer-config": "^1.0", - "squizlabs/php_codesniffer": "^3.0", - "phpstan/phpstan": "^0.12", - "laminas/laminas-diactoros": "^2.3 || ^3.0" + "phpunit/phpunit": "^8 || ^9", + "friendsofphp/php-cs-fixer": "^3", + "oscarotero/php-cs-fixer-config": "^2", + "squizlabs/php_codesniffer": "^3", + "phpstan/phpstan": "^1 || ^2", + "laminas/laminas-diactoros": "^2 || ^3" }, "autoload": { "psr-4": { @@ -50,4 +50,4 @@ "coverage": "phpunit --coverage-text", "coverage-html": "phpunit --coverage-html=coverage" } -} +} \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..7814331 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,16 @@ + + + Middlewares coding standard + + + + + + + + + + + src + tests + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..72745de --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,33 @@ + + + + + tests + + + + + + ./src + + ./tests + ./vendor + + + + diff --git a/src/RequestHandler.php b/src/RequestHandler.php index 9f5a658..aaa30d7 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -32,7 +32,7 @@ class RequestHandler implements MiddlewareInterface /** * Set the resolver instance. */ - public function __construct(ContainerInterface $container = null) + public function __construct(?ContainerInterface $container = null) { $this->container = $container ?: new RequestHandlerContainer(); } diff --git a/tests/Controller.php b/tests/Controller.php index 463e9f0..80fa401 100644 --- a/tests/Controller.php +++ b/tests/Controller.php @@ -14,6 +14,7 @@ public function run(): ResponseInterface { $response = Factory::createResponse(); $response->getBody()->write('Ok'); + return $response; } diff --git a/tests/RequestHandlerTest.php b/tests/RequestHandlerTest.php index 2d2c08b..b037f0a 100644 --- a/tests/RequestHandlerTest.php +++ b/tests/RequestHandlerTest.php @@ -10,7 +10,6 @@ use Middlewares\Utils\Factory; use Middlewares\Utils\RequestHandler as UtilsRequestHandler; use PHPUnit\Framework\TestCase; -use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use RuntimeException; @@ -22,8 +21,10 @@ class RequestHandlerTest extends TestCase */ public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void { + /* @phpstan-ignore function.alreadyNarrowedType */ if (method_exists(parent::class, 'assertMatchesRegularExpression')) { parent::assertMatchesRegularExpression($pattern, $string, $message); + return; } @@ -35,7 +36,7 @@ public static function handleRequest(ServerRequestInterface $request): ResponseI return Factory::createResponse(); } - public function testString() + public function testString(): void { $response = Dispatcher::run( [ @@ -47,7 +48,7 @@ public function testString() self::assertSame(200, $response->getStatusCode()); } - public function testCustomAttribute() + public function testCustomAttribute(): void { $response = Dispatcher::run( [ @@ -59,7 +60,7 @@ public function testCustomAttribute() self::assertSame(200, $response->getStatusCode()); } - public function testInvalidHandler() + public function testInvalidHandler(): void { $this->expectException(RuntimeException::class); @@ -71,7 +72,7 @@ public function testInvalidHandler() ); } - public function testArrayHandler() + public function testArrayHandler(): void { $request = Factory::createServerRequest('GET', '/'); $request = $request->withAttribute('request-handler', ['Middlewares\\Tests\\Controller', 'run']); @@ -86,7 +87,7 @@ public function testArrayHandler() self::assertSame('Ok', (string) $response->getBody()); } - public function testRequestHandler() + public function testRequestHandler(): void { $response = Dispatcher::run( [ @@ -102,7 +103,7 @@ public function testRequestHandler() self::assertSame('Bar', $response->getHeaderLine('X-Foo')); } - public function testClosure() + public function testClosure(): void { $response = Dispatcher::run( [ @@ -118,7 +119,7 @@ public function testClosure() self::assertSame('Bar', $response->getHeaderLine('X-Foo')); } - public function testContinueOnEmptyClosure() + public function testContinueOnEmptyClosure(): void { $response = Dispatcher::run( [ @@ -133,7 +134,7 @@ function () { self::assertSame('Fallback', (string) $response->getBody()); } - public function testThrowExceptionOnEmpty() + public function testThrowExceptionOnEmpty(): void { $response = Dispatcher::run( [ @@ -156,7 +157,7 @@ function () { self::assertSame('Empty request handler', (string) $response->getBody()); } - public function testThrowExceptionOnInvalidHandler() + public function testThrowExceptionOnInvalidHandler(): void { $response = Dispatcher::run( [ From ef9b456d44ce7506fe560785374df3664fb063aa Mon Sep 17 00:00:00 2001 From: Filis Futsarov Date: Fri, 21 Mar 2025 22:12:46 +0100 Subject: [PATCH 2/2] update github workflow --- .github/workflows/main.yaml | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 8a996a5..5d14a59 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -1,10 +1,10 @@ name: "testing" on: - push: - branches: [ master ] - pull_request: - branches: [ master ] + push: + branches: [ master ] + pull_request: + branches: [ master ] jobs: qa: @@ -22,10 +22,10 @@ jobs: id: composer-cache uses: actions/cache@v4 with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- - name: Install dependencies if: steps.composer-cache.outputs.cache-hit != 'true' @@ -40,15 +40,15 @@ jobs: strategy: matrix: - php: - - 7.2 - - 7.3 - - 7.4 - - 8.0 - - 8.1 - - 8.2 - - 8.3 - - 8.4 + php: + - 7.2 + - 7.3 + - 7.4 + - 8.0 + - 8.1 + - 8.2 + - 8.3 + - 8.4 steps: - name: Checkout @@ -71,6 +71,3 @@ jobs: - name: Tests run: composer test - - - name: Tests coverage - run: composer coverage \ No newline at end of file