Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b8a655

Browse filesBrowse files
committed
Add more tests around the AMQP transport
1 parent b931902 commit 4b8a655
Copy full SHA for 4b8a655

File tree

5 files changed

+135
-12
lines changed
Filter options

5 files changed

+135
-12
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+3Lines changed: 3 additions & 0 deletions
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()
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
$transport = $factory->createTransport('amqp://localhost', array('foo' => 'bar'));
45+
46+
$this->assertEquals(
47+
new AmqpTransport($encoder, $decoder, Connection::fromDsn('amqp://localhost', array('foo' => 'bar'), true), array('foo' => 'bar'), true),
48+
$transport
49+
);
50+
}
51+
}
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\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+
class AmqpTransportTest extends TestCase
23+
{
24+
public function testItIsATransport()
25+
{
26+
$transport = $this->getTransport();
27+
28+
$this->assertInstanceOf(TransportInterface::class, $transport);
29+
}
30+
31+
public function testReceivesMessages()
32+
{
33+
$transport = $this->getTransport(
34+
null,
35+
$decoder = $this->getMockBuilder(DecoderInterface::class)->getMock(),
36+
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock()
37+
);
38+
39+
$decodedMessage = new DummyMessage('Decoded.');
40+
41+
$amqpEnvelope = $this->getMockBuilder(\AMQPEnvelope::class)->getMock();
42+
$amqpEnvelope->method('getBody')->willReturn('body');
43+
$amqpEnvelope->method('getHeaders')->willReturn(array('my' => 'header'));
44+
45+
$decoder->method('decode')->with(array('body' => 'body', 'headers' => array('my' => 'header')))->willReturn($decodedMessage);
46+
$connection->method('get')->willReturn($amqpEnvelope);
47+
48+
$transport->receive(function ($message) use ($transport, $decodedMessage) {
49+
$this->assertSame($decodedMessage, $message);
50+
51+
$transport->stop();
52+
});
53+
}
54+
55+
private function getTransport(EncoderInterface $encoder = null, DecoderInterface $decoder = null, Connection $connection = null, array $options = array(), bool $debug = false)
56+
{
57+
$encoder = $encoder ?: $this->getMockBuilder(EncoderInterface::class)->getMock();
58+
$decoder = $decoder ?: $this->getMockBuilder(DecoderInterface::class)->getMock();
59+
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
60+
61+
return new AmqpTransport(
62+
$encoder,
63+
$decoder,
64+
$connection,
65+
$options,
66+
$debug
67+
);
68+
}
69+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpTransport.php
+5-11Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ class AmqpTransport implements TransportInterface
2222
{
2323
private $encoder;
2424
private $decoder;
25-
private $dsn;
25+
private $connection;
2626
private $options;
2727
private $debug;
28-
private $connection;
2928
private $receiver;
3029
private $sender;
3130

32-
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder, string $dsn, array $options, bool $debug)
31+
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder, Connection $connection, array $options, bool $debug)
3332
{
3433
$this->encoder = $encoder;
3534
$this->decoder = $decoder;
36-
$this->dsn = $dsn;
35+
$this->connection = $connection;
3736
$this->options = $options;
3837
$this->debug = $debug;
3938
}
@@ -64,16 +63,11 @@ public function send($message): void
6463

6564
private function getReceiver()
6665
{
67-
return $this->receiver = new AmqpReceiver($this->decoder, $this->connection ?? $this->getConnection());
66+
return $this->receiver = new AmqpReceiver($this->decoder, $this->connection);
6867
}
6968

7069
private function getSender()
7170
{
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);
71+
return $this->sender = new AmqpSender($this->encoder, $this->connection);
7872
}
7973
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpTransportFactory.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ 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(
38+
$this->encoder,
39+
$this->decoder,
40+
Connection::fromDsn($dsn, $options, $this->debug),
41+
$options,
42+
$this->debug
43+
);
3844
}
3945

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.