-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathSnsQsProducerTest.php
More file actions
208 lines (161 loc) · 7.38 KB
/
SnsQsProducerTest.php
File metadata and controls
208 lines (161 loc) · 7.38 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
<?php
namespace Enqueue\SnsQs\Tests;
use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsMessage;
use Enqueue\Sns\SnsProducer;
use Enqueue\SnsQs\SnsQsMessage;
use Enqueue\SnsQs\SnsQsProducer;
use Enqueue\SnsQs\SnsQsQueue;
use Enqueue\SnsQs\SnsQsTopic;
use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsMessage;
use Enqueue\Sqs\SqsProducer;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Destination;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
class SnsQsProducerTest extends TestCase
{
use ClassExtensionTrait;
use ProphecyTrait;
public function testShouldImplementProducerInterface()
{
$this->assertClassImplements(Producer::class, SnsQsProducer::class);
}
public function testCouldBeConstructedWithRequiredArguments()
{
new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());
}
public function testShouldThrowIfMessageIsInvalidType()
{
$this->expectException(InvalidMessageException::class);
$this->expectExceptionMessageMatches('/The message must be an instance of Enqueue\\\\SnsQs\\\\SnsQsMessage but it is Double\\\\Message\\\\P\d+/');
$producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());
$message = $this->prophesize(Message::class)->reveal();
$producer->send(new SnsQsTopic(''), $message);
}
public function testShouldThrowIfDestinationOfInvalidType()
{
$this->expectException(InvalidDestinationException::class);
$producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());
$destination = $this->prophesize(Destination::class)->reveal();
$producer->send($destination, new SnsQsMessage());
}
public function testShouldSetDeliveryDelayToSQSProducer()
{
$delay = 10;
$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->setDeliveryDelay(Argument::is($delay))->shouldBeCalledTimes(1);
$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());
$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);
$producer->setDeliveryDelay($delay);
}
public function testShouldGetDeliveryDelayFromSQSProducer()
{
$delay = 10;
$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->getDeliveryDelay()->willReturn($delay);
$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());
$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);
$this->assertEquals($delay, $producer->getDeliveryDelay());
}
public function testShouldSendSnsTopicMessageToSnsProducer()
{
$snsMock = $this->createSnsContextMock();
$snsMock->method('createMessage')->willReturn(new SnsMessage());
$destination = new SnsQsTopic('');
$snsProducerStub = $this->prophesize(SnsProducer::class);
$snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce();
$snsMock->method('createProducer')->willReturn($snsProducerStub->reveal());
$producer = new SnsQsProducer($snsMock, $this->createSqsContextMock());
$producer->send($destination, new SnsQsMessage());
}
public function testShouldSendSnsTopicMessageWithAttributesToSnsProducer()
{
$snsMock = $this->createSnsContextMock();
$snsMock->method('createMessage')->willReturn(new SnsMessage());
$destination = new SnsQsTopic('');
$snsProducerStub = $this->prophesize(SnsProducer::class);
$snsProducerStub->send(
$destination,
Argument::that(function (SnsMessage $snsMessage) {
return $snsMessage->getMessageAttributes() === ['foo' => 'bar'];
})
)->shouldBeCalledOnce();
$snsMock->method('createProducer')->willReturn($snsProducerStub->reveal());
$producer = new SnsQsProducer($snsMock, $this->createSqsContextMock());
$producer->send($destination, new SnsQsMessage('', [], [], ['foo' => 'bar']));
}
public function testShouldSendToSnsTopicMessageWithGroupIdAndDeduplicationId()
{
$snsMock = $this->createSnsContextMock();
$snsMock->method('createMessage')->willReturn(new SnsMessage());
$destination = new SnsQsTopic('');
$snsProducerStub = $this->prophesize(SnsProducer::class);
$snsProducerStub->send(
$destination,
Argument::that(function (SnsMessage $snsMessage) {
return 'group-id' === $snsMessage->getMessageGroupId()
&& 'deduplication-id' === $snsMessage->getMessageDeduplicationId();
})
)->shouldBeCalledOnce();
$snsMock->method('createProducer')->willReturn($snsProducerStub->reveal());
$snsMessage = new SnsQsMessage();
$snsMessage->setMessageGroupId('group-id');
$snsMessage->setMessageDeduplicationId('deduplication-id');
$producer = new SnsQsProducer($snsMock, $this->createSqsContextMock());
$producer->send($destination, $snsMessage);
}
public function testShouldSendSqsMessageToSqsProducer()
{
$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createMessage')->willReturn(new SqsMessage());
$destination = new SnsQsQueue('');
$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());
$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);
$producer->send($destination, new SnsQsMessage());
}
public function testShouldSendToSqsProducerMessageWithGroupIdAndDeduplicationId()
{
$sqsMock = $this->createSqsContextMock();
$sqsMock->method('createMessage')->willReturn(new SqsMessage());
$destination = new SnsQsQueue('');
$sqsProducerStub = $this->prophesize(SqsProducer::class);
$sqsProducerStub->send(
$destination,
Argument::that(function (SqsMessage $sqsMessage) {
return 'group-id' === $sqsMessage->getMessageGroupId()
&& 'deduplication-id' === $sqsMessage->getMessageDeduplicationId();
})
)->shouldBeCalledOnce();
$sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal());
$sqsMessage = new SnsQsMessage();
$sqsMessage->setMessageGroupId('group-id');
$sqsMessage->setMessageDeduplicationId('deduplication-id');
$producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock);
$producer->send($destination, $sqsMessage);
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject|SnsContext
*/
private function createSnsContextMock(): SnsContext
{
return $this->createMock(SnsContext::class);
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject|SqsContext
*/
private function createSqsContextMock(): SqsContext
{
return $this->createMock(SqsContext::class);
}
}