Skip to content

Commit e91a3f1

Browse files
author
Bartosz Kubicki
committed
Passing arguments for queues as expected
1 parent 04b376d commit e91a3f1

File tree

2 files changed

+70
-35
lines changed

2 files changed

+70
-35
lines changed

lib/internal/Magento/Framework/MessageQueue/Test/Unit/Topology/Config/QueueConfigItem/DataMapperTest.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
58
*/
69
namespace Magento\Framework\MessageQueue\Test\Unit\Topology\Config\QueueConfigItem;
710

8-
use Magento\Framework\MessageQueue\Topology\Config\Data;
911
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
12+
use Magento\Framework\Exception\LocalizedException;
1013
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder;
14+
use Magento\Framework\MessageQueue\Topology\Config\Data;
1115
use Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem\DataMapper;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
1218

13-
class DataMapperTest extends \PHPUnit\Framework\TestCase
19+
class DataMapperTest extends TestCase
1420
{
1521
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
22+
* @var Data|MockObject
1723
*/
1824
private $configData;
1925

2026
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
27+
* @var CommunicationConfig|MockObject
2228
*/
2329
private $communicationConfig;
2430

2531
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
32+
* @var ResponseQueueNameBuilder|MockObject
2733
*/
2834
private $queueNameBuilder;
2935

@@ -32,15 +38,23 @@ class DataMapperTest extends \PHPUnit\Framework\TestCase
3238
*/
3339
private $model;
3440

35-
protected function setUp()
41+
/**
42+
* @return void
43+
*/
44+
protected function setUp(): void
3645
{
3746
$this->configData = $this->createMock(Data::class);
3847
$this->communicationConfig = $this->createMock(CommunicationConfig::class);
3948
$this->queueNameBuilder = $this->createMock(ResponseQueueNameBuilder::class);
4049
$this->model = new DataMapper($this->configData, $this->communicationConfig, $this->queueNameBuilder);
4150
}
4251

43-
public function testGetMappedData()
52+
/**
53+
* @return void
54+
*
55+
* @throws LocalizedException
56+
*/
57+
public function testGetMappedData(): void
4458
{
4559
$data = [
4660
'ex01' => [
@@ -96,7 +110,9 @@ public function testGetMappedData()
96110
['topic02', ['name' => 'topic02', 'is_synchronous' => false]],
97111
];
98112

99-
$this->communicationConfig->expects($this->exactly(2))->method('getTopic')->willReturnMap($communicationMap);
113+
$this->communicationConfig->expects($this->exactly(2))
114+
->method('getTopic')
115+
->willReturnMap($communicationMap);
100116
$this->configData->expects($this->once())->method('get')->willReturn($data);
101117
$this->queueNameBuilder->expects($this->once())
102118
->method('getQueueName')
@@ -110,23 +126,27 @@ public function testGetMappedData()
110126
'connection' => 'amqp',
111127
'durable' => true,
112128
'autoDelete' => false,
113-
'arguments' => [],
129+
'arguments' => ['some' => 'arguments'],
114130
],
115131
'some.queue--amqp' => [
116132
'name' => 'some.queue',
117133
'connection' => 'amqp',
118134
'durable' => true,
119135
'autoDelete' => false,
120-
'arguments' => [],
136+
'arguments' => ['some' => 'arguments'],
121137
],
122138
];
123139
$this->assertEquals($expectedResult, $actualResult);
124140
}
125141

126142
/**
143+
* @return void
144+
*
145+
* @throws LocalizedException
146+
*
127147
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
128148
*/
129-
public function testGetMappedDataForWildcard()
149+
public function testGetMappedDataForWildcard(): void
130150
{
131151
$data = [
132152
'ex01' => [
@@ -200,7 +220,9 @@ public function testGetMappedDataForWildcard()
200220
->method('getTopic')
201221
->with('topic01')
202222
->willReturn(['name' => 'topic01', 'is_synchronous' => true]);
203-
$this->communicationConfig->expects($this->any())->method('getTopics')->willReturn($communicationData);
223+
$this->communicationConfig->expects($this->any())
224+
->method('getTopics')
225+
->willReturn($communicationData);
204226
$this->configData->expects($this->once())->method('get')->willReturn($data);
205227
$this->queueNameBuilder->expects($this->any())
206228
->method('getQueueName')
@@ -215,49 +237,49 @@ public function testGetMappedDataForWildcard()
215237
'connection' => 'amqp',
216238
'durable' => true,
217239
'autoDelete' => false,
218-
'arguments' => [],
240+
'arguments' => ['some' => 'arguments'],
219241
],
220242
'some.queue--amqp' => [
221243
'name' => 'some.queue',
222244
'connection' => 'amqp',
223245
'durable' => true,
224246
'autoDelete' => false,
225-
'arguments' => [],
247+
'arguments' => ['some' => 'arguments'],
226248
],
227249
'responseQueue.topic02--amqp' => [
228250
'name' => 'responseQueue.topic02',
229251
'connection' => 'amqp',
230252
'durable' => true,
231253
'autoDelete' => false,
232-
'arguments' => [],
254+
'arguments' => ['some' => 'arguments'],
233255
],
234256
'responseQueue.topic03--amqp' => [
235257
'name' => 'responseQueue.topic03',
236258
'connection' => 'amqp',
237259
'durable' => true,
238260
'autoDelete' => false,
239-
'arguments' => [],
261+
'arguments' => ['some' => 'arguments'],
240262
],
241263
'responseQueue.topic04.04.04--amqp' => [
242264
'name' => 'responseQueue.topic04.04.04',
243265
'connection' => 'amqp',
244266
'durable' => true,
245267
'autoDelete' => false,
246-
'arguments' => [],
268+
'arguments' => ['some' => 'arguments'],
247269
],
248270
'responseQueue.topic05.05--amqp' => [
249271
'name' => 'responseQueue.topic05.05',
250272
'connection' => 'amqp',
251273
'durable' => true,
252274
'autoDelete' => false,
253-
'arguments' => [],
275+
'arguments' => ['some' => 'arguments'],
254276
],
255277
'responseQueue.topic08.part2.some.test--amqp' => [
256278
'name' => 'responseQueue.topic08.part2.some.test',
257279
'connection' => 'amqp',
258280
'durable' => true,
259281
'autoDelete' => false,
260-
'arguments' => [],
282+
'arguments' => ['some' => 'arguments'],
261283
]
262284
];
263285
$this->assertEquals($expectedResult, $actualResult);

lib/internal/Magento/Framework/MessageQueue/Topology/Config/QueueConfigItem/DataMapper.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
58
*/
69
namespace Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem;
710

8-
use Magento\Framework\MessageQueue\Topology\Config\Data;
911
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
1012
use Magento\Framework\Exception\LocalizedException;
1113
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder;
14+
use Magento\Framework\MessageQueue\Topology\Config\Data;
1215
use Magento\Framework\Phrase;
1316

1417
/**
@@ -59,21 +62,28 @@ public function __construct(
5962
* Get mapped config data.
6063
*
6164
* @return array
65+
* @throws LocalizedException
6266
*/
63-
public function getMappedData()
67+
public function getMappedData(): array
6468
{
6569
if (null === $this->mappedData) {
6670
$this->mappedData = [];
6771
foreach ($this->configData->get() as $exchange) {
6872
$connection = $exchange['connection'];
6973
foreach ($exchange['bindings'] as $binding) {
7074
if ($binding['destinationType'] === 'queue') {
71-
$queueItems = $this->createQueueItems($binding['destination'], $binding['topic'], $connection);
72-
$this->mappedData = array_merge($this->mappedData, $queueItems);
75+
$queueItems = $this->createQueueItems(
76+
(string) $binding['destination'],
77+
(string) $binding['topic'],
78+
(array) $binding['arguments'],
79+
(string) $connection
80+
);
81+
$this->mappedData += $queueItems;
7382
}
7483
}
7584
}
7685
}
86+
7787
return $this->mappedData;
7888
}
7989

@@ -82,10 +92,12 @@ public function getMappedData()
8292
*
8393
* @param string $name
8494
* @param string $topic
95+
* @param array $arguments
8596
* @param string $connection
8697
* @return array
98+
* @throws LocalizedException
8799
*/
88-
private function createQueueItems($name, $topic, $connection)
100+
private function createQueueItems(string $name, string $topic, array $arguments, string $connection): array
89101
{
90102
$output = [];
91103
$synchronousTopics = [];
@@ -103,7 +115,7 @@ private function createQueueItems($name, $topic, $connection)
103115
'connection' => $connection,
104116
'durable' => true,
105117
'autoDelete' => false,
106-
'arguments' => [],
118+
'arguments' => $arguments,
107119
];
108120
}
109121

@@ -112,8 +124,9 @@ private function createQueueItems($name, $topic, $connection)
112124
'connection' => $connection,
113125
'durable' => true,
114126
'autoDelete' => false,
115-
'arguments' => [],
127+
'arguments' => $arguments,
116128
];
129+
117130
return $output;
118131
}
119132

@@ -124,15 +137,14 @@ private function createQueueItems($name, $topic, $connection)
124137
* @return bool
125138
* @throws LocalizedException
126139
*/
127-
private function isSynchronousTopic($topicName)
140+
private function isSynchronousTopic(string $topicName): bool
128141
{
129142
try {
130143
$topic = $this->communicationConfig->getTopic($topicName);
131-
$isSync = (bool)$topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
132-
} catch (LocalizedException $e) {
144+
return (bool) $topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
145+
} catch (LocalizedException $exception) {
133146
throw new LocalizedException(new Phrase('Error while checking if topic is synchronous'));
134147
}
135-
return $isSync;
136148
}
137149

138150
/**
@@ -141,22 +153,24 @@ private function isSynchronousTopic($topicName)
141153
* @param string $wildcard
142154
* @return array
143155
*/
144-
private function matchSynchronousTopics($wildcard)
156+
private function matchSynchronousTopics(string $wildcard): array
145157
{
146158
$topicDefinitions = array_filter(
147159
$this->communicationConfig->getTopics(),
148160
function ($item) {
149-
return (bool)$item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
161+
return (bool) $item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
150162
}
151163
);
152164

153165
$topics = [];
154166
$pattern = $this->buildWildcardPattern($wildcard);
167+
155168
foreach (array_keys($topicDefinitions) as $topicName) {
156169
if (preg_match($pattern, $topicName)) {
157170
$topics[$topicName] = $topicName;
158171
}
159172
}
173+
160174
return $topics;
161175
}
162176

@@ -166,11 +180,10 @@ function ($item) {
166180
* @param string $wildcardKey
167181
* @return string
168182
*/
169-
private function buildWildcardPattern($wildcardKey)
183+
private function buildWildcardPattern(string $wildcardKey): string
170184
{
171185
$pattern = '/^' . str_replace('.', '\.', $wildcardKey);
172-
$pattern = str_replace('#', '.+', $pattern);
173-
$pattern = str_replace('*', '[^\.]+', $pattern);
186+
$pattern = str_replace(['#', '*'], ['.+', '[^\.]+'], $pattern);
174187
$pattern .= strpos($wildcardKey, '#') === strlen($wildcardKey) ? '/' : '$/';
175188
return $pattern;
176189
}

0 commit comments

Comments
 (0)