10000 [Messenger] Make all the dependencies of AmazonSqsTransport injectable · symfony/symfony@f48e7c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f48e7c7

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

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,55 @@
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;
1518
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
19+
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver;
1620
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport;
1721
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
1822
use Symfony\Component\Messenger\Envelope;
23+
use Symfony\Component\Messenger\Exception\TransportException;
1924
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
25+
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
26+
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
2027
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2128
use Symfony\Component\Messenger\Transport\TransportInterface;
29+
use Symfony\Contracts\HttpClient\ResponseInterface;
2230

2331
class AmazonSqsTransportTest extends TestCase
2432
{
33+
/**
34+
* @var MockObject|Connection
35+
*/
36+
private $connection;
37+
38+
/**
39+
* @var MockObject|ReceiverInterface
40+
*/
41+
private $receiver;
42+
43+
/**
44+
* @var MockObject|SenderInterface|MessageCountAwareInterface
45+
*/
46+
private $sender;
47+
48+
/**
49+
* @var AmazonSqsTransport
50+
*/
51+
private $transport;
52+
53+
protected function setUp(): void
54+
{
55+
$this->connection = $this->createMock(Connection::class);
56+
// Mocking the concrete receiver class because mocking multiple interfaces is deprecated
57+
$this->receiver = $this->createMock(AmazonSqsReceiver::class);
58+
$this->sender = $this->createMock(SenderInterface::class);
59+
60+
$this->transport = AmazonSqsTransport::create($this->connection, $this->receiver, $this->sender);
61+
}
62+
2563
public function testItIsATransport()
2664
{
2765
$transport = $this->getTransport();
@@ -58,11 +96,87 @@ public function testTransportIsAMessageCountAware()
5896
$this->assertInstanceOf(MessageCountAwareInterface::class, $transport);
5997
}
6098

99+
public function testItCanGetMessagesViaTheReceiver(): void
100+
{
101+
$envelopes = [new Envelope(new \stdClass()), new Envelope(new \stdClass())];
102+
$this->receiver->expects($this->once())->method('get')->willReturn($envelopes);
103+
$this->assertSame($envelopes, $this->transport->get());
104+
}
105+
106+
public function testItCanAcknowledgeAMessageViaTheReceiver(): void
107+
{
108+
$envelope = new Envelope(new \stdClass());
109+
$this->receiver->expects($this->once())->method('ack')->with($envelope);
110+
$this->transport->ack($envelope);
111+
}
112+
113+
public function testItCanRejectAMessageViaTheReceiver(): void
114+
{
115+
$envelope = new Envelope(new \stdClass());
116+
$this->receiver->expects($this->once())->method('reject')->with($envelope);
117+
$this->transport->reject($envelope);
118+
}
119+
120+
public function testItCanGetMessageCountViaTheReceiver(): void
121+
{
122+
$messageCount = 15;
123+
$this->receiver->expects($this->once())->method('getMessageCount')->willReturn($messageCount);
124+
$this->assertSame($messageCount, $this->transport->getMessageCount());
125+
}
126+
127+
public function testItCanSendAMessageViaTheSender(): void
128+
{
129+
$envelope = new Envelope(new \stdClass());
130+
$this->sender->expects($this->once())->method('send')->with($envelope)->willReturn($envelope);
131+
$this->assertSame($envelope, $this->transport->send($envelope));
132+
}
133+
134+
public function testItCanSetUpTheConnection(): void
135+
{
136+
$this->connection->expects($this->once())->method('setup');
137+
$this->transport->setup();
138+
}
139+
140+
public function testItConvertsHttpExceptionDuringSetupIntoTransportException(): void
141+
{
142+
$this->connection
143+
->expects($this->once())
144+
->method('setup')
145+
->willThrowException($this->createHttpException());
146+
147+
$this->expectException(TransportException::class);
148+
149+
$this->transport->setup();
150+
}
151+
152+
public function testItCanResetTheConnection(): void
153+
{
154+
$this->connection->expects($this->once())->method('reset');
155+
$this->transport->reset();
156+
}
157+
158+
public function testItConvertsHttpExceptionDuringResetIntoTransportException(): void
159+
{
160+
$this->connection
161+
->expects($this->once())
162+
->method('reset')
163+
->willThrowException($this->createHttpException());
164+
165+
$this->expectException(TransportException::class);
166+
167+
$this->transport->reset();
168+
}
169+
61170
private function getTransport(SerializerInterface $serializer = null, Connection $connection = null)
62171
{
63172
$serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock();
64173
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
65174

66175
return new AmazonSqsTransport($connection, $serializer);
67176
}
177+
178+
private function createHttpException(): HttpException
179+
{
180+
return new ServerException($this->createMock(ResponseInterface::class));
181+
}
68182
}

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

Lines changed: 11 additions & 2 deletions
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\M F96F essenger\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;
@@ -31,10 +33,17 @@ class AmazonSqsTransport implements TransportInterface, SetupableTransportInterf
3133
private $receiver;
3234
private $sender;
3335

34-
public function __construct(Connection $connection, SerializerInterface $serializer = null)
36+
public function __construct(Connection $connection, SerializerInterface $serializer = null, ReceiverInterface $receiver = null, SenderInterface $sender = null)
3537
{
3638
$this->connection = $connection;
3739
$this->serializer = $serializer ?? new PhpSerializer();
40+
$this->receiver = $receiver;
41+
$this->sender = $sender;
42+
}
43+
44+
public static function create(Connection $connection, ReceiverInterface $receiver, SenderInterface $sender): self
45+
{
46+
return new self($connection, null, $receiver, $sender);
3847
}
3948

4049
/**
@@ -89,7 +98,7 @@ public function setup(): void
8998
}
9099
}
91100

92-
public function reset()
101+
public function reset(): void
93102
{
94103
try {
95104
$this->connection->reset();

0 commit comments

Comments
 (0)
0