Skip to content

Commit ad5d081

Browse files
committed
Improve test coverage for Factory
1 parent 0f9bc91 commit ad5d081

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

src/Factory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
*/
3838
class Factory
3939
{
40-
private $loop;
4140
private $connector;
4241

4342
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
@@ -46,7 +45,6 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
4645
$connector = new Connector($loop);
4746
}
4847

49-
$this->loop = $loop;
5048
$this->connector = $connector;
5149
}
5250

@@ -130,7 +128,7 @@ public function createClient($url)
130128
$sender = new ActionSender($client);
131129

132130
return $sender->login(rawurldecode($parts['user']), rawurldecode($parts['pass']))->then(
133-
function ($response) use ($client) {
131+
function () use ($client) {
134132
return $client;
135133
},
136134
function ($error) use ($client) {

tests/FactoryTest.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class FactoryTest extends TestCase
99
{
10-
private $loop;
1110
private $tcp;
1211
private $factory;
1312

@@ -16,18 +15,22 @@ class FactoryTest extends TestCase
1615
*/
1716
public function setUpFactory()
1817
{
19-
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
18+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
2019
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
2120

22-
$this->factory = new Factory($this->loop, $this->tcp);
21+
$this->factory = new Factory($loop, $this->tcp);
2322
}
2423

25-
/**
26-
* @doesNotPerformAssertions
27-
*/
28-
public function testDefaultCtor()
24+
public function testDefaultCtorCreatesConnectorAutomatically()
2925
{
30-
$this->factory = new Factory($this->loop);
26+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
27+
$this->factory = new Factory($loop);
28+
29+
$ref = new \ReflectionProperty($this->factory, 'connector');
30+
$ref->setAccessible(true);
31+
$connector = $ref->getValue($this->factory);
32+
33+
$this->assertInstanceOf('React\Socket\Connector', $connector);
3134
}
3235

3336
public function testCreateClientUsesDefaultPortForTcpConnection()
@@ -97,6 +100,49 @@ public function testCreateClientWithAuthenticationWillSendLoginActionWithDecoded
97100
$clientConnected($client);
98101
}
99102

103+
public function testCreateClientWithAuthenticationResolvesWhenAuthenticationSucceeds()
104+
{
105+
$action = $this->getMockBuilder('Clue\React\Ami\Protocol\Action')->getMock();
106+
$client = $this->getMockBuilder('Clue\React\Ami\Client')->disableOriginalConstructor()->getMock();
107+
$client->expects($this->once())->method('createAction')->willReturn($action);
108+
$client->expects($this->once())->method('request')->with($action)->willReturn(\React\Promise\resolve('ignored'));
109+
110+
$promiseConnecting = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
111+
$promiseConnecting->expects($this->once())->method('then')->willReturn(\React\Promise\resolve($client));
112+
$this->tcp->expects($this->once())->method('connect')->willReturn($promiseConnecting);
113+
114+
$promise = $this->factory->createClient('user%40host:pass+word%21@localhost');
115+
116+
$client = null;
117+
$promise->then(function ($value) use (&$client) {
118+
$client = $value;
119+
});
120+
121+
$this->assertInstanceOf('Clue\React\Ami\Client', $client);
122+
}
123+
124+
public function testCreateClientWithAuthenticationWillCloseClientAndRejectWhenLoginRequestRejects()
125+
{
126+
$error = new \RuntimeException();
127+
$action = $this->getMockBuilder('Clue\React\Ami\Protocol\Action')->getMock();
128+
$client = $this->getMockBuilder('Clue\React\Ami\Client')->disableOriginalConstructor()->getMock();
129+
$client->expects($this->once())->method('createAction')->willReturn($action);
130+
$client->expects($this->once())->method('request')->with($action)->willReturn(\React\Promise\reject($error));
131+
$client->expects($this->once())->method('close');
132+
133+
$promiseConnecting = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
134+
$promiseConnecting->expects($this->once())->method('then')->willReturn(\React\Promise\resolve($client));
135+
$this->tcp->expects($this->once())->method('connect')->willReturn($promiseConnecting);
136+
137+
$promise = $this->factory->createClient('user%40host:pass+word%21@localhost');
138+
139+
$exception = null;
140+
$promise->then(null, function ($reason) use (&$exception) {
141+
$exception = $reason;
142+
});
143+
$this->assertSame($error, $exception);
144+
}
145+
100146
public function testCreateClientWithInvalidUrlWillRejectPromise()
101147
{
102148
$promise = $this->factory->createClient('///');

0 commit comments

Comments
 (0)