|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport;
|
13 | 13 |
|
| 14 | +use AsyncAws\Core\Exception\Http\HttpException; |
| 15 | +use AsyncAws\Core\Exception\Http\ServerException; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
14 | 17 | use PHPUnit\Framework\TestCase;
|
15 | 18 | use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
|
| 19 | +use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver; |
16 | 20 | use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport;
|
17 | 21 | use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
|
18 | 22 | use Symfony\Component\Messenger\Envelope;
|
| 23 | +use Symfony\Component\Messenger\Exception\TransportException; |
19 | 24 | use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
|
| 25 | +use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; |
| 26 | +use Symfony\Component\Messenger\Transport\Sender\SenderInterface; |
20 | 27 | use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
|
21 | 28 | use Symfony\Component\Messenger\Transport\TransportInterface;
|
| 29 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
22 | 30 |
|
23 | 31 | class AmazonSqsTransportTest extends TestCase
|
24 | 32 | {
|
| 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 | + |
25 | 63 | public function testItIsATransport()
|
26 | 64 | {
|
27 | 65 | $transport = $this->getTransport();
|
@@ -58,11 +96,87 @@ public function testTransportIsAMessageCountAware()
|
58 | 96 | $this->assertInstanceOf(MessageCountAwareInterface::class, $transport);
|
59 | 97 | }
|
60 | 98 |
|
| 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 | + |
61 | 170 | private function getTransport(SerializerInterface $serializer = null, Connection $connection = null)
|
62 | 171 | {
|
63 | 172 | $serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock();
|
64 | 173 | $connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
|
65 | 174 |
|
66 | 175 | return new AmazonSqsTransport($connection, $serializer);
|
67 | 176 | }
|
| 177 | + |
| 178 | + private function createHttpException(): HttpException |
| 179 | + { |
| 180 | + return new ServerException($this->createMock(ResponseInterface::class)); |
| 181 | + } |
68 | 182 | }
|
0 commit comments