Skip to content
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

Allow to use custom http client #7

Open
wants to merge 1 commit into
base: 6.1
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Tests/Transport/AmazonSqsTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;

class AmazonSqsTransportFactoryTest extends TestCase
{
Expand All @@ -25,4 +28,18 @@ public function testSupportsOnlySqsTransports()
$this->assertFalse($factory->supports('redis://localhost', []));
$this->assertFalse($factory->supports('invalid-dsn', []));
}

public function testCustomHttpClient(): void
{
$httpClient = new MockHttpClient();
$factory = new AmazonSqsTransportFactory(null, $httpClient);

$transport = $factory->createTransport('https://sqs.us-east-2.amazonaws.com/1111/messages?access_key=KEY&secret_key=SECRET', [], Serializer::create());

self::assertSame(0, $httpClient->getRequestsCount());
$transport->send(new Envelope(new \stdClass(), []));

// 1 query to check queueExists, 1 query to sendMessage
self::assertSame(2, $httpClient->getRequestsCount());
}
}
7 changes: 5 additions & 2 deletions Transport/AmazonSqsTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Jérémy Derussé <[email protected]>
*/
class AmazonSqsTransportFactory implements TransportFactoryInterface
{
private ?LoggerInterface $logger;
private ?HttpClientInterface $httpClient;

public function __construct(LoggerInterface $logger = null)
public function __construct(LoggerInterface $logger = null, HttpClientInterface $httpClient = null)
{
$this->logger = $logger;
$this->httpClient = $httpClient;
}

public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
{
unset($options['transport_name']);

return new AmazonSqsTransport(Connection::fromDsn($dsn, $options, null, $this->logger), $serializer);
return new AmazonSqsTransport(Connection::fromDsn($dsn, $options, $this->httpClient, $this->logger), $serializer);
}

public function supports(string $dsn, array $options): bool
Expand Down