diff --git a/README.md b/README.md index 7ffbec0..6544b3b 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,11 @@ In order to be able to establish several connections at the same time, [react/so API to establish simple connections in an async (non-blocking) way. This project includes several classes that extend this base functionality by implementing the same simple `ConnectorInterface`. -This interface provides a single promise-based method `create($host, $ip)` which can be used to easily notify +This interface provides a single promise-based method `connect($uri)` which can be used to easily notify when the connection is successfully established or the `Connector` gives up and the connection fails. ```php -$connector->create('www.google.com', 80)->then(function ($stream) { +$connector->connect('www.google.com:80')->then(function ($stream) { echo 'connection successfully established'; $stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n"); $stream->end(); @@ -70,7 +70,7 @@ and then retry up to 2 times if the connection attempt fails: ```php $connectorRepeater = new ConnectionManagerRepeat($connector, 3); -$connectorRepeater->create('www.google.com', 80)->then(function ($stream) { +$connectorRepeater->connect('www.google.com:80')->then(function ($stream) { echo 'connection successfully established'; $stream->close(); }); diff --git a/composer.json b/composer.json index 3ea93a3..c19078f 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ }, "require": { "php": ">=5.3", - "react/socket-client": "^0.5 || ^0.4 || ^0.3", + "react/socket-client": "^0.7 || ^0.6", "react/event-loop": "^0.4 || ^0.3", "react/promise": "^2.1 || ^1.2", "react/promise-timer": "^1.1" diff --git a/src/ConnectionManagerDelay.php b/src/ConnectionManagerDelay.php index aa2f7bd..2e6a5e5 100644 --- a/src/ConnectionManagerDelay.php +++ b/src/ConnectionManagerDelay.php @@ -19,12 +19,12 @@ public function __construct(ConnectorInterface $connectionManager, $delay, LoopI $this->loop = $loop; } - public function create($host, $port) + public function connect($uri) { $connectionManager = $this->connectionManager; - return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $host, $port) { - return $connectionManager->create($host, $port); + return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $uri) { + return $connectionManager->connect($uri); }); } } diff --git a/src/ConnectionManagerReject.php b/src/ConnectionManagerReject.php index 38b1ff8..f471043 100644 --- a/src/ConnectionManagerReject.php +++ b/src/ConnectionManagerReject.php @@ -9,7 +9,7 @@ // a simple connection manager that rejects every single connection attempt class ConnectionManagerReject implements ConnectorInterface { - public function create($host, $port) + public function connect($_) { return Promise\reject(new Exception('Connection rejected')); } diff --git a/src/ConnectionManagerRepeat.php b/src/ConnectionManagerRepeat.php index 77b1566..ddfc320 100644 --- a/src/ConnectionManagerRepeat.php +++ b/src/ConnectionManagerRepeat.php @@ -22,16 +22,16 @@ public function __construct(ConnectorInterface $connectionManager, $maximumTries $this->maximumTries = $maximumTries; } - public function create($host, $port) + public function connect($uri) { $tries = $this->maximumTries; $connector = $this->connectionManager; - return new Promise(function ($resolve, $reject) use ($host, $port, &$pending, &$tries, $connector) { - $try = function ($error = null) use (&$try, &$pending, &$tries, $host, $port, $connector, $resolve, $reject) { + return new Promise(function ($resolve, $reject) use ($uri, &$pending, &$tries, $connector) { + $try = function ($error = null) use (&$try, &$pending, &$tries, $uri, $connector, $resolve, $reject) { if ($tries > 0) { --$tries; - $pending = $connector->create($host, $port); + $pending = $connector->connect($uri); $pending->then($resolve, $try); } else { $reject(new Exception('Connection still fails even after retrying', 0, $error)); diff --git a/src/ConnectionManagerSwappable.php b/src/ConnectionManagerSwappable.php index 0b73858..4dece85 100644 --- a/src/ConnectionManagerSwappable.php +++ b/src/ConnectionManagerSwappable.php @@ -14,9 +14,9 @@ public function __construct(ConnectorInterface $connectionManager) $this->connectionManager = $connectionManager; } - public function create($host, $port) + public function connect($uri) { - return $this->connectionManager->create($host, $port); + return $this->connectionManager->connect($uri); } public function setConnectionManager(ConnectorInterface $connectionManager) diff --git a/src/ConnectionManagerTimeout.php b/src/ConnectionManagerTimeout.php index 11614dd..b78703f 100644 --- a/src/ConnectionManagerTimeout.php +++ b/src/ConnectionManagerTimeout.php @@ -20,9 +20,9 @@ public function __construct(ConnectorInterface $connectionManager, $timeout, Loo $this->loop = $loop; } - public function create($host, $port) + public function connect($uri) { - $promise = $this->connectionManager->create($host, $port); + $promise = $this->connectionManager->connect($uri); return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) { // connection successfully established but timeout already expired => close successful connection diff --git a/src/Multiple/ConnectionManagerConcurrent.php b/src/Multiple/ConnectionManagerConcurrent.php index 3b87d46..c1eb9cf 100644 --- a/src/Multiple/ConnectionManagerConcurrent.php +++ b/src/Multiple/ConnectionManagerConcurrent.php @@ -8,12 +8,12 @@ class ConnectionManagerConcurrent extends ConnectionManagerConsecutive { - public function create($host, $port) + public function connect($uri) { $all = array(); foreach ($this->managers as $connector) { /* @var $connection Connector */ - $all []= $connector->create($host, $port); + $all []= $connector->connect($uri); } return Promise\any($all)->then(function ($conn) use ($all) { // a connection attempt succeeded diff --git a/src/Multiple/ConnectionManagerConsecutive.php b/src/Multiple/ConnectionManagerConsecutive.php index c5ab1d3..3459742 100644 --- a/src/Multiple/ConnectionManagerConsecutive.php +++ b/src/Multiple/ConnectionManagerConsecutive.php @@ -23,29 +23,28 @@ public function __construct(array $managers) $this->managers = $managers; } - public function create($host, $port) + public function connect($uri) { - return $this->tryConnection($this->managers, $host, $port); + return $this->tryConnection($this->managers, $uri); } /** * * @param ConnectorInterface[] $managers - * @param string $host - * @param int $port + * @param string $uri * @return Promise * @internal */ - public function tryConnection(array $managers, $host, $port) + public function tryConnection(array $managers, $uri) { - return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $host, $port) { - $try = function () use (&$try, &$managers, $host, $port, $resolve, $reject, &$pending) { + return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $uri) { + $try = function () use (&$try, &$managers, $uri, $resolve, $reject, &$pending) { if (!$managers) { return $reject(new UnderflowException('No more managers to try to connect through')); } $manager = array_shift($managers); - $pending = $manager->create($host, $port); + $pending = $manager->connect($uri); $pending->then($resolve, $try); }; diff --git a/src/Multiple/ConnectionManagerRandom.php b/src/Multiple/ConnectionManagerRandom.php index e79489f..88d1fd6 100644 --- a/src/Multiple/ConnectionManagerRandom.php +++ b/src/Multiple/ConnectionManagerRandom.php @@ -4,11 +4,11 @@ class ConnectionManagerRandom extends ConnectionManagerConsecutive { - public function create($host, $port) + public function connect($uri) { $managers = $this->managers; shuffle($managers); - - return $this->tryConnection($managers, $host, $port); + + return $this->tryConnection($managers, $uri); } } diff --git a/src/Multiple/ConnectionManagerSelective.php b/src/Multiple/ConnectionManagerSelective.php index 28d62d5..560ce47 100644 --- a/src/Multiple/ConnectionManagerSelective.php +++ b/src/Multiple/ConnectionManagerSelective.php @@ -63,14 +63,22 @@ public function __construct(array $managers) $this->managers = $managers; } - public function create($host, $port) + public function connect($uri) { try { - $connector = $this->getConnectorForTarget($host, $port); + $parts = parse_url('tcp://' . $uri); + if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) { + throw new InvalidArgumentException('Invalid URI'); + } + + $connector = $this->getConnectorForTarget( + trim($parts['host'], '[]'), + $parts['port'] + ); } catch (UnderflowException $e) { return Promise\reject($e); } - return $connector->create($host, $port); + return $connector->connect($uri); } private function getConnectorForTarget($targetHost, $targetPort) diff --git a/tests/ConnectionManagerDelayTest.php b/tests/ConnectionManagerDelayTest.php index d102837..860ea4a 100644 --- a/tests/ConnectionManagerDelayTest.php +++ b/tests/ConnectionManagerDelayTest.php @@ -17,7 +17,7 @@ public function testDelayTenth() $will = $this->createConnectionManagerMock(true); $cm = new ConnectionManagerDelay($will, 0.1, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $this->loop->run(); @@ -27,11 +27,11 @@ public function testDelayTenth() public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection() { $unused = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $unused->expects($this->never())->method('create'); + $unused->expects($this->never())->method('connect'); $cm = new ConnectionManagerDelay($unused, 1.0, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $promise->cancel(); $this->loop->run(); diff --git a/tests/ConnectionManagerRejectTest.php b/tests/ConnectionManagerRejectTest.php index e9ba35e..d7bf0d8 100644 --- a/tests/ConnectionManagerRejectTest.php +++ b/tests/ConnectionManagerRejectTest.php @@ -7,7 +7,7 @@ class ConnectionManagerRejectTest extends TestCase public function testReject() { $cm = new ConnectionManagerReject(); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); diff --git a/tests/ConnectionManagerRepeatTest.php b/tests/ConnectionManagerRepeatTest.php index 37a5707..8ea7965 100644 --- a/tests/ConnectionManagerRepeatTest.php +++ b/tests/ConnectionManagerRepeatTest.php @@ -10,7 +10,7 @@ public function testRepeatRejected() { $wont = new ConnectionManagerReject(); $cm = new ConnectionManagerRepeat($wont, 3); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); @@ -22,11 +22,11 @@ public function testTwoTriesWillStartTwoConnectionAttempts() $promise = Promise\reject(new \RuntimeException('nope')); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($promise); + $connector->expects($this->exactly(2))->method('connect')->with('google.com:80')->willReturn($promise); $cm = new ConnectionManagerRepeat($connector, 2); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $this->assertPromiseReject($promise); } @@ -45,11 +45,11 @@ public function testCancellationWillNotStartAnyFurtherConnections() $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $cm = new ConnectionManagerRepeat($connector, 3); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $promise->cancel(); } @@ -60,11 +60,11 @@ public function testCancellationWillNotStartAnyFurtherConnectionsIfPromiseReject }); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $cm = new ConnectionManagerRepeat($connector, 3); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $promise->cancel(); } } diff --git a/tests/ConnectionManagerSwappableTest.php b/tests/ConnectionManagerSwappableTest.php index ea3d29c..cd9418f 100644 --- a/tests/ConnectionManagerSwappableTest.php +++ b/tests/ConnectionManagerSwappableTest.php @@ -10,14 +10,14 @@ public function testSwap() $wont = new ConnectionManagerReject(); $cm = new ConnectionManagerSwappable($wont); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); $will = $this->createConnectionManagerMock(true); $cm->setConnectionManager($will); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); } diff --git a/tests/ConnectionManagerTimeoutTest.php b/tests/ConnectionManagerTimeoutTest.php index 9288a88..1408659 100644 --- a/tests/ConnectionManagerTimeoutTest.php +++ b/tests/ConnectionManagerTimeoutTest.php @@ -22,7 +22,7 @@ public function testTimeoutOkay() $will = $this->createConnectionManagerMock(true); $cm = new ConnectionManagerTimeout($will, 0.1, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $this->loop->run(); @@ -31,12 +31,12 @@ public function testTimeoutOkay() public function testTimeoutExpire() { - $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->loop)); + $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r+'), $this->loop)); $wont = new ConnectionManagerDelay($will, 0.2, $this->loop); $cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $this->loop->run(); @@ -49,7 +49,7 @@ public function testTimeoutAbort() $cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $this->loop->run(); @@ -58,7 +58,7 @@ public function testTimeoutAbort() public function testWillEndConnectionIfConnectionResolvesDespiteTimeout() { - $stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock(); + $stream = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->disableOriginalConstructor()->getMock(); $stream->expects($this->once())->method('end'); $loop = $this->loop; @@ -69,11 +69,11 @@ public function testWillEndConnectionIfConnectionResolvesDespiteTimeout() }); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise); + $connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise); $cm = new ConnectionManagerTimeout($connector, 0.001, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->loop->run(); @@ -87,11 +87,11 @@ public function testCancellationOfPromiseWillCancelConnectionAttempt() }); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise); + $connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise); $cm = new ConnectionManagerTimeout($connector, 5.0, $this->loop); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $promise->cancel(); $this->loop->run(); diff --git a/tests/Multiple/ConnectionManagerConcurrentTest.php b/tests/Multiple/ConnectionManagerConcurrentTest.php index 41ea551..ad7689b 100644 --- a/tests/Multiple/ConnectionManagerConcurrentTest.php +++ b/tests/Multiple/ConnectionManagerConcurrentTest.php @@ -18,11 +18,11 @@ public function testWillForwardToInnerConnector() $pending = new Promise\Promise(function() { }); $only = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $only->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $only->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $connector = new ConnectionManagerConcurrent(array($only)); - $promise = $connector->create('google.com', 80); + $promise = $connector->connect('google.com:80'); $promise->then($this->expectCallableNever(), $this->expectCallableNever()); } @@ -31,15 +31,15 @@ public function testWillCancelOtherIfOneResolves() { $resolved = Promise\resolve($this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock()); $first = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $first->expects($this->once())->method('create')->with('google.com', 80)->willReturn($resolved); + $first->expects($this->once())->method('connect')->with('google.com:80')->willReturn($resolved); $pending = new Promise\Promise(function() { }, $this->expectCallableOnce()); $second = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $second->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $second->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $connector = new ConnectionManagerConcurrent(array($first, $second)); - $promise = $connector->create('google.com', 80); + $promise = $connector->connect('google.com:80'); $this->assertPromiseResolve($promise); } @@ -48,16 +48,16 @@ public function testWillCloseOtherIfOneResolves() { $resolved = Promise\resolve($this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock()); $first = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $first->expects($this->once())->method('create')->with('google.com', 80)->willReturn($resolved); + $first->expects($this->once())->method('connect')->with('google.com:80')->willReturn($resolved); $slower = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock(); $slower->expects($this->once())->method('close'); $second = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $second->expects($this->once())->method('create')->with('google.com', 80)->willReturn(Promise\resolve($slower)); + $second->expects($this->once())->method('connect')->with('google.com:80')->willReturn(Promise\resolve($slower)); $connector = new ConnectionManagerConcurrent(array($first, $second)); - $promise = $connector->create('google.com', 80); + $promise = $connector->connect('google.com:80'); $this->assertPromiseResolve($promise); } diff --git a/tests/Multiple/ConnectionManagerConsecutiveTest.php b/tests/Multiple/ConnectionManagerConsecutiveTest.php index d28ad9c..ad5bc96 100644 --- a/tests/Multiple/ConnectionManagerConsecutiveTest.php +++ b/tests/Multiple/ConnectionManagerConsecutiveTest.php @@ -20,7 +20,7 @@ public function testReject() $cm = new ConnectionManagerConsecutive(array($wont)); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); @@ -32,11 +32,11 @@ public function testWillTryAllIfEachRejects() $rejected = Promise\reject(new \RuntimeException('nope')); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($rejected); + $connector->expects($this->exactly(2))->method('connect')->with('google.com:80')->willReturn($rejected); $cm = new ConnectionManagerConsecutive(array($connector, $connector)); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $this->assertPromiseReject($promise); } @@ -46,11 +46,11 @@ public function testCancellationWillNotStartAnyFurtherConnections() $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $cm = new ConnectionManagerConsecutive(array($connector, $connector)); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $promise->cancel(); } } diff --git a/tests/Multiple/ConnectionManagerRandomTest.php b/tests/Multiple/ConnectionManagerRandomTest.php index 37aaaef..f75f331 100644 --- a/tests/Multiple/ConnectionManagerRandomTest.php +++ b/tests/Multiple/ConnectionManagerRandomTest.php @@ -20,7 +20,7 @@ public function testReject() $cm = new ConnectionManagerRandom(array($wont)); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); @@ -32,11 +32,11 @@ public function testWillTryAllIfEachRejects() $rejected = Promise\reject(new \RuntimeException('nope')); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($rejected); + $connector->expects($this->exactly(2))->method('connect')->with('google.com:80')->willReturn($rejected); $cm = new ConnectionManagerRandom(array($connector, $connector)); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $this->assertPromiseReject($promise); } @@ -46,11 +46,11 @@ public function testCancellationWillNotStartAnyFurtherConnections() $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending); + $connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending); $cm = new ConnectionManagerRandom(array($connector, $connector)); - $promise = $cm->create('google.com', 80); + $promise = $cm->connect('google.com:80'); $promise->cancel(); } } diff --git a/tests/Multiple/ConnectionManagerSelectiveTest.php b/tests/Multiple/ConnectionManagerSelectiveTest.php index 450aab2..a51f139 100644 --- a/tests/Multiple/ConnectionManagerSelectiveTest.php +++ b/tests/Multiple/ConnectionManagerSelectiveTest.php @@ -11,7 +11,7 @@ public function testEmptyWillAlwaysReject() { $cm = new ConnectionManagerSelective(array()); - $promise = $cm->create('www.google.com', 80); + $promise = $cm->connect('www.google.com:80'); $this->assertPromiseReject($promise); } @@ -81,13 +81,13 @@ public function testExactDomainMatchForwardsToConnector() $promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock(); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('example.com', 80)->willReturn($promise); + $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise); $cm = new ConnectionManagerSelective(array( 'example.com' => $connector )); - $ret = $cm->create('example.com' , 80); + $ret = $cm->connect('example.com:80'); $this->assertSame($promise, $ret); } @@ -97,13 +97,13 @@ public function testExactIpv6MatchForwardsToConnector() $promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock(); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('::1', 80)->willReturn($promise); + $connector->expects($this->once())->method('connect')->with('[::1]:80')->willReturn($promise); $cm = new ConnectionManagerSelective(array( '::1' => $connector )); - $ret = $cm->create('::1' , 80); + $ret = $cm->connect('[::1]:80'); $this->assertSame($promise, $ret); } @@ -113,13 +113,13 @@ public function testExactIpv6WithPortMatchForwardsToConnector() $promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock(); $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->once())->method('create')->with('::1', 80)->willReturn($promise); + $connector->expects($this->once())->method('connect')->with('[::1]:80')->willReturn($promise); $cm = new ConnectionManagerSelective(array( '[::1]:80' => $connector )); - $ret = $cm->create('::1' , 80); + $ret = $cm->connect('[::1]:80'); $this->assertSame($promise, $ret); } @@ -127,50 +127,50 @@ public function testExactIpv6WithPortMatchForwardsToConnector() public function testNotMatchingDomainWillReject() { $connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); - $connector->expects($this->never())->method('create'); + $connector->expects($this->never())->method('connect'); $cm = new ConnectionManagerSelective(array( 'example.com' => $connector )); - $promise = $cm->create('other.example.com' , 80); + $promise = $cm->connect('other.example.com:80'); $this->assertPromiseReject($promise); } public function testReject() { - $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock())); + $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r+'), $this->createLoopMock())); $cm = new ConnectionManagerSelective(array( 'www.google.com:443' => $will, 'www.youtube.com' => $will )); - $this->assertPromiseResolve($cm->create('www.google.com', 443)); + $this->assertPromiseResolve($cm->connect('www.google.com:443')); - $this->assertPromiseReject($cm->create('www.google.com', 80)); + $this->assertPromiseReject($cm->connect('www.google.com:80')); - $this->assertPromiseResolve($cm->create('www.youtube.com', 80)); + $this->assertPromiseResolve($cm->connect('www.youtube.com:80')); } public function testFirstEntryWinsIfMultipleMatch() { $wont = new ConnectionManagerReject(); - $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock())); + $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r+'), $this->createLoopMock())); $cm = new ConnectionManagerSelective(array( 'www.google.com:443' => $will, '*' => $wont )); - $this->assertPromiseResolve($cm->create('www.google.com', 443)); - $this->assertPromiseReject($cm->create('www.google.com', 80)); + $this->assertPromiseResolve($cm->connect('www.google.com:443')); + $this->assertPromiseReject($cm->connect('www.google.com:80')); } public function testWildcardsMatch() { - $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock())); + $will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r+'), $this->createLoopMock())); $cm = new ConnectionManagerSelective(array( '*.com' => $will, @@ -180,17 +180,17 @@ public function testWildcardsMatch() 'youtube.*' => $will, )); - $this->assertPromiseResolve($cm->create('www.google.com', 80)); - $this->assertPromiseReject($cm->create('www.google.de', 80)); + $this->assertPromiseResolve($cm->connect('www.google.com:80')); + $this->assertPromiseReject($cm->connect('www.google.de:80')); - $this->assertPromiseResolve($cm->create('www.google.de', 443)); - $this->assertPromiseResolve($cm->create('www.google.de', 444)); - $this->assertPromiseResolve($cm->create('www.google.de', 8080)); - $this->assertPromiseReject($cm->create('www.google.de', 445)); + $this->assertPromiseResolve($cm->connect('www.google.de:443')); + $this->assertPromiseResolve($cm->connect('www.google.de:444')); + $this->assertPromiseResolve($cm->connect('www.google.de:8080')); + $this->assertPromiseReject($cm->connect('www.google.de:445')); - $this->assertPromiseResolve($cm->create('www.youtube.de', 80)); - $this->assertPromiseResolve($cm->create('download.youtube.de', 80)); - $this->assertPromiseResolve($cm->create('youtube.de', 80)); + $this->assertPromiseResolve($cm->connect('www.youtube.de:80')); + $this->assertPromiseResolve($cm->connect('download.youtube.de:80')); + $this->assertPromiseResolve($cm->connect('youtube.de:80')); } private function createLoopMock() diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d7f376c..3b379f3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -58,8 +58,7 @@ protected function createCallableMock() protected function createConnectionManagerMock($ret) { - $mock = $this->getMockBuilder('React\SocketClient\Connector') - ->disableOriginalConstructor() + $mock = $this->getMockBuilder('React\SocketClient\ConnectorInterface') ->getMock(); $deferred = new Deferred(); @@ -67,7 +66,7 @@ protected function createConnectionManagerMock($ret) $mock ->expects($this->any()) - ->method('create') + ->method('connect') ->will($this->returnValue($deferred->promise())); return $mock;