Skip to content

[Messenger] Make all the dependencies of AmazonSqsTransport injectable #38846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added new `debug` option to log HTTP requests and responses.
* Allowed for receiver & sender injection into AmazonSqsTransport

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,55 @@

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

use AsyncAws\Core\Exception\Http\HttpException;
use AsyncAws\Core\Exception\Http\ServerException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class AmazonSqsTransportTest extends TestCase
{
/**
* @var MockObject|Connection
*/
private $connection;

/**
* @var MockObject|ReceiverInterface
*/
private $receiver;

/**
* @var MockObject|SenderInterface|MessageCountAwareInterface
*/
private $sender;

/**
* @var AmazonSqsTransport
*/
private $transport;

protected function setUp(): void
{
$this->connection = $this->createMock(Connection::class);
// Mocking the concrete receiver class because mocking multiple interfaces is deprecated
$this->receiver = $this->createMock(AmazonSqsReceiver::class);
$this->sender = $this->createMock(SenderInterface::class);

$this->transport = new AmazonSqsTransport($this->connection, null, $this->receiver, $this->sender);
}

public function testItIsATransport()
{
$transport = $this->getTransport();
Expand Down Expand Up @@ -58,11 +96,87 @@ public function testTransportIsAMessageCountAware()
$this->assertInstanceOf(MessageCountAwareInterface::class, $transport);
}

public function testItCanGetMessagesViaTheReceiver(): void
{
$envelopes = [new Envelope(new \stdClass()), new Envelope(new \stdClass())];
$this->receiver->expects($this->once())->method('get')->willReturn($envelopes);
$this->assertSame($envelopes, $this->transport->get());
}

public function testItCanAcknowledgeAMessageViaTheReceiver(): void
{
$envelope = new Envelope(new \stdClass());
$this->receiver->expects($this->once())->method('ack')->with($envelope);
$this->transport->ack($envelope);
}

public function testItCanRejectAMessageViaTheReceiver(): void
{
$envelope = new Envelope(new \stdClass());
$this->receiver->expects($this->once())->method('reject')->with($envelope);
$this->transport->reject($envelope);
}

public function testItCanGetMessageCountViaTheReceiver(): void
{
$messageCount = 15;
$this->receiver->expects($this->once())->method('getMessageCount')->willReturn($messageCount);
$this->assertSame($messageCount, $this->transport->getMessageCount());
}

public function testItCanSendAMessageViaTheSender(): void
{
$envelope = new Envelope(new \stdClass());
$this->sender->expects($this->once())->method('send')->with($envelope)->willReturn($envelope);
$this->assertSame($envelope, $this->transport->send($envelope));
}

public function testItCanSetUpTheConnection(): void
{
$this->connection->expects($this->once())->method('setup');
$this->transport->setup();
}

public function testItConvertsHttpExceptionDuringSetupIntoTransportException(): void
{
$this->connection
->expects($this->once())
->method('setup')
->willThrowException($this->createHttpException());

$this->expectException(TransportException::class);

$this->transport->setup();
}

public function testItCanResetTheConnection(): void
{
$this->connection->expects($this->once())->method('reset');
$this->transport->reset();
}

public function testItConvertsHttpExceptionDuringResetIntoTransportException(): void
{
$this->connection
->expects($this->once())
->method('reset')
->willThrowException($this->createHttpException());

$this->expectException(TransportException::class);

$this->transport->reset();
}

private function getTransport(SerializerInterface $serializer = null, Connection $connection = null)
{
$serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock();
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();

return new AmazonSqsTransport($connection, $serializer);
}

private function createHttpException(): HttpException
{
return new ServerException($this->createMock(ResponseInterface::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
Expand All @@ -31,10 +33,12 @@ class AmazonSqsTransport implements TransportInterface, SetupableTransportInterf
private $receiver;
private $sender;

public function __construct(Connection $connection, SerializerInterface $serializer = null)
public function __construct(Connection $connection, SerializerInterface $serializer = null, ReceiverInterface $receiver = null, SenderInterface $sender = null)
{
$this->connection = $connection;
$this->serializer = $serializer ?? new PhpSerializer();
$this->receiver = $receiver;
$this->sender = $sender;
}

/**
Expand Down