Skip to content

Commit 26a1fce

Browse files
ENGCOM-8317: Passing arguments for queues as expected #26966
- Merge Pull Request #26966 from bartoszkubicki/magento2:message-queue-arguments-passing-from-config - Merged commits: 1. e91a3f1 2. 5ee0054 3. 00a03e9 4. d802328
2 parents 632a7c6 + d802328 commit 26a1fce

File tree

2 files changed

+82
-44
lines changed

2 files changed

+82
-44
lines changed

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

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\Framework\MessageQueue\Test\Unit\Topology\Config\QueueConfigItem;
911

1012
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
13+
use Magento\Framework\Exception\LocalizedException;
1114
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder;
1215
use Magento\Framework\MessageQueue\Topology\Config\Data;
1316
use Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem\DataMapper;
@@ -17,34 +20,46 @@
1720
class DataMapperTest extends TestCase
1821
{
1922
/**
20-
* @var MockObject
23+
* @var Data|MockObject
2124
*/
22-
private $configData;
25+
private $configDataMock;
2326

2427
/**
25-
* @var MockObject
28+
* @var CommunicationConfig|MockObject
2629
*/
27-
private $communicationConfig;
30+
private $communicationConfigMock;
2831

2932
/**
30-
* @var MockObject
33+
* @var ResponseQueueNameBuilder|MockObject
3134
*/
32-
private $queueNameBuilder;
35+
private $queueNameBuilderMock;
3336

3437
/**
3538
* @var DataMapper
3639
*/
3740
private $model;
3841

42+
/**
43+
* @return void
44+
*/
3945
protected function setUp(): void
4046
{
41-
$this->configData = $this->createMock(Data::class);
42-
$this->communicationConfig = $this->createMock(CommunicationConfig::class);
43-
$this->queueNameBuilder = $this->createMock(ResponseQueueNameBuilder::class);
44-
$this->model = new DataMapper($this->configData, $this->communicationConfig, $this->queueNameBuilder);
47+
$this->configDataMock = $this->createMock(Data::class);
48+
$this->communicationConfigMock = $this->createMock(CommunicationConfig::class);
49+
$this->queueNameBuilderMock = $this->createMock(ResponseQueueNameBuilder::class);
50+
$this->model = new DataMapper(
51+
$this->configDataMock,
52+
$this->communicationConfigMock,
53+
$this->queueNameBuilderMock
54+
);
4555
}
4656

47-
public function testGetMappedData()
57+
/**
58+
* @return void
59+
*
60+
* @throws LocalizedException
61+
*/
62+
public function testGetMappedData(): void
4863
{
4964
$data = [
5065
'ex01' => [
@@ -100,9 +115,11 @@ public function testGetMappedData()
100115
['topic02', ['name' => 'topic02', 'is_synchronous' => false]],
101116
];
102117

103-
$this->communicationConfig->expects($this->exactly(2))->method('getTopic')->willReturnMap($communicationMap);
104-
$this->configData->expects($this->once())->method('get')->willReturn($data);
105-
$this->queueNameBuilder->expects($this->once())
118+
$this->communicationConfigMock->expects($this->exactly(2))
119+
->method('getTopic')
120+
->willReturnMap($communicationMap);
121+
$this->configDataMock->expects($this->once())->method('get')->willReturn($data);
122+
$this->queueNameBuilderMock->expects($this->once())
106123
->method('getQueueName')
107124
->with('topic01')
108125
->willReturn('responseQueue.topic01');
@@ -114,23 +131,27 @@ public function testGetMappedData()
114131
'connection' => 'amqp',
115132
'durable' => true,
116133
'autoDelete' => false,
117-
'arguments' => [],
134+
'arguments' => ['some' => 'arguments'],
118135
],
119136
'some.queue--amqp' => [
120137
'name' => 'some.queue',
121138
'connection' => 'amqp',
122139
'durable' => true,
123140
'autoDelete' => false,
124-
'arguments' => [],
141+
'arguments' => ['some' => 'arguments'],
125142
],
126143
];
127144
$this->assertEquals($expectedResult, $actualResult);
128145
}
129146

130147
/**
148+
* @return void
149+
*
150+
* @throws LocalizedException
151+
*
131152
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
132153
*/
133-
public function testGetMappedDataForWildcard()
154+
public function testGetMappedDataForWildcard(): void
134155
{
135156
$data = [
136157
'ex01' => [
@@ -200,13 +221,15 @@ public function testGetMappedDataForWildcard()
200221
'topic08.part2.some.test' => ['name' => 'topic08.part2.some.test', 'is_synchronous' => true],
201222
];
202223

203-
$this->communicationConfig->expects($this->once())
224+
$this->communicationConfigMock->expects($this->once())
204225
->method('getTopic')
205226
->with('topic01')
206227
->willReturn(['name' => 'topic01', 'is_synchronous' => true]);
207-
$this->communicationConfig->expects($this->any())->method('getTopics')->willReturn($communicationData);
208-
$this->configData->expects($this->once())->method('get')->willReturn($data);
209-
$this->queueNameBuilder->expects($this->any())
228+
$this->communicationConfigMock->expects($this->any())
229+
->method('getTopics')
230+
->willReturn($communicationData);
231+
$this->configDataMock->expects($this->once())->method('get')->willReturn($data);
232+
$this->queueNameBuilderMock->expects($this->any())
210233
->method('getQueueName')
211234
->willReturnCallback(function ($value) {
212235
return 'responseQueue.' . $value;
@@ -219,49 +242,49 @@ public function testGetMappedDataForWildcard()
219242
'connection' => 'amqp',
220243
'durable' => true,
221244
'autoDelete' => false,
222-
'arguments' => [],
245+
'arguments' => ['some' => 'arguments'],
223246
],
224247
'some.queue--amqp' => [
225248
'name' => 'some.queue',
226249
'connection' => 'amqp',
227250
'durable' => true,
228251
'autoDelete' => false,
229-
'arguments' => [],
252+
'arguments' => ['some' => 'arguments'],
230253
],
231254
'responseQueue.topic02--amqp' => [
232255
'name' => 'responseQueue.topic02',
233256
'connection' => 'amqp',
234257
'durable' => true,
235258
'autoDelete' => false,
236-
'arguments' => [],
259+
'arguments' => ['some' => 'arguments'],
237260
],
238261
'responseQueue.topic03--amqp' => [
239262
'name' => 'responseQueue.topic03',
240263
'connection' => 'amqp',
241264
'durable' => true,
242265
'autoDelete' => false,
243-
'arguments' => [],
266+
'arguments' => ['some' => 'arguments'],
244267
],
245268
'responseQueue.topic04.04.04--amqp' => [
246269
'name' => 'responseQueue.topic04.04.04',
247270
'connection' => 'amqp',
248271
'durable' => true,
249272
'autoDelete' => false,
250-
'arguments' => [],
273+
'arguments' => ['some' => 'arguments'],
251274
],
252275
'responseQueue.topic05.05--amqp' => [
253276
'name' => 'responseQueue.topic05.05',
254277
'connection' => 'amqp',
255278
'durable' => true,
256279
'autoDelete' => false,
257-
'arguments' => [],
280+
'arguments' => ['some' => 'arguments'],
258281
],
259282
'responseQueue.topic08.part2.some.test--amqp' => [
260283
'name' => 'responseQueue.topic08.part2.some.test',
261284
'connection' => 'amqp',
262285
'durable' => true,
263286
'autoDelete' => false,
264-
'arguments' => [],
287+
'arguments' => ['some' => 'arguments'],
265288
]
266289
];
267290
$this->assertEquals($expectedResult, $actualResult);

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
8+
declare(strict_types=1);
9+
610
namespace Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem;
711

8-
use Magento\Framework\MessageQueue\Topology\Config\Data;
912
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
1013
use Magento\Framework\Exception\LocalizedException;
1114
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder;
15+
use Magento\Framework\MessageQueue\Topology\Config\Data;
1216
use Magento\Framework\Phrase;
1317

1418
/**
@@ -59,21 +63,29 @@ public function __construct(
5963
* Get mapped config data.
6064
*
6165
* @return array
66+
* @throws LocalizedException
6267
*/
63-
public function getMappedData()
68+
public function getMappedData(): array
6469
{
6570
if (null === $this->mappedData) {
66-
$this->mappedData = [];
71+
$mappedData = [];
6772
foreach ($this->configData->get() as $exchange) {
6873
$connection = $exchange['connection'];
6974
foreach ($exchange['bindings'] as $binding) {
7075
if ($binding['destinationType'] === 'queue') {
71-
$queueItems = $this->createQueueItems($binding['destination'], $binding['topic'], $connection);
72-
$this->mappedData = array_merge($this->mappedData, $queueItems);
76+
$queueItems = $this->createQueueItems(
77+
(string)$binding['destination'],
78+
(string)$binding['topic'],
79+
(array)$binding['arguments'],
80+
(string)$connection
81+
);
82+
$mappedData[] = $queueItems;
7383
}
7484
}
7585
}
86+
$this->mappedData = array_merge([], ...$mappedData);
7687
}
88+
7789
return $this->mappedData;
7890
}
7991

@@ -82,10 +94,12 @@ public function getMappedData()
8294
*
8395
* @param string $name
8496
* @param string $topic
97+
* @param array $arguments
8598
* @param string $connection
8699
* @return array
100+
* @throws LocalizedException
87101
*/
88-
private function createQueueItems($name, $topic, $connection)
102+
private function createQueueItems(string $name, string $topic, array $arguments, string $connection): array
89103
{
90104
$output = [];
91105
$synchronousTopics = [];
@@ -103,7 +117,7 @@ private function createQueueItems($name, $topic, $connection)
103117
'connection' => $connection,
104118
'durable' => true,
105119
'autoDelete' => false,
106-
'arguments' => [],
120+
'arguments' => $arguments,
107121
];
108122
}
109123

@@ -112,8 +126,9 @@ private function createQueueItems($name, $topic, $connection)
112126
'connection' => $connection,
113127
'durable' => true,
114128
'autoDelete' => false,
115-
'arguments' => [],
129+
'arguments' => $arguments,
116130
];
131+
117132
return $output;
118133
}
119134

@@ -124,15 +139,14 @@ private function createQueueItems($name, $topic, $connection)
124139
* @return bool
125140
* @throws LocalizedException
126141
*/
127-
private function isSynchronousTopic($topicName)
142+
private function isSynchronousTopic(string $topicName): bool
128143
{
129144
try {
130145
$topic = $this->communicationConfig->getTopic($topicName);
131-
$isSync = (bool)$topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
132-
} catch (LocalizedException $e) {
146+
return (bool)$topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
147+
} catch (LocalizedException $exception) {
133148
throw new LocalizedException(new Phrase('Error while checking if topic is synchronous'));
134149
}
135-
return $isSync;
136150
}
137151

138152
/**
@@ -141,7 +155,7 @@ private function isSynchronousTopic($topicName)
141155
* @param string $wildcard
142156
* @return array
143157
*/
144-
private function matchSynchronousTopics($wildcard)
158+
private function matchSynchronousTopics(string $wildcard): array
145159
{
146160
$topicDefinitions = array_filter(
147161
$this->communicationConfig->getTopics(),
@@ -152,11 +166,13 @@ function ($item) {
152166

153167
$topics = [];
154168
$pattern = $this->buildWildcardPattern($wildcard);
169+
155170
foreach (array_keys($topicDefinitions) as $topicName) {
156171
if (preg_match($pattern, $topicName)) {
157172
$topics[$topicName] = $topicName;
158173
}
159174
}
175+
160176
return $topics;
161177
}
162178

@@ -166,11 +182,10 @@ function ($item) {
166182
* @param string $wildcardKey
167183
* @return string
168184
*/
169-
private function buildWildcardPattern($wildcardKey)
185+
private function buildWildcardPattern(string $wildcardKey): string
170186
{
171187
$pattern = '/^' . str_replace('.', '\.', $wildcardKey);
172-
$pattern = str_replace('#', '.+', $pattern);
173-
$pattern = str_replace('*', '[^\.]+', $pattern);
188+
$pattern = str_replace(['#', '*'], ['.+', '[^\.]+'], $pattern);
174189
$pattern .= strpos($wildcardKey, '#') === strlen($wildcardKey) ? '/' : '$/';
175190
return $pattern;
176191
}

0 commit comments

Comments
 (0)