Skip to content

Commit 15a9024

Browse files
[Messenger] Make all the dependencies of AmazonSqsTransport injectable
1 parent 268b5b7 commit 15a9024

File tree

3 files changed

+131
-47
lines changed

3 files changed

+131
-47
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.1
5+
-----
6+
7+
* Allowed for receiver & sender injection into AmazonSqsTransport
8+
49
5.2.0
510
-----
611

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportTest.php

+95-28
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,125 @@
1111

1212
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport;
1313

14+
use AsyncAws\Core\Exception\Http\HttpException;
15+
use AsyncAws\Core\Exception\Http\ServerException;
16+
use PHPUnit\Framework\MockObject\MockObject;
1417
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
18+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver;
1619
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport;
1720
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
1821
use Symfony\Component\Messenger\Envelope;
22+
use Symfony\Component\Messenger\Exception\TransportException;
1923
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
20-
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
21-
use Symfony\Component\Messenger\Transport\TransportInterface;
24+
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
25+
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
26+
use Symfony\Contracts\HttpClient\ResponseInterface;
2227

2328
class AmazonSqsTransportTest extends TestCase
2429
{
25-
public function testItIsATransport()
30+
/**
31+
* @var MockObject|Connection
32+
*/
33+
private $connection;
34+
35+
/**
36+
* @var MockObject|ReceiverInterface
37+
*/
38+
private $receiver;
39+
40+
/**
41+
* @var MockObject|SenderInterface|MessageCountAwareInterface
42+
*/
43+
private $sender;
44+
45+
/**
46+
* @var AmazonSqsTransport
47+
*/
48+
private $transport;
49+
50+
protected function setUp(): void
2651
{
27-
$transport = $this->getTransport();
52+
$this->connection = $this->createMock(Connection::class);
53+
// Mocking the concrete receiver class because mocking multiple interfaces is deprecated
54+
$this->receiver = $this->createMock(AmazonSqsReceiver::class);
55+
$this->sender = $this->createMock(SenderInterface::class);
2856

29-
$this->assertInstanceOf(TransportInterface::class, $transport);
57+
$this->transport = AmazonSqsTransport::create($this->connection, $this->receiver, $this->sender);
3058
}
3159

32-
public function testReceivesMessages()
60+
public function testItCanGetMessagesViaTheReceiver(): void
3361
{
34-
$transport = $this->getTransport(
35-
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(),
36-
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock()
37-
);
62+
$envelopes = [new Envelope(new \stdClass()), new Envelope(new \stdClass())];
63+
$this->receiver->expects($this->once())->method('get')->willReturn($envelopes);
64+
$this->assertSame($envelopes, $this->transport->get());
65+
}
3866

39-
$decodedMessage = new DummyMessage('Decoded.');
67+
public function testItCanAcknowledgeAMessageViaTheReceiver(): void
68+
{
69+
$envelope = new Envelope(new \stdClass());
70+
$this->receiver->expects($this->once())->method('ack');
71+
$this->transport->ack($envelope);
72+
}
4073

41-
$sqsEnvelope = [
42-
'id' => '5',
43-
'body' => 'body',
44-
'headers' => ['my' => 'header'],
45-
];
74+
public function testItCanRejectAMessageViaTheReceiver(): void
75+
{
76+
$envelope = new Envelope(new \stdClass());
77+
$this->receiver->expects($this->once())->method('reject');
78+
$this->transport->reject($envelope);
79+
}
4680

47-
$serializer->method('decode')->with(['body' => 'body', 'headers' => ['my' => 'header']])->willReturn(new Envelope($decodedMessage));
48-
$connection->method('get')->willReturn($sqsEnvelope);
81+
public function testItCanGetMessageCountViaTheReceiver(): void
82+
{
83+
$messageCount = 15;
84+
$this->receiver->expects($this->once())->method('getMessageCount')->willReturn($messageCount);
85+
$this->assertSame($messageCount, $this->transport->getMessageCount());
86+
}
4987

50-
$envelopes = iterator_to_array($transport->get());
51-
$this->assertSame($decodedMessage, $envelopes[0]->getMessage());
88+
public function testItCanSendAMessageViaTheSender(): void
89+
{
90+
$envelope = new Envelope(new \stdClass());
91+
$this->sender->expects($this->once())->method('send')->willReturn($envelope);
92+
$this->assertSame($envelope, $this->transport->send($envelope));
5293
}
5394

54-
public function testTransportIsAMessageCountAware()
95+
public function testItCanSetUpTheConnection(): void
5596
{
56-
$transport = $this->getTransport();
97+
$this->connection->expects($this->once())->method('setup');
98+
$this->transport->setup();
99+
}
100+
101+
public function testItConvertsHttpExceptionDuringSetupIntoTransportException(): void
102+
{
103+
$this->connection
104+
->expects($this->once())
105+
->method('setup')
106+
->willThrowException($this->createHttpException());
57107

58-
$this->assertInstanceOf(MessageCountAwareInterface::class, $transport);
108+
$this->expectException(TransportException::class);
109+
110+
$this->transport->setup();
59111
}
60112

61-
private function getTransport(SerializerInterface $serializer = null, Connection $connection = null)
113+
public function testItCanResetTheConnection(): void
62114
{
63-
$serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock();
64-
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
115+
$this->connection->expects($this->once())->method('reset');
116+
$this->transport->reset();
117+
}
65118

66-
return new AmazonSqsTransport($connection, $serializer);
119+
public function testItConvertsHttpExceptionDuringResetIntoTransportException(): void
120+
{
121+
$this->connection
122+
->expects($this->once())
123+
->method('reset')
124+
->willThrowException($this->createHttpException());
125+
126+
$this->expectException(TransportException::class);
127+
128+
$this->transport->reset();
129+
}
130+
131+
private function createHttpException(): HttpException
132+
{
133+
return new ServerException($this->createMock(ResponseInterface::class));
67134
}
68135
}

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php

+31-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Exception\TransportException;
1717
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
18+
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
19+
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
1820
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
1921
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2022
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
@@ -26,55 +28,75 @@
2628
*/
2729
class AmazonSqsTransport implements TransportInterface, SetupableTransportInterface, MessageCountAwareInterface, ResetInterface
2830
{
29-
private $serializer;
31+
/**
32+
* @var Connection
33+
*/
3034
private $connection;
35+
36+
/**
37+
* @var MessageCountAwareInterface|ReceiverInterface
38+
*/
3139
private $receiver;
40+
41+
/**
42+
* @var SenderInterface
43+
*/
3244
private $sender;
3345

34-
public function __construct(Connection $connection, SerializerInterface $serializer = null)
46+
/**
47+
* @param ReceiverInterface|MessageCountAwareInterface|null $receiver
48+
*/
49+
public function __construct(Connection $connection, ?SerializerInterface $serializer = null, ?ReceiverInterface $receiver = null, ?SenderInterface $sender = null)
3550
{
3651
$this->connection = $connection;
37-
$this->serializer = $serializer ?? new PhpSerializer();
52+
$serializer = $serializer ?? new PhpSerializer();
53+
$this->receiver = $receiver ?? new AmazonSqsReceiver($connection, $serializer);
54+
$this->sender = $sender ?? new AmazonSqsSender($connection, $serializer);
55+
}
56+
57+
public static function create(Connection $connection, ReceiverInterface $receiver, SenderInterface $sender): self
58+
{
59+
return new self($connection, null, $receiver, $sender);
3860
}
3961

4062
/**
4163
* {@inheritdoc}
4264
*/
4365
public function get(): iterable
4466
{
45-
return ($this->receiver ?? $this->getReceiver())->get();
67+
return $this->receiver->get();
4668
}
4769

4870
/**
4971
* {@inheritdoc}
5072
*/
5173
public function ack(Envelope $envelope): void
5274
{
53-
($this->receiver ?? $this->getReceiver())->ack($envelope);
75+
$this->receiver->ack($envelope);
5476
}
5577

5678
/**
5779
* {@inheritdoc}
5880
*/
5981
public function reject(Envelope $envelope): void
6082
{
61-
($this->receiver ?? $this->getReceiver())->reject($envelope);
83+
$this->receiver->reject($envelope);
6284
}
6385

6486
/**
6587
* {@inheritdoc}
6688
*/
6789
public function getMessageCount(): int
6890
{
69-
return ($this->receiver ?? $this->getReceiver())->getMessageCount();
91+
return $this->receiver->getMessageCount();
7092
}
7193

7294
/**
7395
* {@inheritdoc}
7496
*/
7597
public function send(Envelope $envelope): Envelope
7698
{
77-
return ($this->sender ?? $this->getSender())->send($envelope);
99+
return $this->sender->send($envelope);
78100
}
79101

80102
/**
@@ -89,22 +111,12 @@ public function setup(): void
89111
}
90112
}
91113

92-
public function reset()
114+
public function reset(): void
93115
{
94116
try {
95117
$this->connection->reset();
96118
} catch (HttpException $e) {
97119
throw new TransportException($e->getMessage(), 0, $e);
98120
}
99121
}
100-
101-
private function getReceiver(): AmazonSqsReceiver
102-
{
103-
return $this->receiver = new AmazonSqsReceiver($this->connection, $this->serializer);
104-
}
105-
106-
private function getSender(): AmazonSqsSender
107-
{
108-
return $this->sender = new AmazonSqsSender($this->connection, $this->serializer);
109-
}
110122
}

0 commit comments

Comments
 (0)