Skip to content

Commit 55ea3fc

Browse files
committed
Use new MessageQueueConfig interface, update unit tests
1 parent 0d881fc commit 55ea3fc

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

app/code/Magento/MysqlMq/Model/Driver/Bulk/Exchange.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\MysqlMq\Model\Driver\Bulk;
77

88
use Magento\Framework\MessageQueue\Bulk\ExchangeInterface;
9-
use Magento\Framework\MessageQueue\ConfigInterface as MessageQueueConfig;
9+
use Magento\Framework\MessageQueue\Topology\ConfigInterface as MessageQueueConfig;
1010
use Magento\MysqlMq\Model\QueueManagement;
1111

1212
/**
@@ -41,7 +41,19 @@ public function __construct(MessageQueueConfig $messageQueueConfig, QueueManagem
4141
*/
4242
public function enqueue($topic, array $envelopes)
4343
{
44-
$queueNames = $this->messageQueueConfig->getQueuesByTopic($topic);
44+
$queueNames = [];
45+
$exchanges = $this->messageQueueConfig->getExchanges();
46+
foreach ($exchanges as $exchange) {
47+
// @todo Is there a more reliable way to identify MySQL exchanges?
48+
if ($exchange->getConnection() == 'db') {
49+
foreach ($exchange->getBindings() as $binding) {
50+
// This only supports exact matching of topics.
51+
if ($binding->getTopic() == $topic) {
52+
$queueNames[] = $binding->getDestination();
53+
}
54+
}
55+
}
56+
}
4557
$messages = array_map(
4658
function ($envelope) {
4759
return $envelope->getBody();

app/code/Magento/MysqlMq/Model/Driver/Exchange.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Framework\MessageQueue\EnvelopeInterface;
99
use Magento\Framework\MessageQueue\ExchangeInterface;
10-
use Magento\Framework\MessageQueue\ConfigInterface as MessageQueueConfig;
10+
use Magento\Framework\MessageQueue\Topology\ConfigInterface as MessageQueueConfig;
1111
use Magento\MysqlMq\Model\QueueManagement;
1212

1313
class Exchange implements ExchangeInterface
@@ -43,7 +43,19 @@ public function __construct(MessageQueueConfig $messageQueueConfig, QueueManagem
4343
*/
4444
public function enqueue($topic, EnvelopeInterface $envelope)
4545
{
46-
$queueNames = $this->messageQueueConfig->getQueuesByTopic($topic);
46+
$queueNames = [];
47+
$exchanges = $this->messageQueueConfig->getExchanges();
48+
foreach ($exchanges as $exchange) {
49+
// @todo Is there a more reliable way to identify MySQL exchanges?
50+
if ($exchange->getConnection() == 'db') {
51+
foreach ($exchange->getBindings() as $binding) {
52+
// This only supports exact matching of topics.
53+
if ($binding->getTopic() == $topic) {
54+
$queueNames[] = $binding->getDestination();
55+
}
56+
}
57+
}
58+
}
4759
$this->queueManagement->addMessageToQueues($topic, $envelope->getBody(), $queueNames);
4860
return null;
4961
}

app/code/Magento/MysqlMq/Setup/Recurring.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Magento\Framework\Setup\InstallSchemaInterface;
99
use Magento\Framework\Setup\ModuleContextInterface;
1010
use Magento\Framework\Setup\SchemaSetupInterface;
11-
use Magento\Framework\MessageQueue\ConfigInterface as MessageQueueConfig;
11+
use Magento\Framework\MessageQueue\Topology\ConfigInterface as MessageQueueConfig;
1212

1313
/**
1414
* Class Recurring
@@ -35,10 +35,9 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
3535
{
3636
$setup->startSetup();
3737

38-
$binds = $this->messageQueueConfig->getBinds();
3938
$queues = [];
40-
foreach ($binds as $bind) {
41-
$queues[] = $bind[MessageQueueConfig::BIND_QUEUE];
39+
foreach ($this->messageQueueConfig->getQueues() as $queue) {
40+
$queues[] = $queue->getName();
4241
}
4342
$connection = $setup->getConnection();
4443
$existingQueues = $connection->fetchCol($connection->select()->from($setup->getTable('queue'), 'name'));

app/code/Magento/MysqlMq/Test/Unit/Model/Driver/Bulk/ExchangeTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class ExchangeTest extends \PHPUnit\Framework\TestCase
1313
{
1414
/**
15-
* @var \Magento\Framework\MessageQueue\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
15+
* @var \Magento\Framework\MessageQueue\Topology\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
1616
*/
1717
private $messageQueueConfig;
1818

@@ -33,7 +33,7 @@ class ExchangeTest extends \PHPUnit\Framework\TestCase
3333
*/
3434
protected function setUp()
3535
{
36-
$this->messageQueueConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConfigInterface::class)
36+
$this->messageQueueConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\Topology\ConfigInterface::class)
3737
->disableOriginalConstructor()->getMock();
3838
$this->queueManagement = $this->getMockBuilder(\Magento\MysqlMq\Model\QueueManagement::class)
3939
->disableOriginalConstructor()->getMock();
@@ -56,10 +56,40 @@ protected function setUp()
5656
public function testEnqueue()
5757
{
5858
$topicName = 'topic.name';
59-
$queueNames = ['queue0', 'queue1'];
59+
$queueNames = ['queue0'];
60+
61+
$binding1 = $this->createMock(\Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItem\BindingInterface::class);
62+
$binding1->expects($this->once())
63+
->method('getTopic')
64+
->willReturn($topicName);
65+
$binding1->expects($this->once())
66+
->method('getDestination')
67+
->willReturn($queueNames[0]);
68+
69+
$binding2 = $this->createMock(\Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItem\BindingInterface::class);
70+
$binding2->expects($this->once())
71+
->method('getTopic')
72+
->willReturn('different.topic');
73+
$binding2->expects($this->never())
74+
->method('getDestination');
75+
76+
$exchange1 = $this->createMock(\Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItemInterface::class);
77+
$exchange1->expects($this->once())
78+
->method('getConnection')
79+
->willReturn('db');
80+
$exchange1->expects($this->once())
81+
->method('getBindings')
82+
->willReturn([$binding1, $binding2]);
83+
$exchange2 = $this->createMock(\Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItemInterface::class);
84+
$exchange2->expects($this->once())
85+
->method('getConnection')
86+
->willReturn('amqp');
87+
$exchange2->expects($this->never())
88+
->method('getBindings');
89+
6090
$envelopeBody = 'serializedMessage';
6191
$this->messageQueueConfig->expects($this->once())
62-
->method('getQueuesByTopic')->with($topicName)->willReturn($queueNames);
92+
->method('getExchanges')->willReturn([$exchange1, $exchange2]);
6393
$envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
6494
->disableOriginalConstructor()->getMock();
6595
$envelope->expects($this->once())->method('getBody')->willReturn($envelopeBody);

app/code/Magento/MysqlMq/Test/Unit/Setup/RecurringTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RecurringTest extends \PHPUnit\Framework\TestCase
2424
private $model;
2525

2626
/**
27-
* @var \Magento\Framework\MessageQueue\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
27+
* @var \Magento\Framework\MessageQueue\Topology\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
2828
*/
2929
private $messageQueueConfig;
3030

@@ -34,7 +34,7 @@ class RecurringTest extends \PHPUnit\Framework\TestCase
3434
protected function setUp()
3535
{
3636
$this->objectManager = new ObjectManager($this);
37-
$this->messageQueueConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConfigInterface::class)
37+
$this->messageQueueConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\Topology\ConfigInterface::class)
3838
->getMockForAbstractClass();
3939
$this->model = $this->objectManager->getObject(
4040
\Magento\MysqlMq\Setup\Recurring::class,
@@ -49,23 +49,14 @@ protected function setUp()
4949
*/
5050
public function testInstall()
5151
{
52-
$binds = [
53-
'first_bind' => [
54-
'queue' => 'queue_name_1',
55-
'exchange' => 'magento-db',
56-
'topic' => 'queue.topic.1'
57-
],
58-
'second_bind' => [
59-
'queue' => 'queue_name_2',
60-
'exchange' => 'magento-db',
61-
'topic' => 'queue.topic.2'
62-
],
63-
'third_bind' => [
64-
'queue' => 'queue_name_3',
65-
'exchange' => 'magento-db',
66-
'topic' => 'queue.topic.3'
67-
]
68-
];
52+
for ($i = 1; $i <=3; $i++) {
53+
$queue = $this->createMock(\Magento\Framework\MessageQueue\Topology\Config\QueueConfigItemInterface::class);
54+
$queue->expects($this->once())
55+
->method('getName')
56+
->willReturn('queue_name_'. $i);
57+
$queues[] = $queue;
58+
}
59+
6960
$dbQueues = [
7061
'queue_name_1',
7162
'queue_name_2',
@@ -81,7 +72,7 @@ public function testInstall()
8172
->getMockForAbstractClass();
8273

8374
$setup->expects($this->once())->method('startSetup')->willReturnSelf();
84-
$this->messageQueueConfig->expects($this->once())->method('getBinds')->willReturn($binds);
75+
$this->messageQueueConfig->expects($this->once())->method('getQueues')->willReturn($queues);
8576
$connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
8677
->getMockForAbstractClass();
8778
$setup->expects($this->once())->method('getConnection')->willReturn($connection);

0 commit comments

Comments
 (0)