From ee507db2942e11332a69d5eb28023f27201ec0bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 1 Mar 2016 22:14:09 +0100 Subject: [PATCH 1/2] Use clue/block-react in order to simplify tests --- composer.json | 3 +++ tests/FunctionalBrowserTest.php | 47 +++++++++++++-------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 8497df6..972010c 100644 --- a/composer.json +++ b/composer.json @@ -22,5 +22,8 @@ "react/promise": "1.*|2.*", "rize/uri-template": "~0.3.0", "ml/iri": "~1.0" + }, + "require-dev": { + "clue/block-react": "~1.0" } } diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index adcf25d..5aaafe7 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -7,6 +7,8 @@ use React\SocketClient\SecureConnector; use React\SocketClient\TcpConnector; use React\SocketClient\DnsConnector; +use Clue\React\Buzz\Message\ResponseException; +use Clue\React\Block; class FunctionalBrowserTest extends TestCase { @@ -24,41 +26,32 @@ public function setUp() public function testSimpleRequest() { - $this->expectPromiseResolve($this->browser->get($this->base . 'get')); - - $this->loop->run(); + Block\await($this->browser->get($this->base . 'get'), $this->loop); } public function testRedirectRequestRelative() { - $this->expectPromiseResolve($this->browser->get($this->base . 'redirect-to?url=get')); - - $this->loop->run(); + Block\await($this->browser->get($this->base . 'redirect-to?url=get'), $this->loop); } public function testRedirectRequestAbsolute() { - $this->expectPromiseResolve($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get'))); - - $this->loop->run(); + Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get')), $this->loop); } public function testNotFollowingRedirectsResolvesWithRedirectResult() { $browser = $this->browser->withOptions(array('followRedirects' => false)); - $this->expectPromiseResolve($browser->get($this->base . 'redirect/3')); - - $this->loop->run(); + Block\await($browser->get($this->base . 'redirect/3'), $this->loop); } public function testRejectingRedirectsRejects() { $browser = $this->browser->withOptions(array('maxRedirects' => 0)); - $this->expectPromiseReject($browser->get($this->base . 'redirect/3')); - - $this->loop->run(); + $this->setExpectedException('RuntimeException'); + Block\await($browser->get($this->base . 'redirect/3'), $this->loop); } public function testCanAccessHttps() @@ -126,22 +119,20 @@ public function testVerifyPeerDisabledForBadSslResolves() public function testInvalidPort() { - $this->expectPromiseReject($this->browser->get('http://www.google.com:443/')); - - $this->loop->run(); + $this->setExpectedException('RuntimeException'); + Block\await($this->browser->get('http://www.google.com:443/'), $this->loop); } public function testErrorStatusCodeRejectsWithResponseException() { - $that = $this; - $this->expectPromiseReject($this->browser->get($this->base . 'status/404'))->then(null, function ($e) use ($that) { - $that->assertInstanceOf('Clue\Buzz\React\Message\ResponseException', $e); - $that->assertEquals(404, $e->getCode()); - - $that->assertInstanceOf('Clue\Buzz\React\Message\Response', $e->getResponse()); - $that->assertEquals(404, $e->getResponse()->getCode()); - }); - - $this->loop->run(); + try { + Block\await($this->browser->get($this->base . 'status/404'), $this->loop); + $this->fail(); + } catch (ResponseException $e) { + $this->assertEquals(404, $e->getCode()); + + $this->assertInstanceOf('Clue\React\Buzz\Message\Response', $e->getResponse()); + $this->assertEquals(404, $e->getResponse()->getCode()); + } } } From 8461a80a8a0385f68da6989a8747f7928c123b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 25 Mar 2016 13:55:42 +0100 Subject: [PATCH 2/2] Simplify tests by relying on new promise API --- composer.json | 2 +- tests/FunctionalBrowserTest.php | 13 ++--- tests/Io/SenderTest.php | 15 ++---- tests/Io/TransactionTest.php | 18 ++++--- tests/Io/UnixConnectorTest.php | 8 +++- tests/bootstrap.php | 85 +-------------------------------- 6 files changed, 28 insertions(+), 113 deletions(-) diff --git a/composer.json b/composer.json index 972010c..c993b2e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "react/http-client": "0.3.*|0.4.*", "react/socket-client": "^0.5 || ^0.4 || ^0.3", "react/dns": "0.3.*|0.4.*", - "react/promise": "1.*|2.*", + "react/promise": "^2 || ^1.1", "rize/uri-template": "~0.3.0", "ml/iri": "~1.0" }, diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index 5aaafe7..a03e839 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -60,9 +60,7 @@ public function testCanAccessHttps() $this->markTestSkipped('Not supported on your platform (outdated HHVM?)'); } - $this->expectPromiseResolve($this->browser->get('https://www.google.com/')); - - $this->loop->run(); + Block\await($this->browser->get('https://www.google.com/'), $this->loop); } public function testVerifyPeerEnabledForBadSslRejects() @@ -86,9 +84,8 @@ public function testVerifyPeerEnabledForBadSslRejects() $sender = Sender::createFromLoopConnectors($this->loop, $tcp, $ssl); $browser = $this->browser->withSender($sender); - $this->expectPromiseReject($browser->get('https://self-signed.badssl.com/')); - - $this->loop->run(); + $this->setExpectedException('RuntimeException'); + Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop); } public function testVerifyPeerDisabledForBadSslResolves() @@ -112,9 +109,7 @@ public function testVerifyPeerDisabledForBadSslResolves() $sender = Sender::createFromLoopConnectors($this->loop, $tcp, $ssl); $browser = $this->browser->withSender($sender); - $this->expectPromiseResolve($browser->get('https://self-signed.badssl.com/')); - - $this->loop->run(); + Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop); } public function testInvalidPort() diff --git a/tests/Io/SenderTest.php b/tests/Io/SenderTest.php index 96179e5..c02bf7c 100644 --- a/tests/Io/SenderTest.php +++ b/tests/Io/SenderTest.php @@ -1,8 +1,9 @@ getMock('React\SocketClient\ConnectorInterface'); - $connector->expects($this->once())->method('create')->will($this->returnValue($this->createRejected(new RuntimeException('Rejected')))); + $connector->expects($this->once())->method('create')->willReturn(Promise\reject(new RuntimeException('Rejected'))); $sender = Sender::createFromLoopConnectors($this->loop, $connector); @@ -47,13 +48,7 @@ public function testSenderRejection() $promise = $sender->send($request); - $this->expectPromiseReject($promise); - } - - private function createRejected($value) - { - $deferred = new Deferred(); - $deferred->reject($value); - return $deferred->promise(); + $this->setExpectedException('RuntimeException'); + Block\await($promise, $this->loop); } } diff --git a/tests/Io/TransactionTest.php b/tests/Io/TransactionTest.php index f0e999c..ae54ac4 100644 --- a/tests/Io/TransactionTest.php +++ b/tests/Io/TransactionTest.php @@ -2,6 +2,9 @@ use Clue\React\Buzz\Io\Transaction; use Clue\React\Buzz\Message\Response; +use Clue\React\Buzz\Message\ResponseException; +use React\Promise; +use Clue\React\Block; class TransactionTest extends TestCase { @@ -12,16 +15,17 @@ public function testReceivingErrorResponseWillRejectWithResponseException() // mock sender to resolve promise with the given $response in response to the given $request $sender = $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock(); - $sender->expects($this->once())->method('send')->with($this->equalTo($request))->will($this->returnValue($this->createPromiseResolved($response))); + $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response)); $transaction = new Transaction($request, $sender); $promise = $transaction->send(); - $that = $this; - $this->expectPromiseReject($promise)->then(null, function ($exception) use ($that, $response) { - $that->assertInstanceOf('Clue\React\Buzz\Message\ResponseException', $exception); - $that->assertEquals(404, $exception->getCode()); - $that->assertSame($response, $exception->getResponse()); - }); + try { + Block\await($promise, $this->getMock('React\EventLoop\LoopInterface')); + $this->fail(); + } catch (ResponseException $exception) { + $this->assertEquals(404, $exception->getCode()); + $this->assertSame($response, $exception->getResponse()); + } } } diff --git a/tests/Io/UnixConnectorTest.php b/tests/Io/UnixConnectorTest.php index 74b6a80..2591ff5 100644 --- a/tests/Io/UnixConnectorTest.php +++ b/tests/Io/UnixConnectorTest.php @@ -2,6 +2,7 @@ use Clue\React\Buzz\Io\UnixConnector; use React\EventLoop\Factory as LoopFactory; +use Clue\React\Block; class UnixConnectorTest extends TestCase { @@ -14,7 +15,8 @@ public function testInvalid() $promise = $connector->create('localhost', 80); - $this->expectPromiseReject($promise); + $this->setExpectedException('RuntimeException'); + Block\await($promise, $loop); } public function testValid() @@ -33,7 +35,9 @@ public function testValid() $promise = $connector->create('localhost', 80); - $this->expectPromiseResolve($promise); + $stream = Block\await($promise, $loop); + /* @var $stream React\Stream\Stream */ + $stream->close(); fclose($server); unlink($path); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f45ddf2..c09de72 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,92 +1,9 @@ createCallableMock(); - - - if (func_num_args() > 0) { - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->equalTo(func_get_arg(0))); - } else { - $mock - ->expects($this->once()) - ->method('__invoke'); - } - - return $mock; - } - - protected function expectCallableNever() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->never()) - ->method('__invoke'); - - return $mock; - } - - protected function expectCallableOnceParameter($type) - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->isInstanceOf($type)); - - return $mock; - } - - /** - * @link https://github.com/reactphp/react/blob/master/tests/React/Tests/Socket/TestCase.php (taken from reactphp/react) - */ - protected function createCallableMock() - { - return $this->getMock('CallableStub'); - } - - protected function expectPromiseResolve($promise) - { - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - - $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); - - return $promise; - } - - protected function expectPromiseReject($promise) - { - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - - $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); - - return $promise; - } - - protected function createPromiseResolved($value = null) - { - $deferred = new Deferred(); - $deferred->resolve($value); - - return $deferred->promise(); - } } - -class CallableStub -{ - public function __invoke() - { - } -} -