From 8db0cb5f29840e5e642279999797b2ece8345665 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Tue, 27 Feb 2024 09:15:09 +0100 Subject: [PATCH] Update test suite and remove legacy PHPUnit workarounds --- composer.json | 2 +- phpunit.xml.legacy | 2 +- tests/Config/ConfigTest.php | 10 +- tests/Config/HostsFileTest.php | 6 +- tests/FunctionalResolverTest.php | 10 +- tests/Protocol/ParserTest.php | 64 ++++--- tests/Query/CachingExecutorTest.php | 68 ++++---- tests/Query/CoopExecutorTest.php | 44 ++--- tests/Query/FallbackExecutorTest.php | 92 +++++----- tests/Query/HostsFileExecutorTest.php | 10 +- tests/Query/RetryExecutorTest.php | 44 ++--- .../Query/SelectiveTransportExecutorTest.php | 23 +-- tests/Query/TcpTransportExecutorTest.php | 165 +++++++++--------- tests/Query/TimeoutExecutorTest.php | 19 +- tests/Query/UdpTransportExecutorTest.php | 116 ++++++------ tests/Resolver/FactoryTest.php | 142 ++++++++------- tests/Resolver/ResolveAliasesTest.php | 96 +++++----- tests/Resolver/ResolverTest.php | 75 ++++---- tests/TestCase.php | 24 +-- 19 files changed, 519 insertions(+), 493 deletions(-) diff --git a/composer.json b/composer.json index 263fba0f..8fe3697d 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "react/promise": "^3.0 || ^2.7 || ^1.2.1" }, "require-dev": { - "phpunit/phpunit": "^9.6 || ^5.7", + "phpunit/phpunit": "^9.6 || ^7.5", "react/async": "^4 || ^3 || ^2", "react/promise-timer": "^1.9" }, diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy index a018d7ab..00868603 100644 --- a/phpunit.xml.legacy +++ b/phpunit.xml.legacy @@ -2,7 +2,7 @@ diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 1ea8d4f7..46d2f53d 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -2,8 +2,8 @@ namespace React\Tests\Dns\Config; -use React\Tests\Dns\TestCase; use React\Dns\Config\Config; +use React\Tests\Dns\TestCase; class ConfigTest extends TestCase { @@ -11,7 +11,7 @@ public function testLoadsSystemDefault() { $config = Config::loadSystemConfigBlocking(); - $this->assertInstanceOf('React\Dns\Config\Config', $config); + $this->assertInstanceOf(Config::class, $config); } public function testLoadsDefaultPath() @@ -22,7 +22,7 @@ public function testLoadsDefaultPath() $config = Config::loadResolvConfBlocking(); - $this->assertInstanceOf('React\Dns\Config\Config', $config); + $this->assertInstanceOf(Config::class, $config); } public function testLoadsFromExplicitPath() @@ -34,7 +34,7 @@ public function testLoadsFromExplicitPath() public function testLoadThrowsWhenPathIsInvalid() { - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); Config::loadResolvConfBlocking(__DIR__ . '/invalid.conf'); } @@ -115,7 +115,7 @@ public function testLoadsFromWmicOnWindows() $config = Config::loadWmicBlocking(); - $this->assertInstanceOf('React\Dns\Config\Config', $config); + $this->assertInstanceOf(Config::class, $config); } public function testLoadsSingleEntryFromWmicOutput() diff --git a/tests/Config/HostsFileTest.php b/tests/Config/HostsFileTest.php index b4ccebc5..a8440896 100644 --- a/tests/Config/HostsFileTest.php +++ b/tests/Config/HostsFileTest.php @@ -2,8 +2,8 @@ namespace React\Tests\Dns\Config; -use React\Tests\Dns\TestCase; use React\Dns\Config\HostsFile; +use React\Tests\Dns\TestCase; class HostsFileTest extends TestCase { @@ -11,7 +11,7 @@ public function testLoadsFromDefaultPath() { $hosts = HostsFile::loadFromPathBlocking(); - $this->assertInstanceOf('React\Dns\Config\HostsFile', $hosts); + $this->assertInstanceOf(HostsFile::class, $hosts); } public function testDefaultShouldHaveLocalhostMapped() @@ -27,7 +27,7 @@ public function testDefaultShouldHaveLocalhostMapped() public function testLoadThrowsForInvalidPath() { - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); HostsFile::loadFromPathBlocking('does/not/exist'); } diff --git a/tests/FunctionalResolverTest.php b/tests/FunctionalResolverTest.php index 3b707d90..25900cc0 100644 --- a/tests/FunctionalResolverTest.php +++ b/tests/FunctionalResolverTest.php @@ -2,8 +2,10 @@ namespace React\Tests\Dns; -use React\Dns\Resolver\Factory; use React\Dns\Model\Message; +use React\Dns\Query\CancellationException; +use React\Dns\RecordNotFoundException; +use React\Dns\Resolver\Factory; use React\EventLoop\Loop; class FunctionalResolverTest extends TestCase @@ -116,7 +118,7 @@ public function testResolveInvalidRejects() }); /** @var \React\Dns\RecordNotFoundException $exception */ - $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception); + $this->assertInstanceOf(RecordNotFoundException::class, $exception); $this->assertEquals('DNS query for example.invalid (A) returned an error response (Non-Existent Domain / NXDOMAIN)', $exception->getMessage()); $this->assertEquals(Message::RCODE_NAME_ERROR, $exception->getCode()); } @@ -138,7 +140,7 @@ public function testResolveCancelledRejectsImmediately() }); /** @var \React\Dns\Query\CancellationException $exception */ - $this->assertInstanceOf('React\Dns\Query\CancellationException', $exception); + $this->assertInstanceOf(CancellationException::class, $exception); $this->assertEquals('DNS query for google.com (A) has been cancelled', $exception->getMessage()); } @@ -157,7 +159,7 @@ public function testResolveAllInvalidTypeRejects() }); /** @var \React\Dns\RecordNotFoundException $exception */ - $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception); + $this->assertInstanceOf(RecordNotFoundException::class, $exception); $this->assertEquals('DNS query for google.com (PTR) did not return a valid answer (NOERROR / NODATA)', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); } diff --git a/tests/Protocol/ParserTest.php b/tests/Protocol/ParserTest.php index 8a66bd75..dfd9e1c7 100644 --- a/tests/Protocol/ParserTest.php +++ b/tests/Protocol/ParserTest.php @@ -28,12 +28,10 @@ public function testConvertTcpDumpToBinary($expected, $data) public function provideConvertTcpDumpToBinary() { - return [ - [chr(0x72).chr(0x62), "72 62"], - [chr(0x72).chr(0x62).chr(0x01).chr(0x00), "72 62 01 00"], - [chr(0x72).chr(0x62).chr(0x01).chr(0x00).chr(0x00).chr(0x01), "72 62 01 00 00 01"], - [chr(0x01).chr(0x00).chr(0x01), "01 00 01"], - ]; + yield [chr(0x72).chr(0x62), "72 62"]; + yield [chr(0x72).chr(0x62).chr(0x01).chr(0x00), "72 62 01 00"]; + yield [chr(0x72).chr(0x62).chr(0x01).chr(0x00).chr(0x00).chr(0x01), "72 62 01 00 00 01"]; + yield [chr(0x01).chr(0x00).chr(0x01), "01 00 01"]; } public function testParseRequest() @@ -768,7 +766,7 @@ public function testParseIncompleteQuestionThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -780,7 +778,7 @@ public function testParseIncompleteQuestionLabelThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -792,7 +790,7 @@ public function testParseIncompleteQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -804,7 +802,7 @@ public function testParseIncompleteOffsetPointerInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -816,7 +814,7 @@ public function testParseInvalidOffsetPointerInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -828,7 +826,7 @@ public function testParseInvalidOffsetPointerToSameLabelInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -840,7 +838,7 @@ public function testParseInvalidOffsetPointerToPreviousLabelInQuestionNameThrows $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -852,7 +850,7 @@ public function testParseInvalidOffsetPointerToStartOfMessageInQuestionNameThrow $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -866,7 +864,7 @@ public function testParseIncompleteAnswerFieldsThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -880,7 +878,7 @@ public function testParseMessageResponseWithIncompleteAuthorityRecordThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -894,7 +892,7 @@ public function testParseMessageResponseWithIncompleteAdditionalRecordThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -911,7 +909,7 @@ public function testParseIncompleteAnswerRecordDataThrows() $data = $this->convertTcpDumpToBinary($data); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parser->parseMessage($data); } @@ -923,7 +921,7 @@ public function testParseInvalidNSResponseWhereDomainNameIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -935,7 +933,7 @@ public function testParseInvalidAResponseWhereIPIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -947,7 +945,7 @@ public function testParseInvalidAAAAResponseWhereIPIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -960,7 +958,7 @@ public function testParseInvalidTXTResponseWhereTxtChunkExceedsLimit() $data .= "00 06"; // answer: rdlength 6 $data .= "06 68 65 6c 6c 6f 6f"; // answer: rdata length 6: helloo - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -973,7 +971,7 @@ public function testParseInvalidMXResponseWhereDomainNameIsIncomplete() $data .= "00 08"; // answer: rdlength 8 $data .= "00 0a 05 68 65 6c 6c 6f"; // answer: rdata priority 10: hello (missing label end) - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -986,7 +984,7 @@ public function testParseInvalidMXResponseWhereDomainNameIsMissing() $data .= "00 02"; // answer: rdlength 2 $data .= "00 0a"; // answer: rdata priority 10 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -999,7 +997,7 @@ public function testParseInvalidSRVResponseWhereDomainNameIsIncomplete() $data .= "00 0b"; // answer: rdlength 11 $data .= "00 0a 00 14 1F 90 04 74 65 73 74"; // answer: rdata priority 10, weight 20, port 8080 test (missing label end) - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1012,7 +1010,7 @@ public function testParseInvalidSRVResponseWhereDomainNameIsMissing() $data .= "00 06"; // answer: rdlength 6 $data .= "00 0a 00 14 1F 90"; // answer: rdata priority 10, weight 20, port 8080 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1025,7 +1023,7 @@ public function testParseInvalidSSHFPResponseWhereRecordIsTooSmall() $data .= "00 02"; // answer: rdlength 2 $data .= "01 01"; // answer: algorithm 1 (RSA), type 1 (SHA), missing fingerprint - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1038,7 +1036,7 @@ public function testParseInvalidOPTResponseWhereRecordIsTooSmall() $data .= "00 03"; // answer: rdlength 3 $data .= "00 00 00"; // answer: type 0, length incomplete - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1051,7 +1049,7 @@ public function testParseInvalidOPTResponseWhereRecordLengthDoesNotMatchOptType( $data .= "00 07"; // answer: rdlength 7 $data .= "00 0b 00 03 01 02 03"; // answer: type OPT_TCP_KEEPALIVE, length 3 instead of 2 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1065,7 +1063,7 @@ public function testParseInvalidSOAResponseWhereFlagsAreMissing() $data .= "02 6e 73 05 68 65 6c 6c 6f 00"; // answer: rdata ns.hello (mname) $data .= "01 65 05 68 65 6c 6c 6f 00"; // answer: rdata e.hello (rname) - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1077,7 +1075,7 @@ public function testParseInvalidCAAResponseEmtpyData() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1090,7 +1088,7 @@ public function testParseInvalidCAAResponseMissingValue() $data .= "00 07"; // answer: rdlength 22 $data .= "00 05 69 73 73 75 65"; // answer: rdata 0, issue - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } @@ -1104,7 +1102,7 @@ public function testParseInvalidCAAResponseIncompleteTag() $data .= "00 ff 69 73 73 75 65"; // answer: rdata 0, issue (incomplete due to invalid tag length) $data .= "68 65 6c 6c 6f"; // answer: hello - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->parseAnswer($data); } diff --git a/tests/Query/CachingExecutorTest.php b/tests/Query/CachingExecutorTest.php index 2a4250fc..f51299f3 100644 --- a/tests/Query/CachingExecutorTest.php +++ b/tests/Query/CachingExecutorTest.php @@ -2,22 +2,26 @@ namespace React\Tests\Dns\Query; +use React\Cache\CacheInterface; use React\Dns\Model\Message; +use React\Dns\Model\Record; use React\Dns\Query\CachingExecutor; +use React\Dns\Query\ExecutorInterface; use React\Dns\Query\Query; use React\Promise\Promise; -use React\Tests\Dns\TestCase; use React\Promise\Deferred; -use React\Dns\Model\Record; +use React\Tests\Dns\TestCase; +use function React\Promise\reject; +use function React\Promise\resolve; class CachingExecutorTest extends TestCase { public function testQueryWillReturnPendingPromiseWhenCacheIsPendingWithoutSendingQueryToFallbackExecutor() { - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $fallback = $this->createMock(ExecutorInterface::class); $fallback->expects($this->never())->method('query'); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); + $cache = $this->createMock(CacheInterface::class); $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(new Promise(function () { })); $executor = new CachingExecutor($fallback, $cache); @@ -33,11 +37,11 @@ public function testQueryWillReturnPendingPromiseWhenCacheReturnsMissAndWillSend { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $fallback = $this->createMock(ExecutorInterface::class); $fallback->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { })); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->willReturn(\React\Promise\resolve(null)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->willReturn(resolve(null)); $executor = new CachingExecutor($fallback, $cache); @@ -48,12 +52,12 @@ public function testQueryWillReturnPendingPromiseWhenCacheReturnsMissAndWillSend public function testQueryWillReturnResolvedPromiseWhenCacheReturnsHitWithoutSendingQueryToFallbackExecutor() { - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $fallback = $this->createMock(ExecutorInterface::class); $fallback->expects($this->never())->method('query'); $message = new Message(); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(\React\Promise\resolve($message)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(resolve($message)); $executor = new CachingExecutor($fallback, $cache); @@ -69,11 +73,11 @@ public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbac $message = new Message(); $message->answers[] = new Record('reactphp.org', Message::TYPE_A, Message::CLASS_IN, 3700, '127.0.0.1'); $message->answers[] = new Record('reactphp.org', Message::TYPE_A, Message::CLASS_IN, 3600, '127.0.0.1'); - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $fallback->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message)); + $fallback = $this->createMock(ExecutorInterface::class); + $fallback->expects($this->once())->method('query')->willReturn(resolve($message)); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(\React\Promise\resolve(null)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(resolve(null)); $cache->expects($this->once())->method('set')->with('reactphp.org:1:1', $message, 3600); $executor = new CachingExecutor($fallback, $cache); @@ -88,11 +92,11 @@ public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbac public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbackExecutorResolvesAndSaveMessageToCacheWithDefaultTtl() { $message = new Message(); - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $fallback->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message)); + $fallback = $this->createMock(ExecutorInterface::class); + $fallback->expects($this->once())->method('query')->willReturn(resolve($message)); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(\React\Promise\resolve(null)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(resolve(null)); $cache->expects($this->once())->method('set')->with('reactphp.org:1:1', $message, 60); $executor = new CachingExecutor($fallback, $cache); @@ -108,11 +112,11 @@ public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbac { $message = new Message(); $message->tc = true; - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $fallback->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message)); + $fallback = $this->createMock(ExecutorInterface::class); + $fallback->expects($this->once())->method('query')->willReturn(resolve($message)); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(\React\Promise\resolve(null)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(resolve(null)); $cache->expects($this->never())->method('set'); $executor = new CachingExecutor($fallback, $cache); @@ -128,11 +132,11 @@ public function testQueryWillReturnRejectedPromiseWhenCacheReturnsMissAndFallbac { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $fallback->expects($this->once())->method('query')->willReturn(\React\Promise\reject($exception = new \RuntimeException())); + $fallback = $this->createMock(ExecutorInterface::class); + $fallback->expects($this->once())->method('query')->willReturn(reject($exception = new \RuntimeException())); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $cache->expects($this->once())->method('get')->willReturn(\React\Promise\resolve(null)); + $cache = $this->createMock(CacheInterface::class); + $cache->expects($this->once())->method('get')->willReturn(resolve(null)); $executor = new CachingExecutor($fallback, $cache); @@ -143,11 +147,11 @@ public function testQueryWillReturnRejectedPromiseWhenCacheReturnsMissAndFallbac public function testCancelQueryWillReturnRejectedPromiseAndCancelPendingPromiseFromCache() { - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $fallback = $this->createMock(ExecutorInterface::class); $fallback->expects($this->never())->method('query'); $pending = new Promise(function () { }, $this->expectCallableOnce()); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); + $cache = $this->createMock(CacheInterface::class); $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn($pending); $executor = new CachingExecutor($fallback, $cache); @@ -163,18 +167,18 @@ public function testCancelQueryWillReturnRejectedPromiseAndCancelPendingPromiseF }); /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for reactphp.org (A) has been cancelled', $exception->getMessage()); } public function testCancelQueryWillReturnRejectedPromiseAndCancelPendingPromiseFromFallbackExecutorWhenCacheReturnsMiss() { $pending = new Promise(function () { }, $this->expectCallableOnce()); - $fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $fallback = $this->createMock(ExecutorInterface::class); $fallback->expects($this->once())->method('query')->willReturn($pending); $deferred = new Deferred(); - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); + $cache = $this->createMock(CacheInterface::class); $cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn($deferred->promise()); $executor = new CachingExecutor($fallback, $cache); @@ -191,7 +195,7 @@ public function testCancelQueryWillReturnRejectedPromiseAndCancelPendingPromiseF }); /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for reactphp.org (A) has been cancelled', $exception->getMessage()); } } diff --git a/tests/Query/CoopExecutorTest.php b/tests/Query/CoopExecutorTest.php index f705c4b9..52ab4104 100644 --- a/tests/Query/CoopExecutorTest.php +++ b/tests/Query/CoopExecutorTest.php @@ -1,11 +1,15 @@ getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->with($query)->willReturn($pending); $connector = new CoopExecutor($base); @@ -24,14 +28,14 @@ public function testQueryOnceWillResolveWhenBaseExecutorResolves() { $message = new Message(); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $base->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message)); + $base = $this->createMock(ExecutorInterface::class); + $base->expects($this->once())->method('query')->willReturn(resolve($message)); $connector = new CoopExecutor($base); $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); $promise = $connector->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then($this->expectCallableOnceWith($message)); } @@ -40,14 +44,14 @@ public function testQueryOnceWillRejectWhenBaseExecutorRejects() { $exception = new RuntimeException(); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $base->expects($this->once())->method('query')->willReturn(\React\Promise\reject($exception)); + $base = $this->createMock(ExecutorInterface::class); + $base->expects($this->once())->method('query')->willReturn(reject($exception)); $connector = new CoopExecutor($base); $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); $promise = $connector->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then(null, $this->expectCallableOnceWith($exception)); } @@ -57,7 +61,7 @@ public function testQueryTwoDifferentQueriesWillPassExactQueryToBaseExecutorTwic $pending = new Promise(function () { }); $query1 = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); $query2 = new Query('reactphp.org', Message::TYPE_AAAA, Message::CLASS_IN); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->exactly(2))->method('query')->withConsecutive( [$query1], [$query2] @@ -72,7 +76,7 @@ public function testQueryTwiceWillPassExactQueryToBaseExecutorOnceWhenQueryIsSti { $pending = new Promise(function () { }); $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->with($query)->willReturn($pending); $connector = new CoopExecutor($base); @@ -85,7 +89,7 @@ public function testQueryTwiceWillPassExactQueryToBaseExecutorTwiceWhenFirstQuer $deferred = new Deferred(); $pending = new Promise(function () { }); $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->exactly(2))->method('query')->with($query)->willReturnOnConsecutiveCalls($deferred->promise(), $pending); $connector = new CoopExecutor($base); @@ -102,7 +106,7 @@ public function testQueryTwiceWillPassExactQueryToBaseExecutorTwiceWhenFirstQuer $deferred = new Deferred(); $pending = new Promise(function () { }); $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->exactly(2))->method('query')->with($query)->willReturnOnConsecutiveCalls($deferred->promise(), $pending); $connector = new CoopExecutor($base); @@ -120,7 +124,7 @@ public function testCancelQueryWillCancelPromiseFromBaseExecutorAndReject() { $promise = new Promise(function () { }, $this->expectCallableOnce()); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->willReturn($promise); $connector = new CoopExecutor($base); @@ -135,7 +139,7 @@ public function testCancelQueryWillCancelPromiseFromBaseExecutorAndReject() }); /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for reactphp.org (A) has been cancelled', $exception->getMessage()); } @@ -143,7 +147,7 @@ public function testCancelOneQueryWhenOtherQueryIsStillPendingWillNotCancelPromi { $promise = new Promise(function () { }, $this->expectCallableNever()); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->willReturn($promise); $connector = new CoopExecutor($base); @@ -161,7 +165,7 @@ public function testCancelSecondQueryWhenFirstQueryIsStillPendingWillNotCancelPr { $promise = new Promise(function () { }, $this->expectCallableNever()); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->willReturn($promise); $connector = new CoopExecutor($base); @@ -179,7 +183,7 @@ public function testCancelAllPendingQueriesWillCancelPromiseFromBaseExecutorAndR { $promise = new Promise(function () { }, $this->expectCallableOnce()); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->willReturn($promise); $connector = new CoopExecutor($base); @@ -199,7 +203,7 @@ public function testQueryTwiceWillQueryBaseExecutorTwiceIfFirstQueryHasAlreadyBe $promise = new Promise(function () { }, $this->expectCallableOnce()); $pending = new Promise(function () { }); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls($promise, $pending); $connector = new CoopExecutor($base); @@ -225,7 +229,7 @@ public function testCancelQueryShouldNotCauseGarbageReferences() throw new \RuntimeException(); }); - $base = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $base = $this->createMock(ExecutorInterface::class); $base->expects($this->once())->method('query')->willReturn($deferred->promise()); $connector = new CoopExecutor($base); diff --git a/tests/Query/FallbackExecutorTest.php b/tests/Query/FallbackExecutorTest.php index b0dc053c..434aa71e 100644 --- a/tests/Query/FallbackExecutorTest.php +++ b/tests/Query/FallbackExecutorTest.php @@ -3,10 +3,14 @@ namespace React\Tests\Dns\Query; use React\Dns\Model\Message; +use React\Dns\Query\ExecutorInterface; use React\Dns\Query\FallbackExecutor; use React\Dns\Query\Query; use React\Promise\Promise; +use React\Promise\PromiseInterface; use React\Tests\Dns\TestCase; +use function React\Promise\reject; +use function React\Promise\resolve; class FallbackExecutorTest extends TestCase { @@ -14,16 +18,16 @@ public function testQueryWillReturnPendingPromiseWhenPrimaryExecutorIsStillPendi { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $primary = $this->createMock(ExecutorInterface::class); $primary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { })); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then($this->expectCallableNever(), $this->expectCallableNever()); } @@ -31,34 +35,34 @@ public function testQueryWillResolveWithMessageWhenPrimaryExecutorResolvesWithMe { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\resolve(new Message())); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(resolve(new Message())); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Dns\Model\Message')), $this->expectCallableNever()); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $promise->then($this->expectCallableOnceWith($this->isInstanceOf(Message::class)), $this->expectCallableNever()); } public function testQueryWillReturnPendingPromiseWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorIsStillPending() { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException())); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException())); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $secondary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { })); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then($this->expectCallableNever(), $this->expectCallableNever()); } @@ -66,43 +70,43 @@ public function testQueryWillResolveWithMessageWhenPrimaryExecutorRejectsPromise { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException())); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException())); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\resolve(new Message())); + $secondary = $this->createMock(ExecutorInterface::class); + $secondary->expects($this->once())->method('query')->with($query)->willReturn(resolve(new Message())); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Dns\Model\Message')), $this->expectCallableNever()); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $promise->then($this->expectCallableOnceWith($this->isInstanceOf(Message::class)), $this->expectCallableNever()); } public function testQueryWillRejectWithExceptionMessagesConcatenatedAfterColonWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorRejectsPromiseWithMessageWithColon() { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server A'))); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server A'))); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server B'))); + $secondary = $this->createMock(ExecutorInterface::class); + $secondary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server B'))); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf('Exception'))); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf(\Exception::class))); $exception = null; $promise->then(null, function ($reason) use (&$exception) { $exception = $reason; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for reactphp.org (A) failed: Unable to connect to DNS server A. Unable to connect to DNS server B', $exception->getMessage()); } @@ -110,25 +114,25 @@ public function testQueryWillRejectWithExceptionMessagesConcatenatedInFullWhenPr { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('Reason A'))); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException('Reason A'))); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('Reason B'))); + $secondary = $this->createMock(ExecutorInterface::class); + $secondary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException('Reason B'))); $executor = new FallbackExecutor($primary, $secondary); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf('Exception'))); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf(\Exception::class))); $exception = null; $promise->then(null, function ($reason) use (&$exception) { $exception = $reason; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Reason A. Reason B', $exception->getMessage()); } @@ -136,10 +140,10 @@ public function testCancelQueryWillReturnRejectedPromiseWithoutCallingSecondaryE { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $primary = $this->createMock(ExecutorInterface::class); $primary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); })); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $secondary->expects($this->never())->method('query'); $executor = new FallbackExecutor($primary, $secondary); @@ -147,7 +151,7 @@ public function testCancelQueryWillReturnRejectedPromiseWithoutCallingSecondaryE $promise = $executor->query($query); $promise->cancel(); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); } @@ -155,10 +159,10 @@ public function testCancelQueryWillReturnRejectedPromiseWhenPrimaryExecutorRejec { $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN); - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException())); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->with($query)->willReturn(reject(new \RuntimeException())); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $secondary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); })); $executor = new FallbackExecutor($primary, $secondary); @@ -166,7 +170,7 @@ public function testCancelQueryWillReturnRejectedPromiseWhenPrimaryExecutorRejec $promise = $executor->query($query); $promise->cancel(); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); } @@ -176,10 +180,10 @@ public function testCancelQueryShouldNotCauseGarbageReferencesWhenCancellingPrim $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $primary = $this->createMock(ExecutorInterface::class); $primary->expects($this->once())->method('query')->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); })); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $secondary->expects($this->never())->method('query'); $executor = new FallbackExecutor($primary, $secondary); @@ -203,10 +207,10 @@ public function testCancelQueryShouldNotCauseGarbageReferencesWhenCancellingSeco $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $primary->expects($this->once())->method('query')->willReturn(\React\Promise\reject(new \RuntimeException())); + $primary = $this->createMock(ExecutorInterface::class); + $primary->expects($this->once())->method('query')->willReturn(reject(new \RuntimeException())); - $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $secondary = $this->createMock(ExecutorInterface::class); $secondary->expects($this->once())->method('query')->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); })); $executor = new FallbackExecutor($primary, $secondary); diff --git a/tests/Query/HostsFileExecutorTest.php b/tests/Query/HostsFileExecutorTest.php index a4c489ca..5bf8f0f5 100644 --- a/tests/Query/HostsFileExecutorTest.php +++ b/tests/Query/HostsFileExecutorTest.php @@ -2,10 +2,12 @@ namespace React\Tests\Dns\Query; -use React\Tests\Dns\TestCase; +use React\Dns\Config\HostsFile; +use React\Dns\Model\Message; +use React\Dns\Query\ExecutorInterface; use React\Dns\Query\HostsFileExecutor; use React\Dns\Query\Query; -use React\Dns\Model\Message; +use React\Tests\Dns\TestCase; class HostsFileExecutorTest extends TestCase { @@ -18,8 +20,8 @@ class HostsFileExecutorTest extends TestCase */ public function setUpMocks() { - $this->hosts = $this->getMockBuilder('React\Dns\Config\HostsFile')->disableOriginalConstructor()->getMock(); - $this->fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $this->hosts = $this->createMock(HostsFile::class); + $this->fallback = $this->createMock(ExecutorInterface::class); $this->executor = new HostsFileExecutor($this->hosts, $this->fallback); } diff --git a/tests/Query/RetryExecutorTest.php b/tests/Query/RetryExecutorTest.php index 0445b757..c7695c76 100644 --- a/tests/Query/RetryExecutorTest.php +++ b/tests/Query/RetryExecutorTest.php @@ -2,14 +2,16 @@ namespace React\Tests\Dns\Query; -use React\Tests\Dns\TestCase; -use React\Dns\Query\RetryExecutor; -use React\Dns\Query\Query; use React\Dns\Model\Message; -use React\Dns\Query\TimeoutException; use React\Dns\Model\Record; -use React\Promise\Deferred; use React\Dns\Query\CancellationException; +use React\Dns\Query\ExecutorInterface; +use React\Dns\Query\Query; +use React\Dns\Query\RetryExecutor; +use React\Dns\Query\TimeoutException; +use React\Promise\Deferred; +use React\Promise\PromiseInterface; +use React\Tests\Dns\TestCase; use function React\Promise\reject; use function React\Promise\resolve; @@ -25,7 +27,7 @@ public function queryShouldDelegateToDecoratedExecutor() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnValue($this->expectPromiseOnce())); $retryExecutor = new RetryExecutor($executor, 2); @@ -46,7 +48,7 @@ public function queryShouldRetryQueryOnTimeout() $executor ->expects($this->exactly(2)) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->onConsecutiveCalls( $this->returnCallback(function ($query) { return reject(new TimeoutException("timeout")); @@ -60,7 +62,7 @@ public function queryShouldRetryQueryOnTimeout() $callback ->expects($this->once()) ->method('__invoke') - ->with($this->isInstanceOf('React\Dns\Model\Message')); + ->with($this->isInstanceOf(Message::class)); $errorback = $this->expectCallableNever(); @@ -80,7 +82,7 @@ public function queryShouldStopRetryingAfterSomeAttempts() $executor ->expects($this->exactly(3)) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { return reject(new TimeoutException("timeout")); })); @@ -96,10 +98,10 @@ public function queryShouldStopRetryingAfterSomeAttempts() }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for igor.io (A) failed: too many retries', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); - $this->assertInstanceOf('React\Dns\Query\TimeoutException', $exception->getPrevious()); + $this->assertInstanceOf(TimeoutException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } @@ -113,7 +115,7 @@ public function queryShouldForwardNonTimeoutErrors() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { return reject(new \Exception); })); @@ -124,7 +126,7 @@ public function queryShouldForwardNonTimeoutErrors() $errorback ->expects($this->once()) ->method('__invoke') - ->with($this->isInstanceOf('Exception')); + ->with($this->isInstanceOf(\Exception::class)); $retryExecutor = new RetryExecutor($executor, 2); @@ -144,7 +146,7 @@ public function queryShouldCancelQueryOnCancel() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) use (&$cancelled) { $deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) { ++$cancelled; @@ -180,7 +182,7 @@ public function queryShouldCancelSecondQueryOnCancel() $executor ->expects($this->exactly(2)) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->onConsecutiveCalls( $this->returnValue($deferred->promise()), $this->returnCallback(function ($query) use (&$cancelled) { @@ -222,7 +224,7 @@ public function queryShouldNotCauseGarbageReferencesOnSuccess() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->willReturn(resolve($this->createStandardResponse())); $retryExecutor = new RetryExecutor($executor, 0); @@ -251,7 +253,7 @@ public function queryShouldNotCauseGarbageReferencesOnTimeoutErrors() $executor ->expects($this->any()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->willReturn(reject(new TimeoutException("timeout"))); $retryExecutor = new RetryExecutor($executor, 0); @@ -286,7 +288,7 @@ public function queryShouldNotCauseGarbageReferencesOnCancellation() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->willReturn($deferred->promise()); $retryExecutor = new RetryExecutor($executor, 0); @@ -317,7 +319,7 @@ public function queryShouldNotCauseGarbageReferencesOnNonTimeoutErrors() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { return reject(new \Exception); })); @@ -349,12 +351,12 @@ protected function expectPromiseOnce($return = null) protected function createExecutorMock() { - return $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + return $this->createMock(ExecutorInterface::class); } protected function createPromiseMock() { - return $this->getMockBuilder('React\Promise\PromiseInterface')->getMock(); + return $this->createMock(PromiseInterface::class); } protected function createStandardResponse() diff --git a/tests/Query/SelectiveTransportExecutorTest.php b/tests/Query/SelectiveTransportExecutorTest.php index c38dbeb2..5f439f3b 100644 --- a/tests/Query/SelectiveTransportExecutorTest.php +++ b/tests/Query/SelectiveTransportExecutorTest.php @@ -3,11 +3,14 @@ namespace React\Tests\Dns\Query; use React\Dns\Model\Message; +use React\Dns\Query\ExecutorInterface; use React\Dns\Query\Query; use React\Dns\Query\SelectiveTransportExecutor; use React\Promise\Deferred; use React\Promise\Promise; use React\Tests\Dns\TestCase; +use function React\Promise\reject; +use function React\Promise\resolve; class SelectiveTransportExecutorTest extends TestCase { @@ -20,8 +23,8 @@ class SelectiveTransportExecutorTest extends TestCase */ public function setUpMocks() { - $this->datagram = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); - $this->stream = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $this->datagram = $this->createMock(ExecutorInterface::class); + $this->stream = $this->createMock(ExecutorInterface::class); $this->executor = new SelectiveTransportExecutor($this->datagram, $this->stream); } @@ -36,7 +39,7 @@ public function testQueryResolvesWhenDatagramTransportResolvesWithoutUsingStream ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\resolve($response)); + ->willReturn(resolve($response)); $this->stream ->expects($this->never()) @@ -57,13 +60,13 @@ public function testQueryResolvesWhenStreamTransportResolvesAfterDatagramTranspo ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); + ->willReturn(reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); $this->stream ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\resolve($response)); + ->willReturn(resolve($response)); $promise = $this->executor->query($query); @@ -78,7 +81,7 @@ public function testQueryRejectsWhenDatagramTransportRejectsWithRuntimeException ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException())); + ->willReturn(reject(new \RuntimeException())); $this->stream ->expects($this->never()) @@ -97,13 +100,13 @@ public function testQueryRejectsWhenStreamTransportRejectsAfterDatagramTransport ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); + ->willReturn(reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); $this->stream ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException())); + ->willReturn(reject(new \RuntimeException())); $promise = $this->executor->query($query); @@ -216,13 +219,13 @@ public function testRejectedPromiseAfterTruncatedResponseShouldNotCreateAnyGarba ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); + ->willReturn(reject(new \RuntimeException('', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90))); $this->stream ->expects($this->once()) ->method('query') ->with($query) - ->willReturn(\React\Promise\reject(new \RuntimeException())); + ->willReturn(reject(new \RuntimeException())); while (gc_collect_cycles()) { // collect all garbage cycles diff --git a/tests/Query/TcpTransportExecutorTest.php b/tests/Query/TcpTransportExecutorTest.php index cb83bfa6..e65e347d 100644 --- a/tests/Query/TcpTransportExecutorTest.php +++ b/tests/Query/TcpTransportExecutorTest.php @@ -5,10 +5,17 @@ use React\Dns\Model\Message; use React\Dns\Protocol\BinaryDumper; use React\Dns\Protocol\Parser; +use React\Dns\Query\CancellationException; use React\Dns\Query\Query; use React\Dns\Query\TcpTransportExecutor; use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; +use React\EventLoop\TimerInterface; +use React\Promise\PromiseInterface; use React\Tests\Dns\TestCase; +use function React\Async\await; +use function React\Promise\Timer\sleep; +use function React\Promise\Timer\timeout; class TcpTransportExecutorTest extends TestCase { @@ -19,7 +26,7 @@ class TcpTransportExecutorTest extends TestCase */ public function testCtorShouldAcceptNameserverAddresses($input, $expected) { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $executor = new TcpTransportExecutor($input, $loop); @@ -32,31 +39,29 @@ public function testCtorShouldAcceptNameserverAddresses($input, $expected) public static function provideDefaultPortProvider() { - return [ - [ - '8.8.8.8', - 'tcp://8.8.8.8:53' - ], - [ - '1.2.3.4:5', - 'tcp://1.2.3.4:5' - ], - [ - 'tcp://1.2.3.4', - 'tcp://1.2.3.4:53' - ], - [ - 'tcp://1.2.3.4:53', - 'tcp://1.2.3.4:53' - ], - [ - '::1', - 'tcp://[::1]:53' - ], - [ - '[::1]:53', - 'tcp://[::1]:53' - ] + yield [ + '8.8.8.8', + 'tcp://8.8.8.8:53' + ]; + yield [ + '1.2.3.4:5', + 'tcp://1.2.3.4:5' + ]; + yield [ + 'tcp://1.2.3.4', + 'tcp://1.2.3.4:53' + ]; + yield [ + 'tcp://1.2.3.4:53', + 'tcp://1.2.3.4:53' + ]; + yield [ + '::1', + 'tcp://[::1]:53' + ]; + yield [ + '[::1]:53', + 'tcp://[::1]:53' ]; } @@ -68,36 +73,36 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $ref->setAccessible(true); $loop = $ref->getValue($executor); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new TcpTransportExecutor('///', $loop); } public function testCtorShouldThrowWhenNameserverAddressContainsHostname() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new TcpTransportExecutor('localhost', $loop); } public function testCtorShouldThrowWhenNameserverSchemeIsInvalid() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new TcpTransportExecutor('udp://1.2.3.4', $loop); } public function testQueryRejectsIfMessageExceedsMaximumMessageSize() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addWriteStream'); $executor = new TcpTransportExecutor('8.8.8.8:53', $loop); @@ -111,13 +116,13 @@ public function testQueryRejectsIfMessageExceedsMaximumMessageSize() }); /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for '. $query->name . ' (A) failed: Query too large for TCP transport', $exception->getMessage()); } public function testQueryRejectsIfServerConnectionFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addWriteStream'); $executor = new TcpTransportExecutor('::1', $loop); @@ -135,19 +140,19 @@ public function testQueryRejectsIfServerConnectionFails() }); /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Unable to connect to DNS server /// (Failed to parse address "///")', $exception->getMessage()); } public function testQueryRejectsOnCancellationWithoutClosingSocketButStartsIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->never())->method('removeWriteStream'); $loop->expects($this->never())->method('addReadStream'); $loop->expects($this->never())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything())->willReturn($timer); $loop->expects($this->never())->method('cancelTimer'); @@ -166,19 +171,19 @@ public function testQueryRejectsOnCancellationWithoutClosingSocketButStartsIdleT }); /** @var \React\Dns\Query\CancellationException $exception */ - $this->assertInstanceOf('React\Dns\Query\CancellationException', $exception); + $this->assertInstanceOf(CancellationException::class, $exception); $this->assertEquals('DNS query for google.com (A) has been cancelled', $exception->getMessage()); } public function testTriggerIdleTimerAfterQueryRejectedOnCancellationWillCloseSocket() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->never())->method('addReadStream'); $loop->expects($this->never())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $timerCallback = null; $loop->expects($this->once())->method('addTimer')->with(0.001, $this->callback(function ($cb) use (&$timerCallback) { $timerCallback = $cb; @@ -195,7 +200,7 @@ public function testTriggerIdleTimerAfterQueryRejectedOnCancellationWillCloseSoc $promise = $executor->query($query); $promise->cancel(); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $promise->then(null, $this->expectCallableOnce()); // trigger idle timer @@ -205,7 +210,7 @@ public function testTriggerIdleTimerAfterQueryRejectedOnCancellationWillCloseSoc public function testQueryRejectsOnCancellationWithoutClosingSocketAndWithoutStartingIdleTimerWhenOtherQueryIsStillPending() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->never())->method('removeWriteStream'); $loop->expects($this->never())->method('addReadStream'); @@ -230,7 +235,7 @@ public function testQueryRejectsOnCancellationWithoutClosingSocketAndWithoutStar public function testQueryAgainAfterPreviousWasCancelledReusesExistingSocket() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->never())->method('removeWriteStream'); $loop->expects($this->never())->method('addReadStream'); @@ -262,20 +267,20 @@ function ($e) use (&$exception) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.01)); + await(sleep(0.01)); if ($exception === null) { - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); } /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Unable to connect to DNS server tcp://127.0.0.1:1 (Connection refused)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); } public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneChunk() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->never())->method('removeWriteStream'); @@ -318,7 +323,7 @@ public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneCh $this->markTestSkipped('Skipped on macOS due to possible race condition'); } - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->never())->method('removeWriteStream'); @@ -352,7 +357,7 @@ public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneCh public function testQueryRejectsWhenClientKeepsSendingWhenServerClosesSocketWithoutCallingCustomErrorHandler() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->once())->method('removeWriteStream'); @@ -400,8 +405,8 @@ public function testQueryRejectsWhenClientKeepsSendingWhenServerClosesSocketWith $this->assertNull($error); // expect EPIPE (Broken pipe), except for macOS kernel race condition - $this->setExpectedException( - 'RuntimeException', + $this->expectException( + \RuntimeException::class, 'Unable to send query to DNS server tcp://' . $address . ' (', defined('SOCKET_EPIPE') ? (PHP_OS !== 'Darwin' || $writePending ? SOCKET_EPIPE : SOCKET_EPROTOTYPE) : null ); @@ -432,13 +437,13 @@ function ($e) use (&$exception) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.01)); + await(sleep(0.01)); if ($exception === null) { - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); } /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Connection to DNS server tcp://' . $address . ' lost', $exception->getMessage()); } @@ -470,7 +475,7 @@ function ($e) use (&$wait) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); $this->assertTrue($wait); $this->assertNotNull($client); @@ -506,7 +511,7 @@ function ($e) use (&$wait) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); $this->assertTrue($wait); $this->assertNotNull($client); @@ -541,13 +546,13 @@ function ($e) use (&$exception) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.01)); + await(sleep(0.01)); if ($exception === null) { - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); } /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Invalid message received from DNS server tcp://' . $address, $exception->getMessage()); } @@ -593,13 +598,13 @@ function ($e) use (&$exception) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.01)); + await(sleep(0.01)); if ($exception === null) { - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); } /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Invalid response message received from DNS server tcp://' . $address, $exception->getMessage()); } @@ -645,13 +650,13 @@ function ($e) use (&$exception) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.01)); + await(sleep(0.01)); if ($exception === null) { - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); } /** @var \RuntimeException $exception */ - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('DNS query for google.com (A) failed: Invalid response message received from DNS server tcp://' . $address, $exception->getMessage()); } @@ -680,14 +685,14 @@ public function testQueryResolvesIfServerSendsValidResponse() $query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN); $promise = $executor->query($query); - $response = \React\Async\await(\React\Promise\Timer\timeout($promise, 0.2)); + $response = await(timeout($promise, 0.2)); - $this->assertInstanceOf('React\Dns\Model\Message', $response); + $this->assertInstanceOf(Message::class, $response); } public function testQueryRejectsIfSocketIsClosedAfterPreviousQueryThatWasStillPending() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->exactly(2))->method('addWriteStream'); $loop->expects($this->exactly(2))->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); @@ -721,7 +726,7 @@ public function testQueryRejectsIfSocketIsClosedAfterPreviousQueryThatWasStillPe public function testQueryResolvesIfServerSendsBackResponseMessageAndWillStartIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); @@ -754,13 +759,13 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAndWillStartIdl public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancellingQueryAndWillStartIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->never())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything())->willReturn($timer); $loop->expects($this->never())->method('cancelTimer'); @@ -789,7 +794,7 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancelling public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancellingOtherQueryAndWillStartIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); @@ -825,13 +830,13 @@ public function testQueryResolvesIfServerSendsBackResponseMessageAfterCancelling public function testTriggerIdleTimerAfterPreviousQueryResolvedWillCloseIdleSocketConnection() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->once())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $timerCallback = null; $loop->expects($this->once())->method('addTimer')->with(0.001, $this->callback(function ($cb) use (&$timerCallback) { $timerCallback = $cb; @@ -867,13 +872,13 @@ public function testTriggerIdleTimerAfterPreviousQueryResolvedWillCloseIdleSocke public function testClosingConnectionAfterPreviousQueryResolvedWillCancelIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->once())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); @@ -905,13 +910,13 @@ public function testClosingConnectionAfterPreviousQueryResolvedWillCancelIdleTim public function testQueryAgainAfterPreviousQueryResolvedWillReuseSocketAndCancelIdleTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->exactly(2))->method('addWriteStream'); $loop->expects($this->once())->method('removeWriteStream'); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->never())->method('removeReadStream'); - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); diff --git a/tests/Query/TimeoutExecutorTest.php b/tests/Query/TimeoutExecutorTest.php index 0ff75e9c..96b355ce 100644 --- a/tests/Query/TimeoutExecutorTest.php +++ b/tests/Query/TimeoutExecutorTest.php @@ -4,9 +4,12 @@ use React\Dns\Model\Message; use React\Dns\Query\CancellationException; +use React\Dns\Query\ExecutorInterface; use React\Dns\Query\Query; use React\Dns\Query\TimeoutException; use React\Dns\Query\TimeoutExecutor; +use React\EventLoop\LoopInterface; +use React\EventLoop\TimerInterface; use React\Promise\Deferred; use React\Tests\Dns\TestCase; use function React\Promise\reject; @@ -23,9 +26,9 @@ class TimeoutExecutorTest extends TestCase */ public function setUpExecutor() { - $this->wrapped = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + $this->wrapped = $this->createMock(ExecutorInterface::class); - $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->loop = $this->createMock(LoopInterface::class); $this->executor = new TimeoutExecutor($this->wrapped, 5.0, $this->loop); } @@ -38,12 +41,12 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $ref->setAccessible(true); $loop = $ref->getValue($executor); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testCancellingPromiseWillCancelWrapped() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer); $this->loop->expects($this->once())->method('cancelTimer')->with($timer); @@ -89,7 +92,7 @@ public function testResolvesPromiseWithoutStartingTimerWhenWrappedReturnsResolve public function testResolvesPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatResolves() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer); $this->loop->expects($this->once())->method('cancelTimer')->with($timer); @@ -125,7 +128,7 @@ public function testRejectsPromiseWithoutStartingTimerWhenWrappedReturnsRejected public function testRejectsPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatRejects() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer); $this->loop->expects($this->once())->method('cancelTimer')->with($timer); @@ -146,7 +149,7 @@ public function testRejectsPromiseAfterCancellingTimerWhenWrappedReturnsPendingP public function testRejectsPromiseAndCancelsPendingQueryWhenTimeoutTriggers() { $timerCallback = null; - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->callback(function ($callback) use (&$timerCallback) { $timerCallback = $callback; return true; @@ -183,7 +186,7 @@ public function testRejectsPromiseAndCancelsPendingQueryWhenTimeoutTriggers() }); assert($exception instanceof TimeoutException); - $this->assertInstanceOf('React\Dns\Query\TimeoutException', $exception); + $this->assertInstanceOf(TimeoutException::class, $exception); $this->assertEquals('DNS query for igor.io (A) timed out' , $exception->getMessage()); } } diff --git a/tests/Query/UdpTransportExecutorTest.php b/tests/Query/UdpTransportExecutorTest.php index 84cc069a..41b27a9a 100644 --- a/tests/Query/UdpTransportExecutorTest.php +++ b/tests/Query/UdpTransportExecutorTest.php @@ -5,10 +5,16 @@ use React\Dns\Model\Message; use React\Dns\Protocol\BinaryDumper; use React\Dns\Protocol\Parser; +use React\Dns\Query\CancellationException; use React\Dns\Query\Query; use React\Dns\Query\UdpTransportExecutor; use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; +use React\Promise\PromiseInterface; use React\Tests\Dns\TestCase; +use function React\Async\await; +use function React\Promise\Timer\sleep; +use function React\Promise\Timer\timeout; class UdpTransportExecutorTest extends TestCase { @@ -19,7 +25,7 @@ class UdpTransportExecutorTest extends TestCase */ public function testCtorShouldAcceptNameserverAddresses($input, $expected) { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $executor = new UdpTransportExecutor($input, $loop); @@ -32,31 +38,29 @@ public function testCtorShouldAcceptNameserverAddresses($input, $expected) public static function provideDefaultPortProvider() { - return [ - [ - '8.8.8.8', - 'udp://8.8.8.8:53' - ], - [ - '1.2.3.4:5', - 'udp://1.2.3.4:5' - ], - [ - 'udp://1.2.3.4', - 'udp://1.2.3.4:53' - ], - [ - 'udp://1.2.3.4:53', - 'udp://1.2.3.4:53' - ], - [ - '::1', - 'udp://[::1]:53' - ], - [ - '[::1]:53', - 'udp://[::1]:53' - ] + yield [ + '8.8.8.8', + 'udp://8.8.8.8:53' + ]; + yield [ + '1.2.3.4:5', + 'udp://1.2.3.4:5' + ]; + yield [ + 'udp://1.2.3.4', + 'udp://1.2.3.4:53' + ]; + yield [ + 'udp://1.2.3.4:53', + 'udp://1.2.3.4:53' + ]; + yield [ + '::1', + 'udp://[::1]:53' + ]; + yield [ + '[::1]:53', + 'udp://[::1]:53' ]; } @@ -68,36 +72,36 @@ public function testCtorWithoutLoopShouldAssignDefaultLoop() $ref->setAccessible(true); $loop = $ref->getValue($executor); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new UdpTransportExecutor('///', $loop); } public function testCtorShouldThrowWhenNameserverAddressContainsHostname() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new UdpTransportExecutor('localhost', $loop); } public function testCtorShouldThrowWhenNameserverSchemeIsInvalid() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); new UdpTransportExecutor('tcp://1.2.3.4', $loop); } public function testQueryRejectsIfMessageExceedsUdpSize() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); $executor = new UdpTransportExecutor('8.8.8.8:53', $loop); @@ -105,15 +109,15 @@ public function testQueryRejectsIfMessageExceedsUdpSize() $query = new Query('google.' . str_repeat('.com', 200), Message::TYPE_A, Message::CLASS_IN); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $exception = null; $promise->then(null, function ($reason) use (&$exception) { $exception = $reason; }); - $this->setExpectedException( - 'RuntimeException', + $this->expectException( + \RuntimeException::class, 'DNS query for ' . $query->name . ' (A) failed: Query too large for UDP transport', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90 ); @@ -122,7 +126,7 @@ public function testQueryRejectsIfMessageExceedsUdpSize() public function testQueryRejectsIfServerConnectionFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); $executor = new UdpTransportExecutor('::1', $loop); @@ -134,15 +138,15 @@ public function testQueryRejectsIfServerConnectionFails() $query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN); $promise = $executor->query($query); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $exception = null; $promise->then(null, function ($reason) use (&$exception) { $exception = $reason; }); - $this->setExpectedException( - 'RuntimeException', + $this->expectException( + \RuntimeException::class, 'DNS query for google.com (A) failed: Unable to connect to DNS server /// (Failed to parse address "///")' ); throw $exception; @@ -150,7 +154,7 @@ public function testQueryRejectsIfServerConnectionFails() public function testQueryRejectsIfSendToServerFailsAfterConnectionWithoutCallingCustomErrorHandler() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); $executor = new UdpTransportExecutor('0.0.0.0', $loop); @@ -171,7 +175,7 @@ public function testQueryRejectsIfSendToServerFailsAfterConnectionWithoutCalling restore_error_handler(); $this->assertNull($error); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $exception = null; $promise->then(null, function ($reason) use (&$exception) { @@ -179,8 +183,8 @@ public function testQueryRejectsIfSendToServerFailsAfterConnectionWithoutCalling }); // ECONNREFUSED (Connection refused) on Linux, EMSGSIZE (Message too long) on macOS - $this->setExpectedException( - 'RuntimeException', + $this->expectException( + \RuntimeException::class, 'DNS query for ' . $query->name . ' (A) failed: Unable to send query to DNS server udp://0.0.0.0:53 (' ); throw $exception; @@ -190,7 +194,7 @@ public function testQueryKeepsPendingIfReadFailsBecauseServerRefusesConnection() { $socket = null; $callback = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream')->with($this->callback(function ($ref) use (&$socket) { $socket = $ref; return true; @@ -207,7 +211,7 @@ public function testQueryKeepsPendingIfReadFailsBecauseServerRefusesConnection() $this->assertNotNull($socket); $callback($socket); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); $pending = true; $promise->then(function () use (&$pending) { @@ -224,7 +228,7 @@ public function testQueryKeepsPendingIfReadFailsBecauseServerRefusesConnection() */ public function testQueryRejectsOnCancellation() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->once())->method('removeReadStream'); @@ -240,7 +244,7 @@ public function testQueryRejectsOnCancellation() }); /** @var \React\Dns\Query\CancellationException $exception */ - $this->assertInstanceOf('React\Dns\Query\CancellationException', $exception); + $this->assertInstanceOf(CancellationException::class, $exception); $this->assertEquals('DNS query for google.com (A) has been cancelled', $exception->getMessage()); } @@ -268,7 +272,7 @@ function ($e) use (&$wait) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); $this->assertTrue($wait); $promise->cancel(); @@ -305,7 +309,7 @@ function ($e) use (&$wait) { } ); - \React\Async\await(\React\Promise\Timer\sleep(0.2)); + await(sleep(0.2)); $this->assertTrue($wait); $promise->cancel(); @@ -336,12 +340,12 @@ public function testQueryRejectsIfServerSendsTruncatedResponse() $promise = $executor->query($query); - $this->setExpectedException( - 'RuntimeException', + $this->expectException( + \RuntimeException::class, 'DNS query for google.com (A) failed: The DNS server udp://' . $address . ' returned a truncated result for a UDP query', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90 ); - \React\Async\await(\React\Promise\Timer\timeout($promise, 0.1)); + await(timeout($promise, 0.1)); } public function testQueryResolvesIfServerSendsValidResponse() @@ -367,8 +371,8 @@ public function testQueryResolvesIfServerSendsValidResponse() $query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN); $promise = $executor->query($query); - $response = \React\Async\await(\React\Promise\Timer\timeout($promise, 0.2)); + $response = await(timeout($promise, 0.2)); - $this->assertInstanceOf('React\Dns\Model\Message', $response); + $this->assertInstanceOf(Message::class, $response); } } diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index af758b51..c50f80f3 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -2,9 +2,21 @@ namespace React\Tests\Dns\Resolver; +use React\Cache\ArrayCache; +use React\Cache\CacheInterface; use React\Dns\Config\Config; +use React\Dns\Query\CachingExecutor; +use React\Dns\Query\CoopExecutor; use React\Dns\Query\HostsFileExecutor; +use React\Dns\Query\FallbackExecutor; +use React\Dns\Query\RetryExecutor; +use React\Dns\Query\SelectiveTransportExecutor; +use React\Dns\Query\TcpTransportExecutor; +use React\Dns\Query\TimeoutExecutor; +use React\Dns\Query\UdpTransportExecutor; use React\Dns\Resolver\Factory; +use React\Dns\Resolver\Resolver; +use React\EventLoop\LoopInterface; use React\Tests\Dns\TestCase; class FactoryTest extends TestCase @@ -15,34 +27,34 @@ public function createShouldCreateResolver() $factory = new Factory(); $resolver = $factory->create('8.8.8.8:53'); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); } /** @test */ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); $resolver = $factory->create('8.8.8.8:53', $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $selectiveExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\SelectiveTransportExecutor', $selectiveExecutor); + $this->assertInstanceOf(SelectiveTransportExecutor::class, $selectiveExecutor); // udp below: @@ -50,13 +62,13 @@ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExe $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($selectiveExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $udpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\UdpTransportExecutor', $udpExecutor); + $this->assertInstanceOf(UdpTransportExecutor::class, $udpExecutor); // tcp below: @@ -64,85 +76,85 @@ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExe $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($selectiveExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); } /** @test */ public function createWithUdpSchemeShouldCreateResolverWithUdpExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); $resolver = $factory->create('udp://8.8.8.8:53', $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $udpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\UdpTransportExecutor', $udpExecutor); + $this->assertInstanceOf(UdpTransportExecutor::class, $udpExecutor); } /** @test */ public function createWithTcpSchemeShouldCreateResolverWithTcpExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); $resolver = $factory->create('tcp://8.8.8.8:53', $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); } /** @test */ public function createWithConfigWithTcpNameserverSchemeShouldCreateResolverWithTcpExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $config = new Config(); $config->nameservers[] = 'tcp://8.8.8.8:53'; @@ -150,35 +162,35 @@ public function createWithConfigWithTcpNameserverSchemeShouldCreateResolverWithT $factory = new Factory(); $resolver = $factory->create($config, $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); } /** @test */ public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResolverWithFallbackExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $config = new Config(); $config->nameservers[] = 'tcp://8.8.8.8:53'; @@ -187,35 +199,35 @@ public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResol $factory = new Factory(); $resolver = $factory->create($config, $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $fallbackExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor); + $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); $ref->setAccessible(true); @@ -227,13 +239,13 @@ public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResol $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); $ref->setAccessible(true); @@ -245,7 +257,7 @@ public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResol /** @test */ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateResolverWithNestedFallbackExecutorStack() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $config = new Config(); $config->nameservers[] = 'tcp://8.8.8.8:53'; @@ -255,35 +267,35 @@ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateRes $factory = new Factory(); $resolver = $factory->create($config, $loop); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $coopExecutor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor); + $this->assertInstanceOf(CoopExecutor::class, $coopExecutor); $ref = new \ReflectionProperty($coopExecutor, 'executor'); $ref->setAccessible(true); $retryExecutor = $ref->getValue($coopExecutor); - $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor); + $this->assertInstanceOf(RetryExecutor::class, $retryExecutor); $ref = new \ReflectionProperty($retryExecutor, 'executor'); $ref->setAccessible(true); $fallbackExecutor = $ref->getValue($retryExecutor); - $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor); + $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); $ref->setAccessible(true); @@ -295,19 +307,19 @@ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateRes $ref->setAccessible(true); $fallbackExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor); + $this->assertInstanceOf(FallbackExecutor::class, $fallbackExecutor); $ref = new \ReflectionProperty($fallbackExecutor, 'executor'); $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); $ref->setAccessible(true); @@ -319,13 +331,13 @@ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateRes $ref->setAccessible(true); $timeoutExecutor = $ref->getValue($fallbackExecutor); - $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor); + $this->assertInstanceOf(TimeoutExecutor::class, $timeoutExecutor); $ref = new \ReflectionProperty($timeoutExecutor, 'executor'); $ref->setAccessible(true); $tcpExecutor = $ref->getValue($timeoutExecutor); - $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); + $this->assertInstanceOf(TcpTransportExecutor::class, $tcpExecutor); $ref = new \ReflectionProperty($tcpExecutor, 'nameserver'); $ref->setAccessible(true); @@ -337,36 +349,36 @@ public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateRes /** @test */ public function createShouldThrowWhenNameserverIsInvalid() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $factory->create('///', $loop); } /** @test */ public function createShouldThrowWhenConfigHasNoNameservers() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); - $this->setExpectedException('UnderflowException'); + $this->expectException(\UnderflowException::class); $factory->create(new Config(), $loop); } /** @test */ public function createShouldThrowWhenConfigHasInvalidNameserver() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); $config = new Config(); $config->nameservers[] = '///'; - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $factory->create($config, $loop); } @@ -376,25 +388,25 @@ public function createCachedShouldCreateResolverWithCachingExecutor() $factory = new Factory(); $resolver = $factory->createCached('8.8.8.8:53'); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $executor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CachingExecutor', $executor); + $this->assertInstanceOf(CachingExecutor::class, $executor); $cache = $this->getCachingExecutorPrivateMemberValue($executor, 'cache'); - $this->assertInstanceOf('React\Cache\ArrayCache', $cache); + $this->assertInstanceOf(ArrayCache::class, $cache); } /** @test */ public function createCachedShouldCreateResolverWithCachingExecutorWithCustomCache() { - $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $cache = $this->createMock(CacheInterface::class); + $loop = $this->createMock(LoopInterface::class); $factory = new Factory(); $resolver = $factory->createCached('8.8.8.8:53', $loop, $cache); - $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $this->assertInstanceOf(Resolver::class, $resolver); $executor = $this->getResolverPrivateExecutor($resolver); - $this->assertInstanceOf('React\Dns\Query\CachingExecutor', $executor); + $this->assertInstanceOf(CachingExecutor::class, $executor); $cacheProperty = $this->getCachingExecutorPrivateMemberValue($executor, 'cache'); $this->assertSame($cache, $cacheProperty); } @@ -405,7 +417,7 @@ 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('React\Dns\Query\HostsFileExecutor', 'fallback'); + $reflector = new \ReflectionProperty(HostsFileExecutor::class, 'fallback'); $reflector->setAccessible(true); $executor = $reflector->getValue($executor); @@ -416,14 +428,14 @@ private function getResolverPrivateExecutor($resolver) private function getResolverPrivateMemberValue($resolver, $field) { - $reflector = new \ReflectionProperty('React\Dns\Resolver\Resolver', $field); + $reflector = new \ReflectionProperty(Resolver::class, $field); $reflector->setAccessible(true); return $reflector->getValue($resolver); } private function getCachingExecutorPrivateMemberValue($resolver, $field) { - $reflector = new \ReflectionProperty('React\Dns\Query\CachingExecutor', $field); + $reflector = new \ReflectionProperty(CachingExecutor::class, $field); $reflector->setAccessible(true); return $reflector->getValue($resolver); } diff --git a/tests/Resolver/ResolveAliasesTest.php b/tests/Resolver/ResolveAliasesTest.php index 61d57a99..013aa048 100644 --- a/tests/Resolver/ResolveAliasesTest.php +++ b/tests/Resolver/ResolveAliasesTest.php @@ -2,10 +2,12 @@ namespace React\Tests\Dns\Resolver; -use React\Tests\Dns\TestCase; -use React\Dns\Resolver\Resolver; use React\Dns\Model\Message; use React\Dns\Model\Record; +use React\Dns\Query\ExecutorInterface; +use React\Dns\Resolver\Resolver; +use React\Tests\Dns\TestCase; +use function React\Promise\resolve; class ResolveAliasesTest extends TestCase { @@ -20,7 +22,7 @@ public function testResolveAliases(array $expectedAnswers, array $answers, $name } $executor = $this->createExecutorMock(); - $executor->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message)); + $executor->expects($this->once())->method('query')->willReturn(resolve($message)); $resolver = new Resolver($executor); @@ -31,67 +33,65 @@ public function testResolveAliases(array $expectedAnswers, array $answers, $name public function provideAliasedAnswers() { - return [ + yield [ + ['178.79.169.131'], [ - ['178.79.169.131'], - [ - new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), ], + 'igor.io', + ]; + yield [ + ['178.79.169.131', '178.79.169.132', '178.79.169.133'], [ - ['178.79.169.131', '178.79.169.132', '178.79.169.133'], - [ - new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.132'), - new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.133'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), + new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.132'), + new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.133'), ], + 'igor.io', + ]; + yield [ + ['178.79.169.131'], [ - ['178.79.169.131'], - [ - new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - new Record('foo.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - new Record('bar.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), + new Record('foo.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), + new Record('bar.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), ], + 'igor.io', + ]; + yield [ + ['178.79.169.131'], [ - ['178.79.169.131'], - [ - new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), - new Record('foo.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), + new Record('foo.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), ], + 'igor.io', + ]; + yield [ + ['178.79.169.131'], [ - ['178.79.169.131'], - [ - new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), - new Record('foo.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'bar.igor.io'), - new Record('bar.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), + new Record('foo.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'bar.igor.io'), + new Record('bar.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), ], + 'igor.io', + ]; + yield [ + ['178.79.169.131', '178.79.169.132', '178.79.169.133'], [ - ['178.79.169.131', '178.79.169.132', '178.79.169.133'], - [ - new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), - new Record('foo.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'bar.igor.io'), - new Record('bar.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'baz.igor.io'), - new Record('bar.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'qux.igor.io'), - new Record('baz.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), - new Record('baz.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.132'), - new Record('qux.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.133'), - ], - 'igor.io', + new Record('igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'foo.igor.io'), + new Record('foo.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'bar.igor.io'), + new Record('bar.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'baz.igor.io'), + new Record('bar.igor.io', Message::TYPE_CNAME, Message::CLASS_IN, 3600, 'qux.igor.io'), + new Record('baz.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'), + new Record('baz.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.132'), + new Record('qux.igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.133'), ], + 'igor.io', ]; } private function createExecutorMock() { - return $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + return $this->createMock(ExecutorInterface::class); } } diff --git a/tests/Resolver/ResolverTest.php b/tests/Resolver/ResolverTest.php index cdc51275..f1df1850 100644 --- a/tests/Resolver/ResolverTest.php +++ b/tests/Resolver/ResolverTest.php @@ -2,12 +2,13 @@ namespace React\Tests\Dns\Resolver; -use React\Dns\Resolver\Resolver; -use React\Dns\Query\Query; use React\Dns\Model\Message; use React\Dns\Model\Record; -use React\Tests\Dns\TestCase; +use React\Dns\Query\ExecutorInterface; +use React\Dns\Query\Query; use React\Dns\RecordNotFoundException; +use React\Dns\Resolver\Resolver; +use React\Tests\Dns\TestCase; use function React\Promise\resolve; class ResolverTest extends TestCase @@ -19,7 +20,7 @@ public function resolveShouldQueryARecords() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -40,7 +41,7 @@ public function resolveAllShouldQueryGivenRecords() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -61,7 +62,7 @@ public function resolveAllShouldIgnoreRecordsWithOtherTypes() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -83,7 +84,7 @@ public function resolveAllShouldReturnMultipleValuesForAlias() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -108,7 +109,7 @@ public function resolveShouldQueryARecordsAndIgnoreCase() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -129,7 +130,7 @@ public function resolveShouldFilterByName() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -139,7 +140,7 @@ public function resolveShouldFilterByName() return resolve($response); })); - $errback = $this->expectCallableOnceWith($this->isInstanceOf('React\Dns\RecordNotFoundException')); + $errback = $this->expectCallableOnceWith($this->isInstanceOf(RecordNotFoundException::class)); $resolver = new Resolver($executor); $resolver->resolve('igor.io')->then($this->expectCallableNever(), $errback); @@ -154,7 +155,7 @@ public function resolveWithNoAnswersShouldCallErrbackIfGiven() $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) { $response = new Message(); $response->qr = true; @@ -173,31 +174,29 @@ public function resolveWithNoAnswersShouldCallErrbackIfGiven() public function provideRcodeErrors() { - return [ - [ - Message::RCODE_FORMAT_ERROR, - 'DNS query for example.com (A) returned an error response (Format Error)', - ], - [ - Message::RCODE_SERVER_FAILURE, - 'DNS query for example.com (A) returned an error response (Server Failure)', - ], - [ - Message::RCODE_NAME_ERROR, - 'DNS query for example.com (A) returned an error response (Non-Existent Domain / NXDOMAIN)' - ], - [ - Message::RCODE_NOT_IMPLEMENTED, - 'DNS query for example.com (A) returned an error response (Not Implemented)' - ], - [ - Message::RCODE_REFUSED, - 'DNS query for example.com (A) returned an error response (Refused)' - ], - [ - 99, - 'DNS query for example.com (A) returned an error response (Unknown error response code 99)' - ] + yield [ + Message::RCODE_FORMAT_ERROR, + 'DNS query for example.com (A) returned an error response (Format Error)', + ]; + yield [ + Message::RCODE_SERVER_FAILURE, + 'DNS query for example.com (A) returned an error response (Server Failure)', + ]; + yield [ + Message::RCODE_NAME_ERROR, + 'DNS query for example.com (A) returned an error response (Non-Existent Domain / NXDOMAIN)' + ]; + yield [ + Message::RCODE_NOT_IMPLEMENTED, + 'DNS query for example.com (A) returned an error response (Not Implemented)' + ]; + yield [ + Message::RCODE_REFUSED, + 'DNS query for example.com (A) returned an error response (Refused)' + ]; + yield [ + 99, + 'DNS query for example.com (A) returned an error response (Unknown error response code 99)' ]; } @@ -211,7 +210,7 @@ public function resolveWithRcodeErrorShouldCallErrbackIfGiven($code, $expectedMe $executor ->expects($this->once()) ->method('query') - ->with($this->isInstanceOf('React\Dns\Query\Query')) + ->with($this->isInstanceOf(Query::class)) ->will($this->returnCallback(function ($query) use ($code) { $response = new Message(); $response->qr = true; @@ -231,6 +230,6 @@ public function resolveWithRcodeErrorShouldCallErrbackIfGiven($code, $expectedMe private function createExecutorMock() { - return $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); + return $this->createMock(ExecutorInterface::class); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 3bc967a0..23bd860e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,29 +39,13 @@ protected function expectCallableNever() protected function createCallableMock() { - if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) { + $builder = $this->getMockBuilder(\stdClass::class); + if (method_exists($builder, 'addMethods')) { // PHPUnit 9+ - return $this->getMockBuilder('stdClass')->addMethods(['__invoke'])->getMock(); + return $builder->addMethods(['__invoke'])->getMock(); } else { // legacy PHPUnit - return $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock(); + return $builder->setMethods(['__invoke'])->getMock(); } } - - public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) - { - if (method_exists($this, 'expectException')) { - // PHPUnit 5 - $this->expectException($exception); - if ($exceptionMessage !== '') { - $this->expectExceptionMessage($exceptionMessage); - } - if ($exceptionCode !== null) { - $this->expectExceptionCode($exceptionCode); - } - } else { - // legacy PHPUnit - parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); - } - } }