8000 Using AMQP auto-setup in all cases, not just in debug · symfony/symfony@069ab5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 069ab5f

Browse files
committed
Using AMQP auto-setup in all cases, not just in debug
1 parent b7e798e commit 069ab5f

File tree

6 files changed

+37
-27
lines changed

6 files changed

+37
-27
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
<service id="messenger.transport.amqp.factory" class="Symfony\Component\Messenger\Transport\AmqpExt\AmqpTransportFactory">
6262
<tag name="messenger.transport_factory" />
6363
<argument type="service" id="messenger.transport.serializer" />
64-
<argument>%kernel.debug%</argument>
6564
</service>
6665
</services>
6766
</container>

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ CHANGELOG
1313
* [BC BREAK] If listening to exceptions while using `AmqpSender` or `AmqpReceiver`, `\AMQPException` is
1414
no longer thrown in favor of `TransportException`.
1515
* Deprecated `LoggingMiddleware`, pass a logger to `SendMessageMiddleware` instead.
16+
* [BC BREAK] `Connection::__construct()` and `Connection::fromDsn()`
17+
both no longer have `$isDebug` arguments.
18+
* [BC BREAK] The Amqp Transport now automatically sets up the exchanges
19+
and queues by default. Previously, this was done when in "debug" mode
20+
only. Pass the `auto-setup` connection option to control this.
1621

1722
4.2.0
1823
-----

src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpTransportFactoryTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class AmqpTransportFactoryTest extends TestCase
2222
public function testSupportsOnlyAmqpTransports()
2323
{
2424
$factory = new AmqpTransportFactory(
25-
$this->getMockBuilder(SerializerInterface::class)->getMock(),
26-
true
25+
$this->getMockBuilder(SerializerInterface::class)->getMock()
2726
);
2827

2928
$this->assertTrue($factory->supports('amqp://localhost', []));
@@ -34,11 +33,10 @@ public function testSupportsOnlyAmqpTransports()
3433
public function testItCreatesTheTransport()
3534
{
3635
$factory = new AmqpTransportFactory(
37-
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(),
38-
true
36+
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock()
3937
);
4038

41-
$expectedTransport = new AmqpTransport(Connection::fromDsn('amqp://localhost', ['foo' => 'bar'], true), $serializer);
39+
$expectedTransport = new AmqpTransport(Connection::fromDsn('amqp://localhost', ['foo' => 'bar']), $serializer);
4240

4341
$this->assertEquals($expectedTransport, $factory->createTransport('amqp://localhost', ['foo' => 'bar']));
4442
}

src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/ConnectionTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function testSetsParametersOnTheQueueAndExchange()
128128
'alternate-exchange' => 'alternate',
129129
],
130130
],
131-
], true, $factory);
131+
], $factory);
132132
$connection->publish('body');
133133
}
134134

@@ -171,9 +171,11 @@ public function testItUsesANormalConnectionByDefault()
171171
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
172172
);
173173

174+
// makes ure the channel looks connected, so it's not re-created
175+
$amqpChannel->expects($this->once())->method('isConnected')->willReturn(true);
174176
$amqpConnection->expects($this->once())->method('connect');
175177

176-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages', [], false, $factory);
178+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages', [], $factory);
177179
$connection->publish('body');
178180
}
179181

@@ -186,13 +188,15 @@ public function testItAllowsToUseAPersistentConnection()
186188
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
187189
);
188190

191+
// makes ure the channel looks connected, so it's not re-created
192+
$amqpChannel->expects($this->once())->method('isConnected')->willReturn(true);
189193
$amqpConnection->expects($this->once())->method('pconnect');
190194

191-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?persistent=true', [], false, $factory);
195+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?persistent=true', [], $factory);
192196
$connection->publish('body');
193197
}
194198

195-
public function testItSetupsTheConnectionWhenDebug()
199+
public function testItSetupsTheConnectionByDefault()
196200
{
197201
$factory = new TestAmqpFactory(
198202
$amqpConnection = $this->getMockBuilder(\AMQPConnection::class)->disableOriginalConstructor()->getMock(),
@@ -206,7 +210,7 @@ public function testItSetupsTheConnectionWhenDebug()
206210
$amqpQueue->expects($this->once())->method('declareQueue');
207211
$amqpQueue->expects($this->once())->method('bind')->with('exchange_name', 'my_key');
208212

209-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', [], true, $factory);
213+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', [], $factory);
210214
$connection->publish('body');
211215
}
212216

@@ -224,13 +228,13 @@ public function testItCanDisableTheSetup()
224228
$amqpQueue->expects($this->never())->method('declareQueue');
225229
$amqpQueue->expects($this->never())->method('bind');
226230

227-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', ['auto-setup' => 'false'], true, $factory);
231+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', ['auto-setup' => 'false'], $factory);
228232
$connection->publish('body');
229233

230-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', ['auto-setup' => false], true, $factory);
234+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', ['auto-setup' => false], $factory);
231235
$connection->publish('body');
232236

233-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key&auto-setup=false', [], true, $factory);
237+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key&auto-setup=false', [], $factory);
234238
$connection->publish('body');
235239
}
236240

@@ -248,7 +252,7 @@ public function testPublishWithQueueOptions()
248252
$amqpExchange->expects($this->once())->method('publish')
249253
->with('body', null, 1, ['delivery_mode' => 2, 'headers' => ['token' => 'uuid', 'type' => '*']]);
250254

251-
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[attributes][delivery_mode]=2&queue[attributes][headers][token]=uuid&queue[flags]=1', [], true, $factory);
255+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[attributes][delivery_mode]=2&queue[attributes][headers][token]=uuid&queue[flags]=1', [], $factory);
252256
$connection->publish('body', $headers);
253257
}
254258
}

src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpTransportFactory.php

Lines changed: 2 additions & 4 deletions
< 10000 td data-grid-cell-id="diff-0c9cafa0aacf4fec65c1836e58a774c04cdef6ab3c509c7aac9b1a4bd4a27999-29-28-2" data-line-anchor="diff-0c9cafa0aacf4fec65c1836e58a774c04cdef6ab3c509c7aac9b1a4bd4a27999R28" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+
public function __construct(SerializerInterface $serializer = null)
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@
2424
class AmqpTransportFactory implements TransportFactoryInterface
2525
{
2626
private $serializer;
27-
private $debug;
2827

29-
public function __construct(SerializerInterface $serializer = null, bool $debug = false)
28
3029
{
3130
$this->serializer = $serializer ?? new PhpSerializer();
32-
$this->debug = $debug;
3331
}
3432

3533
public function createTransport(string $dsn, array $options): TransportInterface
3634
{
37-
return new AmqpTransport(Connection::fromDsn($dsn, $options, $this->debug), $this->serializer);
35+
return new AmqpTransport(Connection::fromDsn($dsn, $options), $this->serializer);
3836
}
3937

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

src/Symfony/Component/Messenger/Transport/AmqpExt/Connection.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Connection
3636
private $connectionCredentials;
3737
private $exchangeConfiguration;
3838
private $queueConfiguration;
39-
private $debug;
4039
private $amqpFactory;
4140

4241
/**
@@ -54,16 +53,15 @@ class Connection
5453
*/
5554
private $amqpQueue;
5655

57-
public function __construct(array $connectionCredentials, array $exchangeConfiguration, array $queueConfiguration, bool $debug = false, AmqpFactory $amqpFactory = null)
56+
public function __construct(array $connectionCredentials, array $exchangeConfiguration, array $queueConfiguration, AmqpFactory $amqpFactory = null)
5857
{
5958
$this->connectionCredentials = $connectionCredentials;
60-
$this->debug = $debug;
6159
$this->exchangeConfiguration = $exchangeConfiguration;
6260
$this->queueConfiguration = $queueConfiguration;
6361
$this->amqpFactory = $amqpFactory ?: new AmqpFactory();
6462
}
6563

66-
public static function fromDsn(string $dsn, array $options = [], bool $debug = false, AmqpFactory $amqpFactory = null): self
64+
public static function fromDsn(string $dsn, array $options = [], AmqpFactory $amqpFactory = null): self
6765
{
6866
if (false === $parsedUrl = parse_url($dsn)) {
6967
throw new InvalidArgumentException(sprintf('The given AMQP DSN "%s" is invalid.', $dsn));
@@ -104,7 +102,7 @@ public static function fromDsn(string $dsn, array $options = [], bool $debug = f
104102
$queueOptions['arguments'] = self::normalizeQueueArguments($queueOptions['arguments']);
105103
}
106104

107-
return new self($amqpOptions, $exchangeOptions, $queueOptions, $debug, $amqpFactory);
105+
return new self($amqpOptions, $exchangeOptions, $queueOptions, $amqpFactory);
108106
}
109107

110108
private static function normalizeQueueArguments(array $arguments): array
@@ -129,7 +127,7 @@ private static function normalizeQueueArguments(array $arguments): array
129127
*/
130128
public function publish(string $body, array $headers = []): void
131129
{
132-
if ($this->debug && $this->shouldSetup()) {
130+
if ($this->shouldSetup()) {
133131
$this->setup();
134132
}
135133

@@ -146,7 +144,7 @@ public function publish(string $body, array $headers = []): void
146144
*/
147145
public function get(): ?\AMQPEnvelope
148146
{
149-
if ($this->debug && $this->shouldSetup()) {
147+
if ($this->shouldSetup()) {
150148
$this->setup();
151149
}
152150

@@ -256,6 +254,14 @@ private function clear(): void
256254

257255
private function shouldSetup(): bool
258256
{
259-
return !\array_key_exists('auto-setup', $this->connectionCredentials) || !\in_array($this->connectionCredentials['auto-setup'], [false, 'false'], true);
257+
if (!array_key_exists('auto-setup', $this->connectionCredentials)) {
258+
return true;
259+
}
260+
261+
if (\in_array($this->connectionCredentials['auto-setup'], [false, 'false'], true)) {
262+
return false;
263+
}
264+
265+
return true;
260266
}
261267
}

0 commit comments

Comments
 (0)
0