10000 minor #27206 [Messenger] Add more tests around the AMQP transport (sr… · symfony/symfony@ebbe1ad · GitHub
[go: up one dir, main page]

Skip to content

Commit ebbe1ad

Browse files
committed
minor #27206 [Messenger] Add more tests around the AMQP transport (sroze)
This PR was merged into the 4.1 branch. Discussion ---------- [Messenger] Add more tests around the AMQP transport | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø Adding more tests to the AMQP transport/factory. These should have captured the following 3 bugs: #27198, #27197, #27196. Commits ------- faf9382 Add more tests around the AMQP transport
2 parents 64a4a2b + faf9382 commit ebbe1ad

File tree

5 files changed

+122
-15
lines changed

5 files changed

+122
-15
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
3737
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
3838
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
39+
use Symfony\Component\Messenger\Transport\TransportFactory;
3940
use Symfony\Component\PropertyAccess\PropertyAccessor;
4041
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
4142
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
@@ -528,6 +529,8 @@ public function testMessenger()
528529
$container = $this->createContainerFromFile('messenger');
529530
$this->assertTrue($container->hasAlias('message_bus'));
530531
$this->assertFalse($container->hasDefinition('messenger.transport.amqp.factory'));
532+
$this->assertTrue($container->hasDefinition('messenger.transport_factory'));
533+
$this->assertSame(TransportFactory::class, $container->getDefinition('messenger.transport_factory')->getClass());
531534
}
532535

533536
public function testMessengerTransports()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpTransport;
16+
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpTransportFactory;
17+
use Symfony\Component\Messenger\Transport\AmqpExt\Connection;
18+
use Symfony\Component\Messenger\Transport\Serialization\DecoderInterface;
19+
use Symfony\Component\Messenger\Transport\Serialization\EncoderInterface;
20+
21+
class AmqpTransportFactoryTest extends TestCase
22+
{
23+
public function testSupportsOnlyAmqpTransports()
24+
{
25+
$factory = new AmqpTransportFactory(
26+
$this->getMockBuilder(EncoderInterface::class)->getMock(),
27+
$this->getMockBuilder(DecoderInterface::class)->getMock(),
28+
true
29+
);
30+
31+
$this->assertTrue($factory->supports('amqp://localhost', array()));
32+
$this->assertFalse($factory->supports('sqs://localhost', array()));
33+
$this->assertFalse($factory->supports('invalid-dsn', array()));
34+
}
35+
36+
public function testItCreatesTheTransport()
37+
{
38+
$factory = new AmqpTransportFactory(
39+
$encoder = $this->getMockBuilder(EncoderInterface::class)->getMock(),
40+
$decoder = $this->getMockBuilder(DecoderInterface::class)->getMock(),
41+
true
42+
);
43+
44+
$expectedTransport = new AmqpTransport($encoder, $decoder, Connection::fromDsn('amqp://localhost', array('foo' => 'bar'), true), array('foo' => 'bar'), true);
45+
46+
$this->assertEquals($expectedTransport, $factory->createTransport('amqp://localhost', array('foo' => 'bar')));
47+
}
48+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt;
13+
14+
use PHPUnit\Framework\TestCase F438 ;
15+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
16+
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpTransport;
17+
use Symfony\Component\Messenger\Transport\AmqpExt\Connection;
18+
use Symfony\Component\Messenger\Transport\Serialization\DecoderInterface;
19+
use Symfony\Component\Messenger\Transport\Serialization\EncoderInterface;
20+
use Symfony\Component\Messenger\Transport\TransportInterface;
21+
22+
/**
23+
* @requires extension amqp
24+
*/
25+
class AmqpTransportTest extends TestCase
26+
{
27+
public function testItIsATransport()
28+
{
29+
$transport = $this->getTransport();
30+
31+
$this->assertInstanceOf(TransportInterface::class, $transport);
32+
}
33+
34+
public function testReceivesMessages()
35+
{
36+
$transport = $this->getTransport(
37+
null,
38+
$decoder = $this->getMockBuilder(DecoderInterface::class)->getMock(),
39+
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock()
40+
);
41+
42+
$decodedMessage = new DummyMessage('Decoded.');
43+
44+
$amqpEnvelope = $this->getMockBuilder(\AMQPEnvelope::class)->getMock();
45+
$amqpEnvelope->method('getBody')->willReturn('body');
46+
$amqpEnvelope->method('getHeaders')->willReturn(array('my' => 'header'));
47+
48+
$decoder->method('decode')->with(array('body' => 'body', 'headers' => array('my' => 'header')))->willReturn($decodedMessage);
49+
$connection->method('get')->willReturn($amqpEnvelope);
50+
51+
$transport->receive(function ($message) use ($transport, $decodedMessage) {
52+
$this->assertSame($decodedMessage, $message);
53+
54+
$transport->stop();
55+
});
56+
}
57+
58+
private function getTransport(EncoderInterface $encoder = null, DecoderInterface $decoder = null, Connection $connection = null)
59+
{
60+
$encoder = $encoder ?: $this->getMockBuilder(EncoderInterface::class)->getMock();
61+
$decoder = $decoder ?: $this->getMockBuilder(DecoderInterface::class)->getMock();
62+
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
63+
64+
return new AmqpTransport($encoder, $decoder, $connection);
65+
}
66+
}

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@ class AmqpTransport implements TransportInterface
2222
{
2323
private $encoder;
2424
private $decoder;
25-
private $dsn;
26-
private $options;
27-
private $debug;
2825
private $connection;
2926
private $receiver;
3027
private $sender;
3128

32-
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder, string $dsn, array $options, bool $debug)
29+
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder, Connection $connection)
3330
{
3431
$this->encoder = $encoder;
3532
$this->decoder = $decoder;
36-
$this->dsn = $dsn;
37-
$this->options = $options;
38-
$this->debug = $debug;
33+
$this->connection = $connection;
3934
}
4035

4136
/**
@@ -64,16 +59,11 @@ public function send($message): void
6459

6560
private function getReceiver()
6661
{
67-
return $this->receiver = new AmqpReceiver($this->decoder, $this->connection ?? $this->getConnection());
62+
return $this->receiver = new AmqpReceiver($this->decoder, $this->connection);
6863
}
6964

7065
private function getSender()
7166
{
72-
return $this->sender = new AmqpSender($this->encoder, $this->connection ?? $this->getConnection());
73-
}
74-
75-
private function getConnection()
76-
{
77-
return $this->connection = Connection::fromDsn($this->dsn, $this->options, $this->debug);
67+
return $this->sender = new AmqpSender($this->encoder, $this->connection);
7868
}
7969
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(EncoderInterface $encoder, DecoderInterface $decoder
3434

3535
public function createTransport(string $dsn, array $options): TransportInterface
3636
{
37-
return new AmqpTransport($this->encoder, $this->decoder, $dsn, $options, $this->debug);
37+
return new AmqpTransport($this->encoder, $this->decoder, Connection::fromDsn($dsn, $options, $this->debug));
3838
}
3939

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

0 commit comments

Comments
 (0)
0