|
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 |
| -use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage; |
| 18 | +use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver; |
16 | 19 | use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport;
|
17 | 20 | use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
|
18 | 21 | use Symfony\Component\Messenger\Envelope;
|
| 22 | +use Symfony\Component\Messenger\Exception\TransportException; |
19 | 23 | 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; |
22 | 27 |
|
23 | 28 | class AmazonSqsTransportTest extends TestCase
|
24 | 29 | {
|
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 |
26 | 51 | {
|
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); |
28 | 56 |
|
29 |
| - $this->assertInstanceOf(TransportInterface::class, $transport); |
| 57 | + $this->transport = AmazonSqsTransport::create($this->connection, $this->receiver, $this->sender); |
30 | 58 | }
|
31 | 59 |
|
32 |
| - public function testReceivesMessages() |
| 60 | + public function testItCanGetMessagesViaTheReceiver(): void |
33 | 61 | {
|
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 | + } |
38 | 66 |
|
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 | + } |
40 | 73 |
|
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 | + } |
46 | 80 |
|
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 | + } |
49 | 87 |
|
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)); |
52 | 93 | }
|
53 | 94 |
|
54 |
| - public function testTransportIsAMessageCountAware() |
| 95 | + public function testItCanSetUpTheConnection(): void |
55 | 96 | {
|
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()); |
57 | 107 |
|
58 |
| - $this->assertInstanceOf(MessageCountAwareInterface::class, $transport); |
| 108 | + $this->expectException(TransportException::class); |
| 109 | + |
| 110 | + $this->transport->setup(); |
59 | 111 | }
|
60 | 112 |
|
61 |
| - private function getTransport(SerializerInterface $serializer = null, Connection $connection = null) |
| 113 | + public function testItCanResetTheConnection(): void |
62 | 114 | {
|
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 | + } |
65 | 118 |
|
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)); |
67 | 134 | }
|
68 | 135 | }
|
0 commit comments