Skip to content

Commit f0f5800

Browse files
committed
Only call setAccessible() if PHP < 8.1
1 parent 2ece663 commit f0f5800

File tree

9 files changed

+59
-46
lines changed

9 files changed

+59
-46
lines changed

tests/AppTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -703,18 +703,18 @@ public function testAddRoutingMiddleware(): void
703703

704704
// Check that the routing middleware really has been added to the tip of the app middleware stack.
705705
$middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher');
706-
$middlewareDispatcherProperty->setAccessible(true);
706+
$this->setAccessible($middlewareDispatcherProperty);
707707
/** @var MiddlewareDispatcher $middlewareDispatcher */
708708
$middlewareDispatcher = $middlewareDispatcherProperty->getValue($app);
709709

710710
$tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip');
711-
$tipProperty->setAccessible(true);
711+
$this->setAccessible($tipProperty);
712712
/** @var RequestHandlerInterface $tip */
713713
$tip = $tipProperty->getValue($middlewareDispatcher);
714714

715715
$reflection = new ReflectionClass($tip);
716716
$middlewareProperty = $reflection->getProperty('middleware');
717-
$middlewareProperty->setAccessible(true);
717+
$this->setAccessible($middlewareProperty);
718718

719719
$this->assertSame($routingMiddleware, $middlewareProperty->getValue($tip));
720720
$this->assertInstanceOf(RoutingMiddleware::class, $routingMiddleware);
@@ -736,18 +736,18 @@ public function testAddErrorMiddleware(): void
736736

737737
// Check that the error middleware really has been added to the tip of the app middleware stack.
738738
$middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher');
739-
$middlewareDispatcherProperty->setAccessible(true);
739+
$this->setAccessible($middlewareDispatcherProperty);
740740
/** @var MiddlewareDispatcher $middlewareDispatcher */
741741
$middlewareDispatcher = $middlewareDispatcherProperty->getValue($app);
742742

743743
$tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip');
744-
$tipProperty->setAccessible(true);
744+
$this->setAccessible($tipProperty);
745745
/** @var RequestHandlerInterface $tip */
746746
$tip = $tipProperty->getValue($middlewareDispatcher);
747747

748748
$reflection = new ReflectionClass($tip);
749749
$middlewareProperty = $reflection->getProperty('middleware');
750-
$middlewareProperty->setAccessible(true);
750+
$this->setAccessible($middlewareProperty);
751751

752752
$this->assertSame($errorMiddleware, $middlewareProperty->getValue($tip));
753753
$this->assertInstanceOf(ErrorMiddleware::class, $errorMiddleware);
@@ -766,18 +766,18 @@ public function testAddBodyParsingMiddleware(): void
766766

767767
// Check that the body parsing middleware really has been added to the tip of the app middleware stack.
768768
$middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher');
769-
$middlewareDispatcherProperty->setAccessible(true);
769+
$this->setAccessible($middlewareDispatcherProperty);
770770
/** @var MiddlewareDispatcher $middlewareDispatcher */
771771
$middlewareDispatcher = $middlewareDispatcherProperty->getValue($app);
772772

773773
$tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip');
774-
$tipProperty->setAccessible(true);
774+
$this->setAccessible($tipProperty);
775775
/** @var RequestHandlerInterface $tip */
776776
$tip = $tipProperty->getValue($middlewareDispatcher);
777777

778778
$reflection = new ReflectionClass($tip);
779779
$middlewareProperty = $reflection->getProperty('middleware');
780-
$middlewareProperty->setAccessible(true);
780+
$this->setAccessible($middlewareProperty);
781781

782782
$this->assertSame($bodyParsingMiddleware, $middlewareProperty->getValue($tip));
783783
$this->assertInstanceOf(BodyParsingMiddleware::class, $bodyParsingMiddleware);

tests/Error/AbstractErrorRendererTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testHTMLErrorRendererRenderFragmentMethod()
6060
$reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class);
6161

6262
$method = $reflectionRenderer->getMethod('renderExceptionFragment');
63-
$method->setAccessible(true);
63+
$this->setAccessible($method);
6464
$output = $method->invoke($renderer, $exception);
6565

6666
$this->assertMatchesRegularExpression('/.*Type:*/', $output);
@@ -101,7 +101,7 @@ public function testJSONErrorRendererDisplaysErrorDetails()
101101
$reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
102102

103103
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
104-
$method->setAccessible(true);
104+
$this->setAccessible($method);
105105

106106
$fragment = $method->invoke($renderer, $exception);
107107
$output = json_encode(json_decode($renderer->__invoke($exception, true)));
@@ -128,7 +128,7 @@ public function testJSONErrorRendererDisplaysPreviousError()
128128
$renderer = new JsonErrorRenderer();
129129
$reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
130130
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
131-
$method->setAccessible(true);
131+
$this->setAccessible($method);
132132

133133
$output = json_encode(json_decode($renderer->__invoke($exception, true)));
134134

@@ -208,7 +208,7 @@ public function testPlainTextErrorRendererFormatFragmentMethod()
208208
$reflectionRenderer = new ReflectionClass(PlainTextErrorRenderer::class);
209209

210210
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
211-
$method->setAccessible(true);
211+
$this->setAccessible($method);
212212
$output = $method->invoke($renderer, $exception);
213213
$this->assertIsString($output);
214214

tests/Factory/AppFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testCreateAppWithAllImplementations(string $psr17factory, string
7777
$routeCollector = $app->getRouteCollector();
7878

7979
$responseFactoryProperty = new ReflectionProperty(RouteCollector::class, 'responseFactory');
80-
$responseFactoryProperty->setAccessible(true);
80+
$this->setAccessible($responseFactoryProperty);
8181

8282
$responseFactory = $responseFactoryProperty->getValue($routeCollector);
8383

@@ -281,7 +281,7 @@ public function testResponseAndStreamFactoryIsBeingInjectedInDecoratedResponseFa
281281
$response = $responseFactory->createResponse();
282282

283283
$streamFactoryProperty = new ReflectionProperty(DecoratedResponse::class, 'streamFactory');
284-
$streamFactoryProperty->setAccessible(true);
284+
$this->setAccessible($streamFactoryProperty);
285285

286286
$this->assertSame($streamFactoryProphecy->reveal(), $streamFactoryProperty->getValue($response));
287287
}

tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function tearDown(): void
3636
SlimHttpServerRequestCreator::class,
3737
'serverRequestDecoratorClass'
3838
);
39-
$serverRequestDecoratorClassProperty->setAccessible(true);
39+
$this->setAccessible($serverRequestDecoratorClassProperty);
4040
$serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, ServerRequest::class);
4141
}
4242

@@ -69,7 +69,7 @@ public function testCreateServerRequestFromGlobalsThrowsRuntimeException()
6969
SlimHttpServerRequestCreator::class,
7070
'serverRequestDecoratorClass'
7171
);
72-
$serverRequestDecoratorClassProperty->setAccessible(true);
72+
$this->setAccessible($serverRequestDecoratorClassProperty);
7373
$serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, '');
7474

7575
$slimHttpServerRequestCreator->createServerRequestFromGlobals();

tests/Handlers/ErrorHandlerTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function testDetermineRenderer()
4040
$class = new ReflectionClass(ErrorHandler::class);
4141

4242
$callableResolverProperty = $class->getProperty('callableResolver');
43-
$callableResolverProperty->setAccessible(true);
43+
$this->setAccessible($callableResolverProperty);
4444
$callableResolverProperty->setValue($handler, $this->getCallableResolver());
4545

4646
$reflectionProperty = $class->getProperty('contentType');
47-
$reflectionProperty->setAccessible(true);
47+
$this->setAccessible($reflectionProperty);
4848
$reflectionProperty->setValue($handler, 'application/json');
4949

5050
$method = $class->getMethod('determineRenderer');
51-
$method->setAccessible(true);
51+
$this->setAccessible($method);
5252

5353
$renderer = $method->invoke($handler);
5454
$this->assertIsCallable($renderer);
@@ -78,15 +78,15 @@ public function testDetermineStatusCode()
7878
$class = new ReflectionClass(ErrorHandler::class);
7979

8080
$reflectionProperty = $class->getProperty('responseFactory');
81-
$reflectionProperty->setAccessible(true);
81+
$this->setAccessible($reflectionProperty);
8282
$reflectionProperty->setValue($handler, $this->getResponseFactory());
8383

8484
$reflectionProperty = $class->getProperty('exception');
85-
$reflectionProperty->setAccessible(true);
85+
$this->setAccessible($reflectionProperty);
8686
$reflectionProperty->setValue($handler, new HttpNotFoundException($request));
8787

8888
$method = $class->getMethod('determineStatusCode');
89-
$method->setAccessible(true);
89+
$this->setAccessible($method);
9090

9191
$statusCode = $method->invoke($handler);
9292
$this->assertSame($statusCode, 404);
@@ -133,15 +133,15 @@ public function testHalfValidContentType()
133133
$class = new ReflectionClass(ErrorHandler::class);
134134

135135
$reflectionProperty = $class->getProperty('responseFactory');
136-
$reflectionProperty->setAccessible(true);
136+
$this->setAccessible($reflectionProperty);
137137
$reflectionProperty->setValue($handler, $this->getResponseFactory());
138138

139139
$reflectionProperty = $class->getProperty('errorRenderers');
140-
$reflectionProperty->setAccessible(true);
140+
$this->setAccessible($reflectionProperty);
141141
$reflectionProperty->setValue($handler, $newErrorRenderers);
142142

143143
$method = $class->getMethod('determineContentType');
144-
$method->setAccessible(true);
144+
$this->setAccessible($method);
145145

146146
$contentType = $method->invoke($handler, $request);
147147

@@ -165,15 +165,15 @@ public function testDetermineContentTypeTextPlainMultiAcceptHeader()
165165
$class = new ReflectionClass(ErrorHandler::class);
166166

167167
$reflectionProperty = $class->getProperty('responseFactory');
168-
$reflectionProperty->setAccessible(true);
168+
$this->setAccessible($reflectionProperty);
169169
$reflectionProperty->setValue($handler, $this->getResponseFactory());
170170

171171
$reflectionProperty = $class->getProperty('errorRenderers');
172-
$reflectionProperty->setAccessible(true);
172+
$this->setAccessible($reflectionProperty);
173173
$reflectionProperty->setValue($handler, $errorRenderers);
174174

175175
$method = $class->getMethod('determineContentType');
176-
$method->setAccessible(true);
176+
$this->setAccessible($method);
177177

178178
$contentType = $method->invoke($handler, $request);
179179

@@ -196,15 +196,15 @@ public function testDetermineContentTypeApplicationJsonOrXml()
196196
$class = new ReflectionClass(ErrorHandler::class);
197197

198198
$reflectionProperty = $class->getProperty('responseFactory');
199-
$reflectionProperty->setAccessible(true);
199+
$this->setAccessible($reflectionProperty);
200200
$reflectionProperty->setValue($handler, $this->getResponseFactory());
201201

202202
$reflectionProperty = $class->getProperty('errorRenderers');
203-
$reflectionProperty->setAccessible(true);
203+
$this->setAccessible($reflectionProperty);
204204
$reflectionProperty->setValue($handler, $errorRenderers);
205205

206206
$method = $class->getMethod('determineContentType');
207-
$method->setAccessible(true);
207+
$this->setAccessible($method);
208208

209209
$contentType = $method->invoke($handler, $request);
210210

@@ -224,7 +224,7 @@ public function testAcceptableMediaTypeIsNotFirstInList()
224224
// provide access to the determineContentType() as it's a protected method
225225
$class = new ReflectionClass(ErrorHandler::class);
226226
$method = $class->getMethod('determineContentType');
227-
$method->setAccessible(true);
227+
$this->setAccessible($method);
228228

229229
// use a mock object here as ErrorHandler cannot be directly instantiated
230230
$handler = $this->createMock(ErrorHandler::class);
@@ -242,7 +242,7 @@ public function testRegisterErrorRenderer()
242242

243243
$reflectionClass = new ReflectionClass(ErrorHandler::class);
244244
$reflectionProperty = $reflectionClass->getProperty('errorRenderers');
245-
$reflectionProperty->setAccessible(true);
245+
$this->setAccessible($reflectionProperty);
246246
$errorRenderers = $reflectionProperty->getValue($handler);
247247

248248
$this->assertArrayHasKey('application/slim', $errorRenderers);
@@ -255,11 +255,11 @@ public function testSetDefaultErrorRenderer()
255255

256256
$reflectionClass = new ReflectionClass(ErrorHandler::class);
257257
$reflectionProperty = $reflectionClass->getProperty('defaultErrorRenderer');
258-
$reflectionProperty->setAccessible(true);
258+
$this->setAccessible($reflectionProperty);
259259
$defaultErrorRenderer = $reflectionProperty->getValue($handler);
260260

261261
$defaultErrorRendererContentTypeProperty = $reflectionClass->getProperty('defaultErrorRendererContentType');
262-
$defaultErrorRendererContentTypeProperty->setAccessible(true);
262+
$this->setAccessible($defaultErrorRendererContentTypeProperty);
263263
$defaultErrorRendererContentType = $defaultErrorRendererContentTypeProperty->getValue($handler);
264264

265265
$this->assertSame(PlainTextErrorRenderer::class, $defaultErrorRenderer);
@@ -396,16 +396,16 @@ public function testLogErrorRenderer()
396396
$handler->setLogErrorRenderer('logErrorRenderer');
397397

398398
$displayErrorDetailsProperty = new ReflectionProperty($handler, 'displayErrorDetails');
399-
$displayErrorDetailsProperty->setAccessible(true);
399+
$this->setAccessible($displayErrorDetailsProperty);
400400
$displayErrorDetailsProperty->setValue($handler, true);
401401

402402
$exception = new RuntimeException();
403403
$exceptionProperty = new ReflectionProperty($handler, 'exception');
404-
$exceptionProperty->setAccessible(true);
404+
$this->setAccessible($exceptionProperty);
405405
$exceptionProperty->setValue($handler, $exception);
406406

407407
$writeToErrorLogMethod = new ReflectionMethod($handler, 'writeToErrorLog');
408-
$writeToErrorLogMethod->setAccessible(true);
408+
$this->setAccessible($writeToErrorLogMethod);
409409
$writeToErrorLogMethod->invoke($handler);
410410
}
411411
}

tests/Middleware/OutputBufferingMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testStyleDefaultValid()
2626
$middleware = new OutputBufferingMiddleware($this->getStreamFactory());
2727

2828
$reflectionProperty = new ReflectionProperty($middleware, 'style');
29-
$reflectionProperty->setAccessible(true);
29+
$this->setAccessible($reflectionProperty);
3030
$value = $reflectionProperty->getValue($middleware);
3131

3232
$this->assertSame('append', $value);
@@ -37,7 +37,7 @@ public function testStyleCustomValid()
3737
$middleware = new OutputBufferingMiddleware($this->getStreamFactory(), 'prepend');
3838

3939
$reflectionProperty = new ReflectionProperty($middleware, 'style');
40-
$reflectionProperty->setAccessible(true);
40+
$this->setAccessible($reflectionProperty);
4141
$value = $reflectionProperty->getValue($middleware);
4242

4343
$this->assertSame('prepend', $value);

tests/ResponseEmitterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function testWillHandleInvalidConnectionStatusWithAnIndeterminateBody():
280280

281281
$mirror = new ReflectionClass(ResponseEmitter::class);
282282
$emitBodyMethod = $mirror->getMethod('emitBody');
283-
$emitBodyMethod->setAccessible(true);
283+
$this->setAccessible($emitBodyMethod);
284284
$emitBodyMethod->invoke($responseEmitter, $response);
285285

286286
$this->expectOutputString("");

tests/Routing/DispatcherTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testCreateDispatcher()
3434
$dispatcher = new Dispatcher($routeCollector);
3535

3636
$method = new ReflectionMethod(Dispatcher::class, 'createDispatcher');
37-
$method->setAccessible(true);
37+
$this->setAccessible($method);
3838

3939
$this->assertInstanceOf(FastRouteDispatcher::class, $method->invoke($dispatcher));
4040
}
@@ -57,7 +57,7 @@ public function testRouteCacheFileCanBeDispatched()
5757
$routeCollector->setCacheFile($cacheFile);
5858

5959
$method = new ReflectionMethod(Dispatcher::class, 'createDispatcher');
60-
$method->setAccessible(true);
60+
$this->setAccessible($method);
6161
$method->invoke($dispatcher);
6262
$this->assertFileExists($cacheFile, 'cache file was not created');
6363

@@ -66,7 +66,7 @@ public function testRouteCacheFileCanBeDispatched()
6666
$dispatcher2 = new Dispatcher($routeCollector2);
6767

6868
$method = new ReflectionMethod(Dispatcher::class, 'createDispatcher');
69-
$method->setAccessible(true);
69+
$this->setAccessible($method);
7070
$method->invoke($dispatcher2);
7171

7272
/** @var RoutingResults $result */
@@ -88,7 +88,7 @@ public function testCreateDispatcherReturnsSameDispatcherASecondTime()
8888
$dispatcher = new Dispatcher($routeCollector);
8989

9090
$method = new ReflectionMethod(Dispatcher::class, 'createDispatcher');
91-
$method->setAccessible(true);
91+
$this->setAccessible($method);
9292

9393
$fastRouteDispatcher = $method->invoke($dispatcher);
9494
$fastRouteDispatcher2 = $method->invoke($dispatcher);

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Psr\Http\Message\StreamFactoryInterface;
2121
use Psr\Http\Message\StreamInterface;
2222
use Psr\Http\Server\RequestHandlerInterface;
23+
use ReflectionMethod;
24+
use ReflectionProperty;
2325
use Slim\CallableResolver;
2426
use Slim\Interfaces\CallableResolverInterface;
2527
use Slim\MiddlewareDispatcher;
@@ -120,4 +122,15 @@ protected function createStream(string $contents = ''): StreamInterface
120122
$psr7ObjectProvider = new PSR7ObjectProvider();
121123
return $psr7ObjectProvider->createStream($contents);
122124
}
125+
126+
/**
127+
* @param ReflectionProperty|ReflectionMethod $ref
128+
* @return void
129+
*/
130+
protected function setAccessible($ref, bool $accessible = true): void
131+
{
132+
if (PHP_VERSION_ID < 80100) {
133+
$ref->setAccessible($accessible);
134+
}
135+
}
123136
}

0 commit comments

Comments
 (0)