From 58db19abeaacd2131262f95ef6230698dcaff415 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Mon, 13 Nov 2017 06:26:32 -0200 Subject: [PATCH 1/2] Use PHPUnit\Framework\TestCase instead of PHPUnit_Framework_TestCase --- composer.json | 2 +- tests/Config/FilesystemFactoryTest.php | 3 ++- tests/Model/MessageTest.php | 3 ++- tests/Protocol/BinaryDumperTest.php | 3 ++- tests/Protocol/ParserTest.php | 3 ++- tests/Query/ExecutorTest.php | 35 ++++++++++++++++++-------- tests/Query/RecordBagTest.php | 3 ++- tests/Query/RecordCacheTest.php | 3 ++- tests/Resolver/ResolveAliasesTest.php | 3 ++- tests/TestCase.php | 4 ++- 10 files changed, 43 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 8380a11e..0c6cf7d8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ }, "require-dev": { "clue/block-react": "^1.2", - "phpunit/phpunit": "^5.0 || ^4.8.10" + "phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35" }, "autoload": { "psr-4": { "React\\Dns\\": "src" } diff --git a/tests/Config/FilesystemFactoryTest.php b/tests/Config/FilesystemFactoryTest.php index 718c11fc..bb9eac75 100644 --- a/tests/Config/FilesystemFactoryTest.php +++ b/tests/Config/FilesystemFactoryTest.php @@ -2,9 +2,10 @@ namespace React\Test\Dns\Config; +use PHPUnit\Framework\TestCase; use React\Dns\Config\FilesystemFactory; -class FilesystemFactoryTest extends \PHPUnit_Framework_TestCase +class FilesystemFactoryTest extends TestCase { /** @test */ public function parseEtcResolvConfShouldParseCorrectly() diff --git a/tests/Model/MessageTest.php b/tests/Model/MessageTest.php index 46528273..53d6b28f 100644 --- a/tests/Model/MessageTest.php +++ b/tests/Model/MessageTest.php @@ -2,10 +2,11 @@ namespace React\Tests\Dns\Model; +use PHPUnit\Framework\TestCase; use React\Dns\Query\Query; use React\Dns\Model\Message; -class MessageTest extends \PHPUnit_Framework_TestCase +class MessageTest extends TestCase { public function testCreateRequestDesiresRecusion() { diff --git a/tests/Protocol/BinaryDumperTest.php b/tests/Protocol/BinaryDumperTest.php index dffc4b5b..bf60ca97 100644 --- a/tests/Protocol/BinaryDumperTest.php +++ b/tests/Protocol/BinaryDumperTest.php @@ -2,10 +2,11 @@ namespace React\Tests\Dns\Protocol; +use PHPUnit\Framework\TestCase; use React\Dns\Protocol\BinaryDumper; use React\Dns\Model\Message; -class BinaryDumperTest extends \PHPUnit_Framework_TestCase +class BinaryDumperTest extends TestCase { public function testRequestToBinary() { diff --git a/tests/Protocol/ParserTest.php b/tests/Protocol/ParserTest.php index 97b07272..195fad24 100644 --- a/tests/Protocol/ParserTest.php +++ b/tests/Protocol/ParserTest.php @@ -2,10 +2,11 @@ namespace React\Tests\Dns\Protocol; +use PHPUnit\Framework\TestCase; use React\Dns\Protocol\Parser; use React\Dns\Model\Message; -class ParserTest extends \PHPUnit_Framework_TestCase +class ParserTest extends TestCase { public function setUp() { diff --git a/tests/Query/ExecutorTest.php b/tests/Query/ExecutorTest.php index 5ddba698..0f5e2d63 100644 --- a/tests/Query/ExecutorTest.php +++ b/tests/Query/ExecutorTest.php @@ -46,17 +46,24 @@ public function queryShouldCreateUdpRequest() $this->executor->query('8.8.8.8:53', $query); } - /** @test */ + /** + * @test + * @expectedException RuntimeException + */ public function resolveShouldRejectIfRequestIsLargerThan512Bytes() { $query = new Query(str_repeat('a', 512).'.igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $promise = $this->executor->query('8.8.8.8:53', $query); - $this->setExpectedException('RuntimeException', 'DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version'); + $this->expectExceptionMessage('DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version'); Block\await($promise, $this->loop); } - /** @test */ + /** + * @test + * @expectedException React\Dns\Query\CancellationException + * @expectedExceptionMessage DNS query for igor.io has been cancelled + */ public function resolveShouldCloseConnectionWhenCancelled() { $conn = $this->createConnectionMock(false); @@ -80,11 +87,14 @@ public function resolveShouldCloseConnectionWhenCancelled() $promise->cancel(); - $this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled'); Block\await($promise, $this->loop); } - /** @test */ + /** + * @test + * @expectedException React\Dns\Query\CancellationException + * @expectedExceptionMessage DNS query for igor.io has been cancelled + */ public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull() { $this->loop @@ -98,7 +108,6 @@ public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull $promise->cancel(); - $this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled'); Block\await($promise, $this->loop); } @@ -128,7 +137,11 @@ public function resolveShouldRejectIfResponseIsTruncated() $this->executor->query('8.8.8.8:53', $query); } - /** @test */ + /** + * @test + * @expectedException RuntimeException + * @expectedExceptionMessage DNS query for igor.io failed: Nope + */ public function resolveShouldFailIfUdpThrow() { $this->loop @@ -149,7 +162,6 @@ public function resolveShouldFailIfUdpThrow() $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $promise = $this->executor->query('8.8.8.8:53', $query); - $this->setExpectedException('RuntimeException', 'DNS query for igor.io failed: Nope'); Block\await($promise, $this->loop); } @@ -188,7 +200,11 @@ public function resolveShouldCancelTimerWhenFullResponseIsReceived() $this->executor->query('8.8.8.8:53', $query); } - /** @test */ + /** + * @test + * @expectedException React\Dns\Query\TimeoutException + * @expectedExceptionMessage DNS query for igor.io timed out + */ public function resolveShouldCloseConnectionOnTimeout() { $this->executor = $this->createExecutorMock(); @@ -218,7 +234,6 @@ public function resolveShouldCloseConnectionOnTimeout() $this->assertNotNull($timerCallback); $timerCallback(); - $this->setExpectedException('React\Dns\Query\TimeoutException', 'DNS query for igor.io timed out'); Block\await($promise, $this->loop); } diff --git a/tests/Query/RecordBagTest.php b/tests/Query/RecordBagTest.php index 48353531..83b89344 100644 --- a/tests/Query/RecordBagTest.php +++ b/tests/Query/RecordBagTest.php @@ -2,11 +2,12 @@ namespace React\Tests\Dns\Query; +use PHPUnit\Framework\TestCase; use React\Dns\Query\RecordBag; use React\Dns\Model\Message; use React\Dns\Model\Record; -class RecordBagTest extends \PHPUnit_Framework_TestCase +class RecordBagTest extends TestCase { /** * @covers React\Dns\Query\RecordBag diff --git a/tests/Query/RecordCacheTest.php b/tests/Query/RecordCacheTest.php index 4500c724..399fbe8a 100644 --- a/tests/Query/RecordCacheTest.php +++ b/tests/Query/RecordCacheTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Dns\Query; +use PHPUnit\Framework\TestCase; use React\Cache\ArrayCache; use React\Dns\Model\Message; use React\Dns\Model\Record; @@ -9,7 +10,7 @@ use React\Dns\Query\Query; use React\Promise\PromiseInterface; -class RecordCacheTest extends \PHPUnit_Framework_TestCase +class RecordCacheTest extends TestCase { /** * @covers React\Dns\Query\RecordCache diff --git a/tests/Resolver/ResolveAliasesTest.php b/tests/Resolver/ResolveAliasesTest.php index 7ffa082e..b5175e3e 100644 --- a/tests/Resolver/ResolveAliasesTest.php +++ b/tests/Resolver/ResolveAliasesTest.php @@ -2,12 +2,13 @@ namespace React\Tests\Dns\Resolver; +use PHPUnit\Framework\TestCase; use React\Dns\Resolver\Resolver; use React\Dns\Query\Query; use React\Dns\Model\Message; use React\Dns\Model\Record; -class ResolveAliasesTest extends \PHPUnit_Framework_TestCase +class ResolveAliasesTest extends TestCase { /** * @covers React\Dns\Resolver\Resolver::resolveAliases diff --git a/tests/TestCase.php b/tests/TestCase.php index 620bfaa1..5ddc9d8b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,9 @@ namespace React\Tests\Dns; -abstract class TestCase extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase as BaseTestCase; + +abstract class TestCase extends BaseTestCase { protected function expectCallableOnce() { From b0b76d6bc403d9d93f7b20238ba02596405c0822 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Sun, 19 Nov 2017 07:45:54 -0200 Subject: [PATCH 2/2] Added setExpectedException method for compatibility --- tests/Query/ExecutorTest.php | 35 ++++++++++------------------------- tests/TestCase.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/tests/Query/ExecutorTest.php b/tests/Query/ExecutorTest.php index 0f5e2d63..5ddba698 100644 --- a/tests/Query/ExecutorTest.php +++ b/tests/Query/ExecutorTest.php @@ -46,24 +46,17 @@ public function queryShouldCreateUdpRequest() $this->executor->query('8.8.8.8:53', $query); } - /** - * @test - * @expectedException RuntimeException - */ + /** @test */ public function resolveShouldRejectIfRequestIsLargerThan512Bytes() { $query = new Query(str_repeat('a', 512).'.igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $promise = $this->executor->query('8.8.8.8:53', $query); - $this->expectExceptionMessage('DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version'); + $this->setExpectedException('RuntimeException', 'DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version'); Block\await($promise, $this->loop); } - /** - * @test - * @expectedException React\Dns\Query\CancellationException - * @expectedExceptionMessage DNS query for igor.io has been cancelled - */ + /** @test */ public function resolveShouldCloseConnectionWhenCancelled() { $conn = $this->createConnectionMock(false); @@ -87,14 +80,11 @@ public function resolveShouldCloseConnectionWhenCancelled() $promise->cancel(); + $this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled'); Block\await($promise, $this->loop); } - /** - * @test - * @expectedException React\Dns\Query\CancellationException - * @expectedExceptionMessage DNS query for igor.io has been cancelled - */ + /** @test */ public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull() { $this->loop @@ -108,6 +98,7 @@ public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull $promise->cancel(); + $this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled'); Block\await($promise, $this->loop); } @@ -137,11 +128,7 @@ public function resolveShouldRejectIfResponseIsTruncated() $this->executor->query('8.8.8.8:53', $query); } - /** - * @test - * @expectedException RuntimeException - * @expectedExceptionMessage DNS query for igor.io failed: Nope - */ + /** @test */ public function resolveShouldFailIfUdpThrow() { $this->loop @@ -162,6 +149,7 @@ public function resolveShouldFailIfUdpThrow() $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $promise = $this->executor->query('8.8.8.8:53', $query); + $this->setExpectedException('RuntimeException', 'DNS query for igor.io failed: Nope'); Block\await($promise, $this->loop); } @@ -200,11 +188,7 @@ public function resolveShouldCancelTimerWhenFullResponseIsReceived() $this->executor->query('8.8.8.8:53', $query); } - /** - * @test - * @expectedException React\Dns\Query\TimeoutException - * @expectedExceptionMessage DNS query for igor.io timed out - */ + /** @test */ public function resolveShouldCloseConnectionOnTimeout() { $this->executor = $this->createExecutorMock(); @@ -234,6 +218,7 @@ public function resolveShouldCloseConnectionOnTimeout() $this->assertNotNull($timerCallback); $timerCallback(); + $this->setExpectedException('React\Dns\Query\TimeoutException', 'DNS query for igor.io timed out'); Block\await($promise, $this->loop); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5ddc9d8b..a5a22bfd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -41,4 +41,21 @@ protected function createCallableMock() { return $this->getMockBuilder('React\Tests\Dns\CallableStub')->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 4 + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); + } + } }