Skip to content

Commit 343c54e

Browse files
Do not use ReflectionProperty::setAccessible() with PHP >= 8.1
1 parent d7dee6b commit 343c54e

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

ChangeLog-9.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes of the PHPUnit 9.6 release series are documented in this fil
66

77
### Changed
88

9+
* Do not use `ReflectionProperty::setAccessible()` with PHP >= 8.1
910
* Do not use `SplObjectStorage` methods that will be deprecated in PHP 8.5
1011

1112
## [9.6.23] - 2025-05-02

tests/unit/Framework/Constraint/BinaryOperatorTestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\Framework\Constraint;
1111

12+
use const PHP_VERSION;
1213
use function array_fill;
1314
use function array_map;
1415
use function array_slice;
@@ -18,6 +19,7 @@
1819
use function implode;
1920
use function sprintf;
2021
use function str_split;
22+
use function version_compare;
2123
use PHPUnit\Framework\ExpectationFailedException;
2224
use PHPUnit\TestFixture\BooleanConstraint;
2325
use PHPUnit\TestFixture\CountConstraint;
@@ -425,7 +427,10 @@ public function testFailureDescriptionWithSingleOperand(): void
425427
->willReturn('is the only');
426428

427429
$method = new ReflectionMethod($className, 'failureDescription');
428-
$method->setAccessible(true);
430+
431+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
432+
$method->setAccessible(true);
433+
}
429434

430435
$this->assertSame("'whatever' is the only", $method->invoke($operator, 'whatever'));
431436
}
@@ -533,7 +538,10 @@ public function testFailureDescriptionWithMultipleOperands(): void
533538
->willReturn('is fifth or later');
534539

535540
$method = new ReflectionMethod($className, 'failureDescription');
536-
$method->setAccessible(true);
541+
542+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
543+
$method->setAccessible(true);
544+
}
537545

538546
$expectedToString = self::operatorJoinStrings([
539547
'is first',

tests/unit/Framework/Constraint/UnaryOperatorTestCase.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
*/
1010
namespace PHPUnit\Framework\Constraint;
1111

12+
use const PHP_VERSION;
1213
use function array_map;
1314
use function sprintf;
15+
use function version_compare;
1416
use PHPUnit\Framework\ExpectationFailedException;
1517
use PHPUnit\TestFixture\BooleanConstraint;
1618
use PHPUnit\TestFixture\CountConstraint;
@@ -395,7 +397,10 @@ public function testFailureDescriptionWithNativeTransformations(string $input, s
395397
$constraint = new $className($operand);
396398

397399
$method = (new ReflectionMethod($className, 'failureDescription'));
398-
$method->setAccessible(true);
400+
401+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
402+
$method->setAccessible(true);
403+
}
399404

400405
$this->assertSame("'whatever' " . $expected, $method->invokeArgs($constraint, ['whatever']));
401406
}
@@ -427,7 +432,10 @@ public function testFailureDescriptionWithNonContextualTerminalConstraint(): voi
427432
->willReturn($string);
428433

429434
$method = (new ReflectionMethod($className, 'failureDescription'));
430-
$method->setAccessible(true);
435+
436+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
437+
$method->setAccessible(true);
438+
}
431439

432440
$expected = "'whatever' " . $string;
433441

@@ -459,7 +467,10 @@ public function testFailureDescriptionWithContextualTerminalConstraint(): void
459467
->method('toString');
460468

461469
$method = (new ReflectionMethod($className, 'failureDescription'));
462-
$method->setAccessible(true);
470+
471+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
472+
$method->setAccessible(true);
473+
}
463474

464475
$expected = "'whatever' " . $string;
465476

@@ -499,7 +510,10 @@ public function testFailureDescriptionWithContextualUnaryOperator(): void
499510
->method('toString');
500511

501512
$method = (new ReflectionMethod($className, 'failureDescription'));
502-
$method->setAccessible(true);
513+
514+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
515+
$method->setAccessible(true);
516+
}
503517

504518
$expected = "'whatever' " . $string;
505519

@@ -543,7 +557,10 @@ public function testFailureDescriptionWithNonContextualBinaryOperatorOfHigherPre
543557
->willReturn($string);
544558

545559
$method = (new ReflectionMethod($className, 'failureDescription'));
546-
$method->setAccessible(true);
560+
561+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
562+
$method->setAccessible(true);
563+
}
547564

548565
$expected = "'whatever' " . $string;
549566

@@ -585,7 +602,10 @@ public function testFailureDescriptionWithContextualBinaryOperatorOfHigherPreced
585602
->method('toString');
586603

587604
$method = (new ReflectionMethod($className, 'failureDescription'));
588-
$method->setAccessible(true);
605+
606+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
607+
$method->setAccessible(true);
608+
}
589609

590610
$expected = "'whatever' " . $string;
591611

@@ -627,7 +647,10 @@ public function testFailureDescriptionWithBinaryOperatorOfLowerPrecedence(): voi
627647
->willReturn($string);
628648

629649
$method = (new ReflectionMethod($className, 'failureDescription'));
630-
$method->setAccessible(true);
650+
651+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
652+
$method->setAccessible(true);
653+
}
631654

632655
$expected = $this->getOperatorName() . "( 'whatever' " . $string . ' )';
633656

tests/unit/Framework/MockObject/MockObjectTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,9 @@ private function resetMockObjects(): void
13561356
$prop = $refl->getProperty('mockObjects');
13571357

13581358
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
1359-
$prop->setAccessible(true);
1359+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
1360+
$prop->setAccessible(true);
1361+
}
13601362
}
13611363

13621364
$prop->setValue($this, []);

0 commit comments

Comments
 (0)