diff --git a/README.md b/README.md index bcd2b26..e64b07b 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,9 @@ It helps with establishing a plain TCP/IP or secure SSL/TLS connection to the AM and issuing an initial `login` action. ```php -$factory->createClient('user:secret@localhost')->then( +$factory->createClient($amiUrl)->then( function (Client $client) { - // client connected and authenticated + // client connected (and authenticated) }, function (Exception $e) { // an error occured while trying to connect or authorize client @@ -114,8 +114,29 @@ $factory->createClient('user:secret@localhost')->then( ); ``` -> Note: The given $amiUrl *must* include a host, it *should* include a username and secret -> and it *can* include a scheme (tcp/ssl) and port definition. +The `$amiUrl` contains the host and optional port to connect to: + +```php +$factory->createClient('127.0.0.1:5038'); +``` + +> If the `$amiUrl` is `null` (or omitted) this method defaults to connecting + to your local host (`127.0.0.1:5038`). + +The above examples to not pass any authentication details, so you may have to +call `ActionSender::login()` after connecting or use the recommended shortcut +to pass a username and secret for your AMI login details like this: + +```php +$factory->createClient('user:secret@localhost'); +``` + +The `Factory` defaults to establishing a plaintext TCP connection. +If you want to connect through a secure TLS proxy, you can use the `tls` scheme: + +```php +$factory->createClient('tls://user:secret@localhost:12345'); +``` ### Client diff --git a/composer.json b/composer.json index 432fdcd..b28f62a 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,9 @@ ], "require": { "php": ">=5.3", + "evenement/evenement": "~1.0|~2.0", "react/promise": "~1.0|~2.0", - "react/socket-client": "0.3.*|0.4.*", + "react/socket-client": "^0.5 || ^0.4 || ^0.3", "react/event-loop": "0.3.*|0.4.*" }, "require-dev": { diff --git a/src/Factory.php b/src/Factory.php index 5e18aab..36884f3 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -3,6 +3,7 @@ namespace Clue\React\Ami; use React\EventLoop\LoopInterface; +use React\SocketClient\ConnectorInterface; use React\SocketClient\Connector; use React\SocketClient\SecureConnector; use React\Dns\Resolver\Factory as ResolverFactory; @@ -15,7 +16,7 @@ class Factory private $connector; private $secureConnector; - public function __construct(LoopInterface $loop, Connector $connector = null, SecureConnector $secureConnector = null) + public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ConnectorInterface $secureConnector = null) { if ($connector === null) { $resolverFactory = new ResolverFactory(); @@ -34,7 +35,7 @@ public function createClient($address = null) { $parts = $this->parseUrl($address); - $secure = (isset($parts['schema']) && $parts['schema'] !== 'tcp'); + $secure = (isset($parts['scheme']) && $parts['scheme'] !== 'tcp'); $connector = $secure ? $this->secureConnector : $this->connector; $promise = $connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php new file mode 100644 index 0000000..33d303f --- /dev/null +++ b/tests/FactoryTest.php @@ -0,0 +1,49 @@ +loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->tcp = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); + $this->tls = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); + + $this->factory = new Factory($this->loop, $this->tcp, $this->tls); + } + + public function testDefaultCtor() + { + $this->factory = new Factory($this->loop); + } + + public function testCreateClientUsesTcpConnectorWithDefaultLocation() + { + $promise = new Promise(function () { }); + $this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise); + + $this->factory->createClient(); + } + + public function testCreateClientUsesTcpConnectorWithLocalhostLocation() + { + $promise = new Promise(function () { }); + $this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise); + + $this->factory->createClient('localhost'); + } + + public function testCreateClientUsesTlsConnectorWithTlsLocation() + { + $promise = new Promise(function () { }); + $this->tls->expects($this->once())->method('create')->with('ami.local', 1234)->willReturn($promise); + + $this->factory->createClient('tls://ami.local:1234'); + } +}