forked from clue/reactphp-ami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryTest.php
More file actions
164 lines (126 loc) · 6.65 KB
/
FactoryTest.php
File metadata and controls
164 lines (126 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Clue\Tests\React\Ami;
use Clue\React\Ami\Factory;
use React\Promise\Promise;
class FactoryTest extends TestCase
{
private $tcp;
private $factory;
/**
* @before
*/
public function setUpFactory()
{
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->factory = new Factory(null, $this->tcp);
}
public function testDefaultCtorCreatesConnectorAutomatically()
{
$this->factory = new Factory();
$ref = new \ReflectionProperty($this->factory, 'connector');
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$connector = $ref->getValue($this->factory);
$this->assertInstanceOf('React\Socket\Connector', $connector);
}
public function testCtorThrowsForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
new Factory('loop');
}
public function testCtorThrowsForInvalidConnector()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
new Factory(null, 'connector');
}
public function testCreateClientUsesDefaultPortForTcpConnection()
{
$promise = new Promise(function () { });
$this->tcp->expects($this->once())->method('connect')->with('tcp://localhost:5038')->willReturn($promise);
$this->factory->createClient('localhost');
}
public function testCreateClientUsesTlsPortForTlsConnection()
{
$promise = new Promise(function () { });
$this->tcp->expects($this->once())->method('connect')->with('tls://localhost:5039')->willReturn($promise);
$this->factory->createClient('tls://localhost');
}
public function testCreateClientUsesTlsConnectorWithTlsLocation()
{
$promise = new Promise(function () { });
$this->tcp->expects($this->once())->method('connect')->with('tls://ami.local:1234')->willReturn($promise);
$this->factory->createClient('tls://ami.local:1234');
}
public function testCreateClientResolvesWithClientWhenConnectionResolves()
{
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$this->tcp->expects($this->once())->method('connect')->willReturn(\React\Promise\resolve($connection));
$promise = $this->factory->createClient('localhost');
$client = null;
$promise->then(function ($value) use (&$client) {
$client = $value;
});
$this->assertInstanceOf('Clue\React\Ami\Client', $client);
}
public function testCreateClientWithAuthenticationWillSendLoginActionWithDecodedUserInfo()
{
$promiseAuthenticated = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$clientConnected = null;
$promiseClient = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$promiseClient->expects($this->once())->method('then')->with($this->callback(function ($callback) use (&$clientConnected) {
$clientConnected = $callback;
return true;
}))->willReturn($promiseAuthenticated);
$promiseConnecting = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$promiseConnecting->expects($this->once())->method('then')->willReturn($promiseClient);
$this->tcp->expects($this->once())->method('connect')->willReturn($promiseConnecting);
$action = $this->getMockBuilder('Clue\React\Ami\Protocol\Action')->getMock();
$client = $this->getMockBuilder('Clue\React\Ami\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('createAction')->with('Login', array('UserName' => 'user@host', 'Secret' => 'pass+word!', 'Events' => null))->willReturn($action);
$client->expects($this->once())->method('request')->with($action)->willReturn($promiseAuthenticated);
$promise = $this->factory->createClient('user%40host:pass+word%21@localhost');
$this->assertSame($promiseAuthenticated, $promise);
$this->assertNotNull($clientConnected);
$clientConnected($client);
}
public function testCreateClientWithAuthenticationResolvesWhenAuthenticationSucceeds()
{
$action = $this->getMockBuilder('Clue\React\Ami\Protocol\Action')->getMock();
$client = $this->getMockBuilder('Clue\React\Ami\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('createAction')->willReturn($action);
$client->expects($this->once())->method('request')->with($action)->willReturn(\React\Promise\resolve('ignored'));
$promiseConnecting = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$promiseConnecting->expects($this->once())->method('then')->willReturn(\React\Promise\resolve($client));
$this->tcp->expects($this->once())->method('connect')->willReturn($promiseConnecting);
$promise = $this->factory->createClient('user%40host:pass+word%21@localhost');
$client = null;
$promise->then(function ($value) use (&$client) {
$client = $value;
});
$this->assertInstanceOf('Clue\React\Ami\Client', $client);
}
public function testCreateClientWithAuthenticationWillCloseClientAndRejectWhenLoginRequestRejects()
{
$error = new \RuntimeException();
$action = $this->getMockBuilder('Clue\React\Ami\Protocol\Action')->getMock();
$client = $this->getMockBuilder('Clue\React\Ami\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('createAction')->willReturn($action);
$client->expects($this->once())->method('request')->with($action)->willReturn(\React\Promise\reject($error));
$client->expects($this->once())->method('close');
$promiseConnecting = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$promiseConnecting->expects($this->once())->method('then')->willReturn(\React\Promise\resolve($client));
$this->tcp->expects($this->once())->method('connect')->willReturn($promiseConnecting);
$promise = $this->factory->createClient('user%40host:pass+word%21@localhost');
$exception = null;
$promise->then(null, function ($reason) use (&$exception) {
$exception = $reason;
});
$this->assertSame($error, $exception);
}
public function testCreateClientWithInvalidUrlWillRejectPromise()
{
$promise = $this->factory->createClient('///');
$promise->then(null, $this->expectCallableOnce());
}
}