-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathHandlerTest.php
216 lines (183 loc) · 8.85 KB
/
HandlerTest.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
use Aws\ApiGatewayManagementApi\Exception\ApiGatewayManagementApiException;
use Aws\CommandInterface;
use Aws\Exception\AwsException;
use Aws\MockHandler;
use Aws\Result;
use Bref\Context\Context;
use Georgeboot\LaravelEchoApiGateway\ConnectionRepository;
use Georgeboot\LaravelEchoApiGateway\Handler;
use Georgeboot\LaravelEchoApiGateway\SubscriptionRepository;
use GuzzleHttp\Psr7\Response;
use Mockery\Mock;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
it('can subscribe to open channels', function () {
app()->instance(SubscriptionRepository::class, Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('subscribeToChannel')->withArgs(function (string $connectionId, string $channel): bool {
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
})->once();
}));
app()->instance(ConnectionRepository::class, Mockery::mock(ConnectionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('sendMessage')->withArgs(function (string $connectionId, string $data): bool {
return $connectionId === 'connection-id-1' and $data === '{"event":"subscription_succeeded","channel":"test-channel","data":[]}';
})->once();
}));
/** @var Handler $handler */
$handler = app(Handler::class);
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
$handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'MESSAGE',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'subscribe', 'data' => ['channel' => 'test-channel']]),
], $context);
});
it('can unsubscribe from a channel', function () {
app()->instance(SubscriptionRepository::class, Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('unsubscribeFromChannel')->withArgs(function (string $connectionId, string $channel): bool {
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
})->once();
}));
app()->instance(ConnectionRepository::class, Mockery::mock(ConnectionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('sendMessage')->withArgs(function (string $connectionId, string $data): bool {
return $connectionId === 'connection-id-1' and $data === '{"event":"unsubscription_succeeded","channel":"test-channel","data":[]}';
})->once();
}));
/** @var Handler $handler */
$handler = app(Handler::class);
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
$handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'MESSAGE',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'unsubscribe', 'data' => ['channel' => 'test-channel']]),
], $context);
});
it('can broadcast a whisper', function () {
app()->instance(SubscriptionRepository::class, Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('getConnectionIdsForChannel')->withArgs(function (string $channel): bool {
return $channel === 'test-channel';
})->once()
->andReturn(collect(['connection-id-1', 'connection-id-2']));
}));
app()->instance(ConnectionRepository::class, Mockery::mock(ConnectionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('sendMessage')->withArgs(function (string $connectionId, string $data): bool {
return $connectionId === 'connection-id-2' and $data === '{"event":"client-test","channel":"test-channel","data":"whisper"}';
})->once();
}));
/** @var Handler $handler */
$handler = app(Handler::class);
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
$handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'MESSAGE',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'client-test', 'channel' => 'test-channel', 'data'=>'whisper']),
], $context);
});
it('leaves presence channels', function () {
app()->instance(SubscriptionRepository::class, Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('getChannelsSubscribedToByConnectionId')->withArgs(function (string $connectionId): bool {
return $connectionId === 'connection-id-1';
})->once()
->andReturn(collect([
[
'channel'=>'presence-channel',
'userData'=>json_encode(['user_info'=>['the user info']]),
],
[
'channel'=>'other-channel',
]
]));
$mock->shouldReceive('getConnectionIdsForChannel')->withArgs(function (string $channel) {
return $channel === 'presence-channel';
})->once()
->andReturn(collect(['connection-id-1', 'connection-id-2']));
$mock->shouldReceive('clearConnection')->withArgs(function (string $connectionId) {
return $connectionId === 'connection-id-1';
})->once();
}));
app()->instance(ConnectionRepository::class, Mockery::mock(ConnectionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('sendMessage')->withArgs(function (string $connectionId, string $data): bool {
return $connectionId === 'connection-id-2' and $data === '{"event":"member_removed","channel":"presence-channel","data":["the user info"]}';
})->once();
}));
/** @var Handler $handler */
$handler = app(Handler::class);
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
$handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'DISCONNECT',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'disconnect']),
], $context);
});
it('handles dropped connections with HTTP_GONE', function () {
$mock = new MockHandler();
$mock->append(function (CommandInterface $cmd, RequestInterface $req) {
$mock = Mockery::mock(SymfonyResponse::class, function ($mock) {
$mock->shouldReceive('getStatusCode')
->andReturn(SymfonyResponse::HTTP_GONE);
});
return new ApiGatewayManagementApiException('', $cmd, [
'response' => $mock
]);
});
/** @var SubscriptionRepository */
$subscriptionRepository = Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('clearConnection')->withArgs(function (string $connectionId): bool {
return $connectionId === 'dropped-connection-id-1234';
})->once();
});
$config = config('laravel-echo-api-gateway');
/** @var ConnectionRepository */
$connectionRepository = new ConnectionRepository($subscriptionRepository, array_merge_recursive(['connection' => ['handler' => $mock]], $config));
$connectionRepository->sendMessage('dropped-connection-id-1234', 'test-message');
});
it('handles dropped connections with GoneException', function () {
$mock = new MockHandler();
$mock->append(function (CommandInterface $cmd, RequestInterface $req) {
return new ApiGatewayManagementApiException('', $cmd, ['code' => 'GoneException']);
});
/** @var SubscriptionRepository */
$subscriptionRepository = Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('clearConnection')->withArgs(function (string $connectionId): bool {
return $connectionId === 'dropped-connection-id-1234';
})->once();
});
$config = config('laravel-echo-api-gateway');
/** @var ConnectionRepository */
$connectionRepository = new ConnectionRepository($subscriptionRepository, array_merge_recursive(['connection' => ['handler' => $mock]], $config));
$connectionRepository->sendMessage('dropped-connection-id-1234', 'test-message');
});