Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clue/block-react in order to simplify tests #53

Merged
merged 2 commits into from
Mar 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
"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"
},
"require-dev": {
"clue/block-react": "~1.0"
}
}
60 changes: 23 additions & 37 deletions tests/FunctionalBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand All @@ -67,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()
Expand All @@ -93,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()
Expand All @@ -119,29 +109,25 @@ 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()
{
$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());
}
}
}
15 changes: 5 additions & 10 deletions tests/Io/SenderTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Clue\React\Buzz\Io\Sender;
use React\Promise\Deferred;
use Clue\React\Buzz\Message\Request;
use React\Promise;
use Clue\React\Block;

class SenderTest extends TestCase
{
Expand Down Expand Up @@ -39,21 +40,15 @@ public function testCreateFromLoopUnix()
public function testSenderRejection()
{
$connector = $this->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);

$request = new Request('GET', 'http://www.google.com/');

$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);
}
}
18 changes: 11 additions & 7 deletions tests/Io/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}
}
8 changes: 6 additions & 2 deletions tests/Io/UnixConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Clue\React\Buzz\Io\UnixConnector;
use React\EventLoop\Factory as LoopFactory;
use Clue\React\Block;

class UnixConnectorTest extends TestCase
{
Expand All @@ -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()
Expand All @@ -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);
Expand Down
85 changes: 1 addition & 84 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,92 +1,9 @@
<?php

use React\Promise\Deferred;

require_once __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';

error_reporting(-1);

class TestCase extends PHPUnit_Framework_TestCase
{
protected function expectCallableOnce()
{
$mock = $this->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()
{
}
}