From 28e63191477b41d8208a17f630a6f451bedcce6c Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 23 Nov 2023 12:34:20 +0000 Subject: [PATCH 1/5] Use setStaticPropertyValue() to set static properties in tests From PHP 8.3, calling ReflectionProperty::setValue() with a single value is deprecated, so we use ReflectionClass::setStaticPropertyValue() instead. --- tests/Factory/AppFactoryTest.php | 16 ++++------------ .../Psr17/SlimHttpServerRequestCreatorTest.php | 8 ++------ 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/Factory/AppFactoryTest.php b/tests/Factory/AppFactoryTest.php index b802c2dc9..46eb56486 100644 --- a/tests/Factory/AppFactoryTest.php +++ b/tests/Factory/AppFactoryTest.php @@ -45,12 +45,8 @@ class AppFactoryTest extends TestCase { protected function tearDown(): void { - $responseFactoryDecoratorClassProperty = new ReflectionProperty( - SlimHttpPsr17Factory::class, - 'responseFactoryClass' - ); - $responseFactoryDecoratorClassProperty->setAccessible(true); - $responseFactoryDecoratorClassProperty->setValue(DecoratedResponseFactory::class); + $reflectionClass = new \ReflectionClass(SlimHttpPsr17Factory::class); + $reflectionClass->setStaticPropertyValue('responseFactoryClass', DecoratedResponseFactory::class); } public function provideImplementations() @@ -103,12 +99,8 @@ public function testDetermineResponseFactoryThrowsRuntimeExceptionIfDecoratedNot 'Slim\\Factory\\Psr17\\SlimHttpPsr17Factory could not instantiate a decorated response factory.' ); - $responseFactoryDecoratorClassProperty = new ReflectionProperty( - SlimHttpPsr17Factory::class, - 'responseFactoryClass' - ); - $responseFactoryDecoratorClassProperty->setAccessible(true); - $responseFactoryDecoratorClassProperty->setValue(stdClass::class); + $reflectionClass = new \ReflectionClass(SlimHttpPsr17Factory::class); + $reflectionClass->setStaticPropertyValue('responseFactoryClass', SlimHttpPsr17Factory::class); Psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); AppFactory::setSlimHttpDecoratorsAutomaticDetection(true); diff --git a/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php b/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php index 98c23e0b5..d81a8b387 100644 --- a/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php +++ b/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php @@ -91,12 +91,8 @@ public function testCreateServerRequestFromGlobalsThrowsRuntimeExceptionIfNotIns $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); - $serverRequestDecoratorClassProperty = new ReflectionProperty( - SlimHttpServerRequestCreator::class, - 'serverRequestDecoratorClass' - ); - $serverRequestDecoratorClassProperty->setAccessible(true); - $serverRequestDecoratorClassProperty->setValue(stdClass::class); + $reflectionClass = new \ReflectionClass(SlimHttpServerRequestCreator::class); + $reflectionClass->setStaticPropertyValue('serverRequestDecoratorClass', stdClass::class); $slimHttpServerRequestCreator->createServerRequestFromGlobals(); } From fb556263897d2ea15615f51e4f2ade644b81a3c1 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 23 Nov 2023 12:36:47 +0000 Subject: [PATCH 2/5] Remove unused reflected property in ErrorHandlerTest It's not used and causes a warning. --- tests/Handlers/ErrorHandlerTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index f23acd6b3..9346f1c7e 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -241,10 +241,6 @@ public function testAcceptableMediaTypeIsNotFirstInList() $method = $class->getMethod('determineContentType'); $method->setAccessible(true); - $reflectionProperty = $class->getProperty('responseFactory'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($class, $this->getResponseFactory()); - // use a mock object here as ErrorHandler cannot be directly instantiated $handler = $this ->getMockBuilder(ErrorHandler::class) From 4c7e0cfe6cca0c9c0b0d5b454302ece0bf73809b Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 23 Nov 2023 12:45:08 +0000 Subject: [PATCH 3/5] Update BodyParsingMiddlewareTest Dynamically adding a property to a class is deprecated and so rather than adding the request to the response from the handler, we now add it to the handler itself and test it there. --- tests/Middleware/BodyParsingMiddlewareTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/Middleware/BodyParsingMiddlewareTest.php b/tests/Middleware/BodyParsingMiddlewareTest.php index 75f0df3c4..30cba022c 100644 --- a/tests/Middleware/BodyParsingMiddlewareTest.php +++ b/tests/Middleware/BodyParsingMiddlewareTest.php @@ -31,6 +31,7 @@ protected function createRequestHandler(): RequestHandlerInterface $response = $this->createResponse(); return new class ($response) implements RequestHandlerInterface { private $response; + public $request; public function __construct(ResponseInterface $response) { @@ -39,7 +40,7 @@ public function __construct(ResponseInterface $response) public function handle(ServerRequestInterface $request): ResponseInterface { - $this->response->request = $request; + $this->request = $request; return $this->response; } }; @@ -152,9 +153,10 @@ public function testParsing($contentType, $body, $expected) $request = $this->createRequestWithBody($contentType, $body); $middleware = new BodyParsingMiddleware(); - $response = $middleware->process($request, $this->createRequestHandler()); + $requestHandler = $this->createRequestHandler(); + $middleware->process($request, $requestHandler); - $this->assertEquals($expected, $response->request->getParsedBody()); + $this->assertEquals($expected, $requestHandler->request->getParsedBody()); } public function testParsingWithARegisteredParser() @@ -167,9 +169,10 @@ public function testParsingWithARegisteredParser() }, ]; $middleware = new BodyParsingMiddleware($parsers); - $response = $middleware->process($request, $this->createRequestHandler()); + $requestHandler = $this->createRequestHandler(); + $middleware->process($request, $requestHandler); - $this->assertSame(['data' => '{"foo":"bar"}'], $response->request->getParsedBody()); + $this->assertSame(['data' => '{"foo":"bar"}'], $requestHandler->request->getParsedBody()); } public function testParsingFailsWhenAnInvalidTypeIsReturned() From 85a19ec091d195a264a8dc27a30409bc610b6eb2 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 23 Nov 2023 12:40:52 +0000 Subject: [PATCH 4/5] Add PHP 8.3 to the CI matrix Also use 8.2 for the CS and static analysis tests. --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 86f06735c..4a2f660be 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,10 +10,10 @@ jobs: strategy: fail-fast: false matrix: - php: [7.4, 8.0, 8.1, 8.2] + php: [7.4, 8.0, 8.1, 8.2, 8.3] experimental: [false] include: - - php: 8.1 + - php: 8.2 analysis: true steps: From 4bfb12aa55e70a22c4826c1d867b5d7ec1b5eb53 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Tue, 12 Dec 2023 09:35:25 +0000 Subject: [PATCH 5/5] Import ReflectionClass --- tests/Factory/AppFactoryTest.php | 5 +++-- tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Factory/AppFactoryTest.php b/tests/Factory/AppFactoryTest.php index 46eb56486..25349ffc8 100644 --- a/tests/Factory/AppFactoryTest.php +++ b/tests/Factory/AppFactoryTest.php @@ -18,6 +18,7 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamFactoryInterface; +use ReflectionClass; use ReflectionProperty; use RuntimeException; use Slim\Factory\AppFactory; @@ -45,7 +46,7 @@ class AppFactoryTest extends TestCase { protected function tearDown(): void { - $reflectionClass = new \ReflectionClass(SlimHttpPsr17Factory::class); + $reflectionClass = new ReflectionClass(SlimHttpPsr17Factory::class); $reflectionClass->setStaticPropertyValue('responseFactoryClass', DecoratedResponseFactory::class); } @@ -99,7 +100,7 @@ public function testDetermineResponseFactoryThrowsRuntimeExceptionIfDecoratedNot 'Slim\\Factory\\Psr17\\SlimHttpPsr17Factory could not instantiate a decorated response factory.' ); - $reflectionClass = new \ReflectionClass(SlimHttpPsr17Factory::class); + $reflectionClass = new ReflectionClass(SlimHttpPsr17Factory::class); $reflectionClass->setStaticPropertyValue('responseFactoryClass', SlimHttpPsr17Factory::class); Psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); diff --git a/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php b/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php index d81a8b387..328f4cb4c 100644 --- a/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php +++ b/tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php @@ -11,6 +11,7 @@ namespace Slim\Tests\Factory\Psr17; use Psr\Http\Message\ServerRequestInterface; +use ReflectionClass; use ReflectionProperty; use RuntimeException; use Slim\Factory\Psr17\SlimHttpServerRequestCreator; @@ -91,7 +92,7 @@ public function testCreateServerRequestFromGlobalsThrowsRuntimeExceptionIfNotIns $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); - $reflectionClass = new \ReflectionClass(SlimHttpServerRequestCreator::class); + $reflectionClass = new ReflectionClass(SlimHttpServerRequestCreator::class); $reflectionClass->setStaticPropertyValue('serverRequestDecoratorClass', stdClass::class); $slimHttpServerRequestCreator->createServerRequestFromGlobals();