-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathSnsProducerTest.php
More file actions
245 lines (197 loc) · 7.34 KB
/
Copy pathSnsProducerTest.php
File metadata and controls
245 lines (197 loc) · 7.34 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Enqueue\Sns\Tests;
use Aws\Result;
use Enqueue\Sns\SnsClient;
use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsDestination;
use Enqueue\Sns\SnsMessage;
use Enqueue\Sns\SnsProducer;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Destination;
use Interop\Queue\Exception\DeliveryDelayNotSupportedException;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Exception\PriorityNotSupportedException;
use Interop\Queue\Exception\TimeToLiveNotSupportedException;
use Interop\Queue\Producer;
use PHPUnit\Framework\TestCase;
class SnsProducerTest extends TestCase
{
use ClassExtensionTrait;
public function testShouldImplementProducerInterface()
{
$this->assertClassImplements(Producer::class, SnsProducer::class);
}
public function testShouldThrowIfBodyOfInvalidType()
{
$this->expectException(InvalidMessageException::class);
$this->expectExceptionMessage('The message body must be a non-empty string.');
$producer = new SnsProducer($this->createSnsContextMock());
$message = new SnsMessage('');
$producer->send(new SnsDestination(''), $message);
}
public function testShouldThrowIfDestinationOfInvalidType()
{
$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Sns\SnsDestination but got Mock_Destinat');
$producer = new SnsProducer($this->createSnsContextMock());
$producer->send($this->createMock(Destination::class), new SnsMessage());
}
public function testShouldThrowIfPublishFailed()
{
$destination = new SnsDestination('queue-name');
$client = $this->createSnsClientMock();
$client
->expects($this->once())
->method('publish')
->willReturn(new Result())
;
$context = $this->createSnsContextMock();
$context
->expects($this->once())
->method('getTopicArn')
->with($this->identicalTo($destination))
->willReturn('theTopicArn')
;
$context
->expects($this->once())
->method('getSnsClient')
->willReturn($client)
;
$message = new SnsMessage('foo');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Message was not sent');
$producer = new SnsProducer($context);
$producer->send($destination, $message);
}
public function testShouldThrowIfsetTimeToLiveIsNotNull()
{
$this->expectException(TimeToLiveNotSupportedException::class);
$producer = new SnsProducer($this->createSnsContextMock());
$result = $producer->setTimeToLive();
$this->assertInstanceOf(SnsProducer::class, $result);
$this->expectExceptionMessage('The provider does not support time to live feature');
$producer->setTimeToLive(200);
}
public function testShouldThrowIfsetPriorityIsNotNull()
{
$this->expectException(PriorityNotSupportedException::class);
$producer = new SnsProducer($this->createSnsContextMock());
$result = $producer->setPriority();
$this->assertInstanceOf(SnsProducer::class, $result);
$this->expectExceptionMessage('The provider does not support priority feature');
$producer->setPriority(200);
}
public function testShouldThrowIfsetDeliveryDelayIsNotNull()
{
$this->expectException(DeliveryDelayNotSupportedException::class);
$producer = new SnsProducer($this->createSnsContextMock());
$result = $producer->setDeliveryDelay();
$this->assertInstanceOf(SnsProducer::class, $result);
$this->expectExceptionMessage('The provider does not support delivery delay feature');
$producer->setDeliveryDelay(200);
}
public function testShouldPublish()
{
$destination = new SnsDestination('queue-name');
$expectedArguments = [
'Message' => 'theBody',
'MessageAttributes' => [
'Headers' => [
'DataType' => 'String',
'StringValue' => '[{"hkey":"hvaleu"},{"key":"value"}]',
],
],
'TopicArn' => 'theTopicArn',
];
$client = $this->createSnsClientMock();
$client
->expects($this->once())
->method('publish')
->with($this->identicalTo($expectedArguments))
->willReturn(new Result(['MessageId' => 'theMessageId']))
;
$context = $this->createSnsContextMock();
$context
->expects($this->once())
->method('getTopicArn')
->with($this->identicalTo($destination))
->willReturn('theTopicArn')
;
$context
->expects($this->once())
->method('getSnsClient')
->willReturn($client)
;
$message = new SnsMessage('theBody', ['key' => 'value'], ['hkey' => 'hvaleu']);
$producer = new SnsProducer($context);
$producer->send($destination, $message);
}
/**
* @throws InvalidMessageException
*/
public function testShouldPublishWithMergedAttributes()
{
$context = $this->createSnsContextMock();
$client = $this->createSnsClientMock();
$context
->expects($this->once())
->method('getSnsClient')
->willReturn($client);
$expectedArgument = [
'Message' => 'message',
'MessageAttributes' => [
'Headers' => [
'DataType' => 'String',
'StringValue' => '[[],[]]',
],
'Foo' => [
'DataType' => 'String',
'StringValue' => 'foo-value',
],
'Bar' => [
'DataType' => 'Binary',
'BinaryValue' => 'bar-val',
],
],
'TopicArn' => '',
'MessageStructure' => 'structure',
'PhoneNumber' => 'phone',
'Subject' => 'subject',
'TargetArn' => 'target_arn',
];
$client
->expects($this->once())
->method('publish')
->with($this->identicalTo($expectedArgument))
->willReturn(new Result(['MessageId' => 'theMessageId']));
$attributes = [
'Foo' => [
'DataType' => 'String',
'StringValue' => 'foo-value',
],
];
$message = new SnsMessage(
'message', [], [], $attributes, 'structure', 'phone',
'subject', 'target_arn'
);
$message->addAttribute('Bar', 'Binary', 'bar-val');
$destination = new SnsDestination('queue-name');
$producer = new SnsProducer($context);
$producer->send($destination, $message);
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject|SnsContext
*/
private function createSnsContextMock(): SnsContext
{
return $this->createMock(SnsContext::class);
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject|SnsClient
*/
private function createSnsClientMock(): SnsClient
{
return $this->createMock(SnsClient::class);
}
}