diff --git a/composer.json b/composer.json index 767e12c7..626c4fd9 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ }, "require-dev": { "clue/block-react": "^1.2", - "phpunit/phpunit": "^7.0 || ^6.4 || ^5.7 || ^4.8.35" + "phpunit/phpunit": "^9.0 || ^4.8.35" }, "autoload": { "psr-4": { "React\\Dns\\": "src" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 04d426b5..0e947b87 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,6 @@ - + ./tests/ diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 31ae1f96..6416495b 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -32,11 +32,9 @@ public function testLoadsFromExplicitPath() $this->assertEquals(array('8.8.8.8'), $config->nameservers); } - /** - * @expectedException RuntimeException - */ public function testLoadThrowsWhenPathIsInvalid() { + $this->setExpectedException('RuntimeException'); Config::loadResolvConfBlocking(__DIR__ . '/invalid.conf'); } diff --git a/tests/Config/HostsFileTest.php b/tests/Config/HostsFileTest.php index ff74ad2c..8a7ab852 100644 --- a/tests/Config/HostsFileTest.php +++ b/tests/Config/HostsFileTest.php @@ -25,11 +25,9 @@ public function testDefaultShouldHaveLocalhostMapped() $this->assertContains('127.0.0.1', $hosts->getIpsForHost('localhost')); } - /** - * @expectedException RuntimeException - */ public function testLoadThrowsForInvalidPath() { + $this->setExpectedException('RuntimeException'); HostsFile::loadFromPathBlocking('does/not/exist'); } diff --git a/tests/FunctionalResolverTest.php b/tests/FunctionalResolverTest.php index 102274cf..92ccc20e 100644 --- a/tests/FunctionalResolverTest.php +++ b/tests/FunctionalResolverTest.php @@ -9,7 +9,10 @@ class FunctionalTest extends TestCase { - public function setUp() + /** + * @before + */ + public function setUpResolver() { $this->loop = LoopFactory::create(); diff --git a/tests/Protocol/ParserTest.php b/tests/Protocol/ParserTest.php index 6efe0481..7fe1cd7d 100644 --- a/tests/Protocol/ParserTest.php +++ b/tests/Protocol/ParserTest.php @@ -2,13 +2,16 @@ namespace React\Tests\Dns\Protocol; -use PHPUnit\Framework\TestCase; -use React\Dns\Protocol\Parser; use React\Dns\Model\Message; +use React\Dns\Protocol\Parser; +use React\Tests\Dns\TestCase; class ParserTest extends TestCase { - public function setUp() + /** + * @before + */ + public function setUpParser() { $this->parser = new Parser(); } @@ -735,9 +738,6 @@ public function testParsePTRResponseWithSpecialCharactersEscaped() $this->assertSame('3rd\.\ Floor\ Copy\ Room._printer._tcp.dns-sd.org', $response->answers[0]->data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteQuestionThrows() { $data = ""; @@ -747,12 +747,10 @@ public function testParseIncompleteQuestionThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteQuestionLabelThrows() { $data = ""; @@ -761,12 +759,10 @@ public function testParseIncompleteQuestionLabelThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteQuestionNameThrows() { $data = ""; @@ -775,12 +771,10 @@ public function testParseIncompleteQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteOffsetPointerInQuestionNameThrows() { $data = ""; @@ -789,12 +783,10 @@ public function testParseIncompleteOffsetPointerInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidOffsetPointerInQuestionNameThrows() { $data = ""; @@ -803,12 +795,10 @@ public function testParseInvalidOffsetPointerInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidOffsetPointerToSameLabelInQuestionNameThrows() { $data = ""; @@ -817,12 +807,10 @@ public function testParseInvalidOffsetPointerToSameLabelInQuestionNameThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidOffsetPointerToStartOfMessageInQuestionNameThrows() { $data = ""; @@ -831,12 +819,10 @@ public function testParseInvalidOffsetPointerToStartOfMessageInQuestionNameThrow $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteAnswerFieldsThrows() { $data = ""; @@ -847,12 +833,10 @@ public function testParseIncompleteAnswerFieldsThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseMessageResponseWithIncompleteAuthorityRecordThrows() { $data = ""; @@ -863,12 +847,10 @@ public function testParseMessageResponseWithIncompleteAuthorityRecordThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseMessageResponseWithIncompleteAdditionalRecordThrows() { $data = ""; @@ -879,12 +861,10 @@ public function testParseMessageResponseWithIncompleteAdditionalRecordThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseIncompleteAnswerRecordDataThrows() { $data = ""; @@ -898,12 +878,10 @@ public function testParseIncompleteAnswerRecordDataThrows() $data = $this->convertTcpDumpToBinary($data); + $this->setExpectedException('InvalidArgumentException'); $this->parser->parseMessage($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidNSResponseWhereDomainNameIsMissing() { $data = ""; @@ -912,12 +890,10 @@ public function testParseInvalidNSResponseWhereDomainNameIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidAResponseWhereIPIsMissing() { $data = ""; @@ -926,12 +902,10 @@ public function testParseInvalidAResponseWhereIPIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidAAAAResponseWhereIPIsMissing() { $data = ""; @@ -940,12 +914,10 @@ public function testParseInvalidAAAAResponseWhereIPIsMissing() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidTXTResponseWhereTxtChunkExceedsLimit() { $data = ""; @@ -955,12 +927,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidMXResponseWhereDomainNameIsIncomplete() { $data = ""; @@ -970,12 +940,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidMXResponseWhereDomainNameIsMissing() { $data = ""; @@ -985,12 +953,10 @@ public function testParseInvalidMXResponseWhereDomainNameIsMissing() $data .= "00 02"; // answer: rdlength 2 $data .= "00 0a"; // answer: rdata priority 10 + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidSRVResponseWhereDomainNameIsIncomplete() { $data = ""; @@ -1000,12 +966,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidSRVResponseWhereDomainNameIsMissing() { $data = ""; @@ -1015,12 +979,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidSSHFPResponseWhereRecordIsTooSmall() { $data = ""; @@ -1030,12 +992,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidOPTResponseWhereRecordIsTooSmall() { $data = ""; @@ -1045,12 +1005,10 @@ public function testParseInvalidOPTResponseWhereRecordIsTooSmall() $data .= "00 03"; // answer: rdlength 3 $data .= "00 00 00"; // answer: type 0, length incomplete + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidOPTResponseWhereRecordLengthDoesNotMatchOptType() { $data = ""; @@ -1060,12 +1018,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidSOAResponseWhereFlagsAreMissing() { $data = ""; @@ -1076,12 +1032,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidCAAResponseEmtpyData() { $data = ""; @@ -1090,12 +1044,10 @@ public function testParseInvalidCAAResponseEmtpyData() $data .= "00 01 51 80"; // answer: ttl 86400 $data .= "00 00"; // answer: rdlength 0 + $this->setExpectedException('InvalidArgumentException'); $this->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidCAAResponseMissingValue() { $data = ""; @@ -1105,12 +1057,10 @@ 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->parseAnswer($data); } - /** - * @expectedException InvalidArgumentException - */ public function testParseInvalidCAAResponseIncompleteTag() { $data = ""; @@ -1121,6 +1071,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->parseAnswer($data); } diff --git a/tests/Query/HostsFileExecutorTest.php b/tests/Query/HostsFileExecutorTest.php index 455bafe8..2a1d818e 100644 --- a/tests/Query/HostsFileExecutorTest.php +++ b/tests/Query/HostsFileExecutorTest.php @@ -13,7 +13,10 @@ class HostsFileExecutorTest extends TestCase private $fallback; private $executor; - public function setUp() + /** + * @before + */ + public function setUpMocks() { $this->hosts = $this->getMockBuilder('React\Dns\Config\HostsFile')->disableOriginalConstructor()->getMock(); $this->fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); diff --git a/tests/Query/SelectiveTransportExecutorTest.php b/tests/Query/SelectiveTransportExecutorTest.php index d9d22b20..a2632045 100644 --- a/tests/Query/SelectiveTransportExecutorTest.php +++ b/tests/Query/SelectiveTransportExecutorTest.php @@ -11,7 +11,10 @@ class SelectiveTransportExecutorTest extends TestCase { - public function setUp() + /** + * @before + */ + public function setUpMocks() { $this->datagram = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); $this->stream = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock(); diff --git a/tests/Query/TcpTransportExecutorTest.php b/tests/Query/TcpTransportExecutorTest.php index f15866da..ce9c845c 100644 --- a/tests/Query/TcpTransportExecutorTest.php +++ b/tests/Query/TcpTransportExecutorTest.php @@ -60,33 +60,27 @@ public static function provideDefaultPortProvider() ); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new TcpTransportExecutor('///', $loop); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverAddressContainsHostname() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new TcpTransportExecutor('localhost', $loop); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverSchemeIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new TcpTransportExecutor('udp://1.2.3.4', $loop); } diff --git a/tests/Query/TimeoutExecutorTest.php b/tests/Query/TimeoutExecutorTest.php index 1f2d30c3..a6c2728e 100644 --- a/tests/Query/TimeoutExecutorTest.php +++ b/tests/Query/TimeoutExecutorTest.php @@ -2,18 +2,22 @@ namespace React\Tests\Dns\Query; -use React\Dns\Query\TimeoutExecutor; -use React\Dns\Query\Query; use React\Dns\Model\Message; -use React\Promise\Deferred; use React\Dns\Query\CancellationException; -use React\Tests\Dns\TestCase; +use React\Dns\Query\Query; +use React\Dns\Query\TimeoutException; +use React\Dns\Query\TimeoutExecutor; use React\EventLoop\Factory; use React\Promise; +use React\Promise\Deferred; +use React\Tests\Dns\TestCase; class TimeoutExecutorTest extends TestCase { - public function setUp() + /** + * @before + */ + public function setUpExecutor() { $this->loop = Factory::create(); @@ -92,23 +96,17 @@ public function testWrappedWillBeCancelledOnTimeout() return $deferred->promise(); })); - $callback = $this->expectCallableNever(); - - $errorback = $this->createCallableMock(); - $errorback - ->expects($this->once()) - ->method('__invoke') - ->with($this->logicalAnd( - $this->isInstanceOf('React\Dns\Query\TimeoutException'), - $this->attribute($this->equalTo('DNS query for igor.io timed out'), 'message') - )); - $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN); - $this->executor->query($query)->then($callback, $errorback); + $promise = $this->executor->query($query); $this->assertEquals(0, $cancelled); - $this->loop->run(); + try { + \Clue\React\Block\await($promise, $this->loop); + $this->fail(); + } catch (TimeoutException $exception) { + $this->assertEquals('DNS query for igor.io timed out' , $exception->getMessage()); + } $this->assertEquals(1, $cancelled); } diff --git a/tests/Query/UdpTransportExecutorTest.php b/tests/Query/UdpTransportExecutorTest.php index d01ab3ee..78a85bb8 100644 --- a/tests/Query/UdpTransportExecutorTest.php +++ b/tests/Query/UdpTransportExecutorTest.php @@ -60,33 +60,27 @@ public static function provideDefaultPortProvider() ); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new UdpTransportExecutor('///', $loop); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverAddressContainsHostname() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new UdpTransportExecutor('localhost', $loop); } - /** - * @expectedException InvalidArgumentException - */ public function testCtorShouldThrowWhenNameserverSchemeIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->setExpectedException('InvalidArgumentException'); new UdpTransportExecutor('tcp://1.2.3.4', $loop); } diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index 1f201bf2..fe089739 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -19,7 +19,6 @@ public function createShouldCreateResolver() $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); } - /** @test */ public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExecutorStack() { @@ -135,15 +134,13 @@ public function createWithTcpSchemeShouldCreateResolverWithTcpExecutorStack() $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor); } - /** - * @test - * @expectedException InvalidArgumentException - */ + /** @test */ public function createShouldThrowWhenNameserverIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $factory = new Factory(); + $this->setExpectedException('InvalidArgumentException'); $factory->create('///', $loop); } diff --git a/tests/TestCase.php b/tests/TestCase.php index a5a22bfd..2c0c0cf1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,7 +39,7 @@ protected function expectCallableNever() protected function createCallableMock() { - return $this->getMockBuilder('React\Tests\Dns\CallableStub')->getMock(); + return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); } public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)