Skip to content

feature #37002 [Amqp] Add amqps support #37518

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -51,6 +51,27 @@ public function testItCanBeConstructedWithDefaults()
);
}


public function testItCanBeConstructedWithAnAMQPSDsn()
{
$this->assertEquals(
new Connection([
'host' => 'localhost',
'port' => 5671,
'vhost' => '/',
'cacert' => '/etc/ssl/cafile',
'cert' => '/etc/ssl/cert.pem',
'key' => '/etc/ssl/private_key.pem',
'verify' => false,
], [
'name' => self::DEFAULT_EXCHANGE_NAME,
], [
self::DEFAULT_EXCHANGE_NAME => [],
]),
Connection::fromDsn('amqps://')
);
}

public function testItGetsParametersFromTheDsn()
{
$this->assertEquals(
Expand Down Expand Up @@ -312,6 +333,44 @@ public function testItSetupsTheConnection()
$connection->publish('body');
}

public function testItSetupsTheTTLConnection()
{
$amqpConnection = $this->createMock(\AMQPConnection::class);
$amqpChannel = $this->createMock(\AMQPChannel::class);
$amqpExchange = $this->createMock(\AMQPExchange::class);
$amqpQueue0 = $this->createMock(\AMQPQueue::class);
$amqpQueue1 = $this->createMock(\AMQPQueue::class);

$factory = $this->createMock(AmqpFactory::class);
$factory->method('createConnection')->willReturn($amqpConnection);
$factory->method('createChannel')->willReturn($amqpChannel);
$factory->method('createExchange')->willReturn($amqpExchange);
$factory->method('createQueue')->will($this->onConsecutiveCalls($amqpQueue0, $amqpQueue1));

$amqpExchange->expects($this->once())->method('declareExchange');
$amqpExchange->expects($this->once())->method('publish')->with('body', 'routing_key', AMQP_NOPARAM, ['headers' => [], 'delivery_mode' => 2]);
$amqpQueue0->expects($this->once())->method('declareQueue');
$amqpQueue0->expects($this->exactly(2))->method('bind')->withConsecutive(
[self::DEFAULT_EXCHANGE_NAME, 'binding_key0'],
[self::DEFAULT_EXCHANGE_NAME, 'binding_key1']
);
$amqpQueue1->expects($this->once())->method('declareQueue');
$amqpQueue1->expects($this->exactly(2))->method('bind')->withConsecutive(
[self::DEFAULT_EXCHANGE_NAME, 'binding_key2'],
[self::DEFAULT_EXCHANGE_NAME, 'binding_key3']
);

$dsn = 'amqps://localhost?'.
'exchange[default_publish_routing_key]=routing_key&'.
'queues[queue0][binding_keys][0]=binding_key0&'.
'queues[queue0][binding_keys][1]=binding_key1&'.
'queues[queue1][binding_keys][0]=binding_key2&'.
'queues[queue1][binding_keys][1]=binding_key3';

$connection = Connection::fromDsn($dsn, [], $factory);
$connection->publish('body');
}

public function testBindingArguments()
{
$amqpConnection = $this->createMock(\AMQPConnection::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function createTransport(string $dsn, array $options, SerializerInterface

public function supports(string $dsn, array $options): bool
{
return 0 === strpos($dsn, 'amqp://');
return 0 === strpos($dsn, 'amqp://') || 0 === strpos($dsn, 'amqps://');
}
}
class_alias(AmqpTransportFactory::class, \Symfony\Component\Messenger\Transport\AmqpExt\AmqpTransportFactory::class);
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function fromDsn(string $dsn, array $options = [], AmqpFactory $am
{
if (false === $parsedUrl = parse_url($dsn)) {
// this is a valid URI that parse_url cannot handle when you want to pass all parameters as options
if ('amqp://' !== $dsn) {
if ('amqp://' !== $dsn || 'amqps://' !== $dsn) {
throw new InvalidArgumentException(sprintf('The given AMQP DSN "%s" is invalid.', $dsn));
}

Expand All @@ -175,10 +175,11 @@ public static function fromDsn(string $dsn, array $options = [], AmqpFactory $am
$pathParts = isset($parsedUrl['path']) ? explode('/', trim($parsedUrl['path'], '/')) : [];
$exchangeName = $pathParts[1] ?? 'messages';
parse_str($parsedUrl['query'] ?? '', $parsedQuery);
$port = $parsedUrl['port'] ?? $dsn === 'amqps://' ? 5671 : 5672;

$amqpOptions = array_replace_recursive([
'host' => $parsedUrl['host'] ?? 'localhost',
'port' => $parsedUrl['port'] ?? 5672,
'port' => $port,
'vhost' => isset($pathParts[0]) ? urldecode($pathParts[0]) : '/',
'exchange' => [
'name' => $exchangeName,
Expand Down