From 0d0267ba22e5f2726cc4e9127b5c23d5d7b0f0b4 Mon Sep 17 00:00:00 2001 From: Worma Date: Sat, 2 Aug 2025 10:12:57 +0200 Subject: [PATCH] setAccessible() has no effect as of PHP 8.1 --- src/Query/RetryExecutor.php | 4 +- tests/Query/TcpTransportExecutorTest.php | 48 +++++-- tests/Query/TimeoutExecutorTest.php | 4 +- tests/Query/UdpTransportExecutorTest.php | 16 ++- tests/Resolver/FactoryTest.php | 152 +++++++++++++++++------ 5 files changed, 168 insertions(+), 56 deletions(-) diff --git a/src/Query/RetryExecutor.php b/src/Query/RetryExecutor.php index e68dcc61..5fdf45ad 100644 --- a/src/Query/RetryExecutor.php +++ b/src/Query/RetryExecutor.php @@ -49,7 +49,9 @@ public function tryQuery(Query $query, $retries) // avoid garbage references by replacing all closures in call stack. // what a lovely piece of code! $r = new \ReflectionProperty(\Exception::class, 'trace'); - $r->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $r->setAccessible(true); + } $trace = $r->getValue($e); // Exception trace arguments are not available on some PHP 7.4 installs diff --git a/tests/Query/TcpTransportExecutorTest.php b/tests/Query/TcpTransportExecutorTest.php index 5fc62b43..2d0a126d 100644 --- a/tests/Query/TcpTransportExecutorTest.php +++ b/tests/Query/TcpTransportExecutorTest.php @@ -31,7 +31,9 @@ public function testCtorShouldAcceptNameserverAddresses($input, $expected) $executor = new TcpTransportExecutor($input, $loop); $ref = new \ReflectionProperty($executor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $value = $ref->getValue($executor); $this->assertEquals($expected, $value); @@ -70,7 +72,9 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $executor = new TcpTransportExecutor('127.0.0.1'); $ref = new \ReflectionProperty($executor, 'loop'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $loop = $ref->getValue($executor); $this->assertInstanceOf(LoopInterface::class, $loop); @@ -128,7 +132,9 @@ public function testQueryRejectsIfServerConnectionFails() $executor = new TcpTransportExecutor('::1', $loop); $ref = new \ReflectionProperty($executor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $ref->setValue($executor, '///'); $query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN); @@ -307,7 +313,9 @@ public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneCh $promise->then($this->expectCallableNever(), $this->expectCallableNever()); $ref = new \ReflectionProperty($executor, 'writePending'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $writePending = $ref->getValue($executor); $this->assertTrue($writePending); @@ -349,7 +357,9 @@ public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneCh $promise->then($this->expectCallableNever(), $this->expectCallableNever()); $ref = new \ReflectionProperty($executor, 'writePending'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $writePending = $ref->getValue($executor); $this->assertTrue($writePending); @@ -390,7 +400,9 @@ public function testQueryRejectsWhenClientKeepsSendingWhenServerClosesSocketWith $executor->handleWritable(); $ref = new \ReflectionProperty($executor, 'writePending'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $writePending = $ref->getValue($executor); // We expect an EPIPE (Broken pipe) on second write. @@ -745,7 +757,9 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAndWillStartIdl // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); @@ -780,7 +794,9 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancelling // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); @@ -813,7 +829,9 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancelling // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); @@ -854,7 +872,9 @@ public function testTriggerIdleTimerAfterPreviousQueryResolvedWillCloseIdleSocke // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); @@ -892,7 +912,9 @@ public function testClosingConnectionAfterPreviousQueryResolvedWillCancelIdleTim // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); @@ -930,7 +952,9 @@ public function testQueryAgainAfterPreviousQueryResolvedWillReuseSocketAndCancel // use outgoing buffer as response message $ref = new \ReflectionProperty($executor, 'writeBuffer'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $data = $ref->getValue($executor); $client = stream_socket_accept($server); diff --git a/tests/Query/TimeoutExecutorTest.php b/tests/Query/TimeoutExecutorTest.php index 96b355ce..81663d6b 100644 --- a/tests/Query/TimeoutExecutorTest.php +++ b/tests/Query/TimeoutExecutorTest.php @@ -38,7 +38,9 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $executor = new TimeoutExecutor($this->executor, 5.0); $ref = new \ReflectionProperty($executor, 'loop'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $loop = $ref->getValue($executor); $this->assertInstanceOf(LoopInterface::class, $loop); diff --git a/tests/Query/UdpTransportExecutorTest.php b/tests/Query/UdpTransportExecutorTest.php index 41b27a9a..e7cd1fe8 100644 --- a/tests/Query/UdpTransportExecutorTest.php +++ b/tests/Query/UdpTransportExecutorTest.php @@ -30,7 +30,9 @@ public function testCtorShouldAcceptNameserverAddresses($input, $expected) $executor = new UdpTransportExecutor($input, $loop); $ref = new \ReflectionProperty($executor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $value = $ref->getValue($executor); $this->assertEquals($expected, $value); @@ -69,7 +71,9 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $executor = new UdpTransportExecutor('127.0.0.1'); $ref = new \ReflectionProperty($executor, 'loop'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $loop = $ref->getValue($executor); $this->assertInstanceOf(LoopInterface::class, $loop); @@ -132,7 +136,9 @@ public function testQueryRejectsIfServerConnectionFails() $executor = new UdpTransportExecutor('::1', $loop); $ref = new \ReflectionProperty($executor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $ref->setValue($executor, '///'); $query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN); @@ -161,7 +167,9 @@ public function testQueryRejectsIfSendToServerFailsAfterConnectionWithoutCalling // increase hard-coded maximum packet size to allow sending excessive data $ref = new \ReflectionProperty($executor, 'maxPacketSize'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $ref->setValue($executor, PHP_INT_MAX); $error = null; diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index c50f80f3..38aa412f 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -45,13 +45,17 @@ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExe $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $selectiveExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(SelectiveTransportExecutor::class, $selectiveExecutor); @@ -59,13 +63,17 @@ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExe // udp below: $ref = new \ReflectionProperty($selectiveExecutor, 'datagramExecutor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($selectiveExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $udpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(UdpTransportExecutor::class, $udpExecutor); @@ -73,13 +81,17 @@ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExe // tcp below: $ref = new \ReflectionProperty($selectiveExecutor, 'streamExecutor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($selectiveExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); @@ -100,19 +112,25 @@ public function createWithUdpSchemeShouldCreateResolverWithUdpExecutorStack() $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $udpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(UdpTransportExecutor::class, $udpExecutor); @@ -133,19 +151,25 @@ public function createWithTcpSchemeShouldCreateResolverWithTcpExecutorStack() $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); @@ -169,19 +193,25 @@ public function createWithConfigWithTcpNameserverSchemeShouldCreateResolverWithT $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); @@ -206,49 +236,65 @@ public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResol $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $fallbackExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $nameserver = $ref->getValue($tcpExecutor); $this->assertEquals('tcp://8.8.8.8:53', $nameserver); $ref = new \ReflectionProperty($fallbackExecutor, 'fallback'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $nameserver = $ref->getValue($tcpExecutor); $this->assertEquals('tcp://1.1.1.1:53', $nameserver); @@ -274,73 +320,97 @@ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateRes $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $retryExecutor = $ref->getValue($coopExecutor); $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $fallbackExecutor = $ref->getValue($retryExecutor); $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $nameserver = $ref->getValue($tcpExecutor); $this->assertEquals('tcp://8.8.8.8:53', $nameserver); $ref = new \ReflectionProperty($fallbackExecutor, 'fallback'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $fallbackExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $nameserver = $ref->getValue($tcpExecutor); $this->assertEquals('tcp://1.1.1.1:53', $nameserver); $ref = new \ReflectionProperty($fallbackExecutor, 'fallback'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $timeoutExecutor = $ref->getValue($fallbackExecutor); $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $tcpExecutor = $ref->getValue($timeoutExecutor); $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); - $ref->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $nameserver = $ref->getValue($tcpExecutor); $this->assertEquals('tcp://9.9.9.9:53', $nameserver); @@ -418,7 +488,9 @@ private function getResolverPrivateExecutor($resolver) // extract underlying executor that may be wrapped in multiple layers of hosts file executors while ($executor instanceof HostsFileExecutor) { $reflector = new \ReflectionProperty(HostsFileExecutor::class, 'fallback'); - $reflector->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $reflector->setAccessible(true); + } $executor = $reflector->getValue($executor); } @@ -429,14 +501,18 @@ private function getResolverPrivateExecutor($resolver) private function getResolverPrivateMemberValue($resolver, $field) { $reflector = new \ReflectionProperty(Resolver::class, $field); - $reflector->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $reflector->setAccessible(true); + } return $reflector->getValue($resolver); } private function getCachingExecutorPrivateMemberValue($resolver, $field) { $reflector = new \ReflectionProperty(CachingExecutor::class, $field); - $reflector->setAccessible(true); + if (\PHP_VERSION_ID < 80100) { + $reflector->setAccessible(true); + } return $reflector->getValue($resolver); } }