diff --git a/.travis.yml b/.travis.yml index d0dc010..0a5430e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,6 @@ matrix: - php: 7.3 - php: 7.4 - php: hhvm-3.18 - install: - - composer require phpunit/phpunit:^5 --dev --no-interaction # requires legacy phpunit allow_failures: - php: hhvm-3.18 diff --git a/composer.json b/composer.json index 313bd03..8749123 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "clue/http-proxy-react": "^1.3", "clue/reactphp-ssh-proxy": "^1.0", "clue/socks-react": "^1.0", - "phpunit/phpunit": "^7.0 || ^6.4 || ^5.7 || ^4.8.35", + "phpunit/phpunit": "^9.0 || ^5.7 || ^4.8.35", "react/http": "^0.8" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8bb54bb..e580744 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,4 +11,4 @@ ./src/ - \ No newline at end of file + diff --git a/tests/BrowserTest.php b/tests/BrowserTest.php index fd7f250..ed3c273 100644 --- a/tests/BrowserTest.php +++ b/tests/BrowserTest.php @@ -4,7 +4,6 @@ use Clue\React\Block; use Clue\React\Buzz\Browser; -use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use React\Promise\Promise; use RingCentral\Psr7\Uri; @@ -15,7 +14,10 @@ class BrowserTest extends TestCase private $sender; private $browser; - public function setUp() + /** + * @before + */ + public function setUpBrowser() { $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $this->sender = $this->getMockBuilder('Clue\React\Buzz\Io\Transaction')->disableOriginalConstructor()->getMock(); @@ -226,7 +228,6 @@ public function provideOtherBaseUris() /** * @param string $other * @dataProvider provideOtherBaseUris - * @expectedException UnexpectedValueException */ public function testRequestingUrlsNotBelowBaseWillRejectBeforeSending($other) { @@ -234,14 +235,13 @@ public function testRequestingUrlsNotBelowBaseWillRejectBeforeSending($other) $this->sender->expects($this->never())->method('send'); + $this->setExpectedException('UnexpectedValueException'); Block\await($browser->get($other), $this->loop); } - /** - * @expectedException InvalidArgumentException - */ public function testWithBaseUriNotAbsoluteFails() { + $this->setExpectedException('InvalidArgumentException'); $this->browser->withBase('hello'); } @@ -271,11 +271,9 @@ public function testWithProtocolVersionFollowedBySubmitRequestSendsRequestWithPr $this->browser->submit('http://example.com/', array()); } - /** - * @expectedException InvalidArgumentException - */ public function testWithProtocolVersionInvalidThrows() { + $this->setExpectedException('InvalidArgumentException'); $this->browser->withProtocolVersion('1.2'); } @@ -291,19 +289,4 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection() $promise = $this->browser->get('http://example.com/'); $promise->cancel(); } - - protected function expectCallableOnce() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke'); - - return $mock; - } - - protected function createCallableMock() - { - return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); - } } diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index 398b311..35c7bf7 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -5,7 +5,6 @@ use Clue\React\Block; use Clue\React\Buzz\Browser; use Clue\React\Buzz\Message\ResponseException; -use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Factory; use React\Http\Response; @@ -23,7 +22,10 @@ class FunctionalBrowserTest extends TestCase private $browser; private $base; - public function setUp() + /** + * @before + */ + public function setUpBrowserAndServer() { $this->loop = $loop = Factory::create(); $this->browser = new Browser($this->loop); @@ -143,20 +145,15 @@ public function testSimpleRequest() Block\await($this->browser->get($this->base . 'get'), $this->loop); } - /** - * @expectedException RuntimeException - */ public function testCancelGetRequestWillRejectRequest() { $promise = $this->browser->get($this->base . 'get'); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - */ public function testCancelSendWithPromiseFollowerWillRejectRequest() { $promise = $this->browser->send(new Request('GET', $this->base . 'get'))->then(function () { @@ -164,14 +161,13 @@ public function testCancelSendWithPromiseFollowerWillRejectRequest() }); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - */ public function testRequestWithoutAuthenticationFails() { + $this->setExpectedException('RuntimeException'); Block\await($this->browser->get($this->base . 'basic-auth/user/pass'), $this->loop); } @@ -214,10 +210,6 @@ public function testRedirectFromPageWithInvalidAuthToPageWithCorrectAuthenticati Block\await($this->browser->get($base . 'redirect-to?url=' . urlencode($target)), $this->loop); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Request cancelled - */ public function testCancelRedirectedRequestShouldReject() { $promise = $this->browser->get($this->base . 'redirect-to?url=delay%2F10'); @@ -226,30 +218,25 @@ public function testCancelRedirectedRequestShouldReject() $promise->cancel(); }); + $this->setExpectedException('RuntimeException', 'Request cancelled'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Request timed out after 0.1 seconds - */ public function testTimeoutDelayedResponseShouldReject() { $promise = $this->browser->withOptions(array('timeout' => 0.1))->get($this->base . 'delay/10'); + $this->setExpectedException('RuntimeException', 'Request timed out after 0.1 seconds'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Request timed out after 0.1 seconds - */ public function testTimeoutDelayedResponseAfterStreamingRequestShouldReject() { $stream = new ThroughStream(); $promise = $this->browser->withOptions(array('timeout' => 0.1))->post($this->base . 'delay/10', array(), $stream); $stream->end(); + $this->setExpectedException('RuntimeException', 'Request timed out after 0.1 seconds'); Block\await($promise, $this->loop); } @@ -287,13 +274,11 @@ public function testNotFollowingRedirectsResolvesWithRedirectResult() Block\await($browser->get($this->base . 'redirect-to?url=get'), $this->loop); } - /** - * @expectedException RuntimeException - */ public function testRejectingRedirectsRejects() { $browser = $this->browser->withOptions(array('maxRedirects' => 0)); + $this->setExpectedException('RuntimeException'); Block\await($browser->get($this->base . 'redirect-to?url=get'), $this->loop); } @@ -320,7 +305,6 @@ public function testCanAccessHttps() /** * @group online - * @expectedException RuntimeException */ public function testVerifyPeerEnabledForBadSslRejects() { @@ -336,6 +320,7 @@ public function testVerifyPeerEnabledForBadSslRejects() $browser = new Browser($this->loop, $connector); + $this->setExpectedException('RuntimeException'); Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop); } @@ -362,10 +347,10 @@ public function testVerifyPeerDisabledForBadSslResolves() /** * @group online - * @expectedException RuntimeException */ public function testInvalidPort() { + $this->setExpectedException('RuntimeException'); Block\await($this->browser->get('http://www.google.com:443/'), $this->loop); } diff --git a/tests/Io/ChunkedEncoderTest.php b/tests/Io/ChunkedEncoderTest.php index c449033..8376dff 100644 --- a/tests/Io/ChunkedEncoderTest.php +++ b/tests/Io/ChunkedEncoderTest.php @@ -3,7 +3,7 @@ namespace Clue\Tests\React\Buzz\Io; use Clue\React\Buzz\Io\ChunkedEncoder; -use PHPUnit\Framework\TestCase; +use Clue\Tests\React\Buzz\TestCase; use React\Stream\ThroughStream; class ChunkedEncoderTest extends TestCase @@ -11,7 +11,10 @@ class ChunkedEncoderTest extends TestCase private $input; private $chunkedStream; - public function setUp() + /** + * @before + */ + public function setUpStream() { $this->input = new ThroughStream(); $this->chunkedStream = new ChunkedEncoder($this->input); @@ -81,40 +84,4 @@ public function testPipeStream() $this->assertSame($dest, $ret); } - - protected function expectCallableOnce() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke'); - - return $mock; - } - - protected function expectCallableOnceWith($value) - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($value); - - return $mock; - } - - protected function expectCallableNever() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->never()) - ->method('__invoke'); - - return $mock; - } - - protected function createCallableMock() - { - return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); - } } diff --git a/tests/Io/SenderTest.php b/tests/Io/SenderTest.php index d260b65..c855e9e 100644 --- a/tests/Io/SenderTest.php +++ b/tests/Io/SenderTest.php @@ -5,7 +5,7 @@ use Clue\React\Block; use Clue\React\Buzz\Io\Sender; use Clue\React\Buzz\Message\ReadableBodyStream; -use PHPUnit\Framework\TestCase; +use Clue\Tests\React\Buzz\TestCase; use React\HttpClient\Client as HttpClient; use React\HttpClient\RequestData; use React\Promise; @@ -16,7 +16,10 @@ class SenderTest extends TestCase { private $loop; - public function setUp() + /** + * @before + */ + public function setUpLoop() { $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); } @@ -28,9 +31,6 @@ public function testCreateFromLoop() $this->assertInstanceOf('Clue\React\Buzz\Io\Sender', $sender); } - /** - * @expectedException InvalidArgumentException - */ public function testSenderRejectsInvalidUri() { $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); @@ -42,12 +42,10 @@ public function testSenderRejectsInvalidUri() $promise = $sender->send($request); + $this->setExpectedException('InvalidArgumentException'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - */ public function testSenderConnectorRejection() { $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); @@ -59,6 +57,7 @@ public function testSenderConnectorRejection() $promise = $sender->send($request); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } @@ -303,9 +302,6 @@ public function testSendCustomMethodWithExplicitContentLengthZeroWillBePassedAsI $sender->send($request); } - /** - * @expectedException RuntimeException - */ public function testCancelRequestWillCancelConnector() { $promise = new \React\Promise\Promise(function () { }, function () { @@ -322,12 +318,10 @@ public function testCancelRequestWillCancelConnector() $promise = $sender->send($request); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } - /** - * @expectedException RuntimeException - */ public function testCancelRequestWillCloseConnection() { $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); @@ -343,6 +337,7 @@ public function testCancelRequestWillCloseConnection() $promise = $sender->send($request); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } diff --git a/tests/Io/TransactionTest.php b/tests/Io/TransactionTest.php index e47c362..18ada59 100644 --- a/tests/Io/TransactionTest.php +++ b/tests/Io/TransactionTest.php @@ -6,8 +6,8 @@ use Clue\React\Buzz\Io\Transaction; use Clue\React\Buzz\Message\MessageFactory; use Clue\React\Buzz\Message\ResponseException; +use Clue\Tests\React\Buzz\TestCase; use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use React\EventLoop\Factory; use React\Promise; @@ -409,9 +409,6 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau $this->assertEquals('hello world', (string)$response->getBody()); } - /** - * @expectedException RuntimeException - */ public function testCancelBufferingResponseWillCloseStreamAndReject() { $messageFactory = new MessageFactory(); @@ -432,6 +429,7 @@ public function testCancelBufferingResponseWillCloseStreamAndReject() $promise = $transaction->send($request); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $loop, 0.001); } @@ -813,30 +811,4 @@ private function makeSenderMock() { return $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock(); } - - protected function expectCallableOnce() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke'); - - return $mock; - } - - protected function expectCallableOnceWith($value) - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($value); - - return $mock; - } - - protected function createCallableMock() - { - return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); - } } diff --git a/tests/Message/MessageFactoryTest.php b/tests/Message/MessageFactoryTest.php index ad86bd0..bc8a462 100644 --- a/tests/Message/MessageFactoryTest.php +++ b/tests/Message/MessageFactoryTest.php @@ -9,7 +9,10 @@ class MessageFactoryTest extends TestCase { private $messageFactory; - public function setUp() + /** + * @before + */ + public function setUpMessageFactory() { $this->messageFactory = new MessageFactory(); } diff --git a/tests/Message/ReadableBodyStreamTest.php b/tests/Message/ReadableBodyStreamTest.php index 3990cb7..203963b 100644 --- a/tests/Message/ReadableBodyStreamTest.php +++ b/tests/Message/ReadableBodyStreamTest.php @@ -3,7 +3,7 @@ namespace Clue\Tests\React\Buzz\Message; use Clue\React\Buzz\Message\ReadableBodyStream; -use PHPUnit\Framework\TestCase; +use Clue\Tests\React\Buzz\TestCase; use React\Stream\ThroughStream; class ReadableBodyStreamTest extends TestCase @@ -11,7 +11,10 @@ class ReadableBodyStreamTest extends TestCase private $input; private $stream; - public function setUp() + /** + * @before + */ + public function setUpStream() { $this->input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); $this->stream = new ReadableBodyStream($this->input); @@ -183,11 +186,9 @@ public function testPointlessTostringReturnsEmptyString() $this->assertEquals('', (string)$this->stream); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessDetachThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->detach(); } @@ -196,11 +197,9 @@ public function testPointlessGetSizeReturnsNull() $this->assertEquals(null, $this->stream->getSize()); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessTellThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->tell(); } @@ -209,19 +208,15 @@ public function testPointlessIsSeekableReturnsFalse() $this->assertEquals(false, $this->stream->isSeekable()); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessSeekThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->seek(0); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessRewindThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->rewind(); } @@ -230,27 +225,21 @@ public function testPointlessIsWritableReturnsFalse() $this->assertEquals(false, $this->stream->isWritable()); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessWriteThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->write(''); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessReadThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->read(8192); } - /** - * @expectedException BadMethodCallException - */ public function testPointlessGetContentsThrows() { + $this->setExpectedException('BadMethodCallException'); $this->stream->getContents(); } diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..7f1324f --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,59 @@ +expectException($exception); + if ($exceptionMessage !== '') { + $this->expectExceptionMessage($exceptionMessage); + } + if ($exceptionCode !== null) { + $this->expectExceptionCode($exceptionCode); + } + } else { + // legacy PHPUnit 4 + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); + } + } + + protected function expectCallableOnce() + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke'); + + return $mock; + } + + protected function expectCallableOnceWith($value) + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($value); + + return $mock; + } + + protected function expectCallableNever() + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->never()) + ->method('__invoke'); + + return $mock; + } + + protected function createCallableMock() + { + return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); + } +}