-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing arguments for queues as expected #26966
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
|
@@ -8,6 +11,7 @@ | |
namespace Magento\Framework\MessageQueue\Test\Unit\Topology\Config\QueueConfigItem; | ||
|
||
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder; | ||
use Magento\Framework\MessageQueue\Topology\Config\Data; | ||
use Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem\DataMapper; | ||
|
@@ -17,17 +21,17 @@ | |
class DataMapperTest extends TestCase | ||
{ | ||
/** | ||
* @var MockObject | ||
* @var Data|MockObject | ||
*/ | ||
private $configData; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add "Mock" suffix to all property names that contains mock object? |
||
|
||
/** | ||
* @var MockObject | ||
* @var CommunicationConfig|MockObject | ||
*/ | ||
private $communicationConfig; | ||
|
||
/** | ||
* @var MockObject | ||
* @var ResponseQueueNameBuilder|MockObject | ||
*/ | ||
private $queueNameBuilder; | ||
|
||
|
@@ -36,6 +40,9 @@ class DataMapperTest extends TestCase | |
*/ | ||
private $model; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->configData = $this->createMock(Data::class); | ||
|
@@ -44,7 +51,12 @@ protected function setUp(): void | |
$this->model = new DataMapper($this->configData, $this->communicationConfig, $this->queueNameBuilder); | ||
} | ||
|
||
public function testGetMappedData() | ||
/** | ||
* @return void | ||
* | ||
* @throws LocalizedException | ||
*/ | ||
public function testGetMappedData(): void | ||
{ | ||
$data = [ | ||
'ex01' => [ | ||
|
@@ -100,7 +112,9 @@ public function testGetMappedData() | |
['topic02', ['name' => 'topic02', 'is_synchronous' => false]], | ||
]; | ||
|
||
$this->communicationConfig->expects($this->exactly(2))->method('getTopic')->willReturnMap($communicationMap); | ||
$this->communicationConfig->expects($this->exactly(2)) | ||
->method('getTopic') | ||
->willReturnMap($communicationMap); | ||
$this->configData->expects($this->once())->method('get')->willReturn($data); | ||
$this->queueNameBuilder->expects($this->once()) | ||
->method('getQueueName') | ||
|
@@ -114,23 +128,27 @@ public function testGetMappedData() | |
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'some.queue--amqp' => [ | ||
'name' => 'some.queue', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
]; | ||
$this->assertEquals($expectedResult, $actualResult); | ||
} | ||
|
||
/** | ||
* @return void | ||
* | ||
* @throws LocalizedException | ||
* | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function testGetMappedDataForWildcard() | ||
public function testGetMappedDataForWildcard(): void | ||
{ | ||
$data = [ | ||
'ex01' => [ | ||
|
@@ -204,7 +222,9 @@ public function testGetMappedDataForWildcard() | |
->method('getTopic') | ||
->with('topic01') | ||
->willReturn(['name' => 'topic01', 'is_synchronous' => true]); | ||
$this->communicationConfig->expects($this->any())->method('getTopics')->willReturn($communicationData); | ||
$this->communicationConfig->expects($this->any()) | ||
->method('getTopics') | ||
->willReturn($communicationData); | ||
$this->configData->expects($this->once())->method('get')->willReturn($data); | ||
$this->queueNameBuilder->expects($this->any()) | ||
->method('getQueueName') | ||
|
@@ -219,49 +239,49 @@ public function testGetMappedDataForWildcard() | |
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'some.queue--amqp' => [ | ||
'name' => 'some.queue', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'responseQueue.topic02--amqp' => [ | ||
'name' => 'responseQueue.topic02', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'responseQueue.topic03--amqp' => [ | ||
'name' => 'responseQueue.topic03', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'responseQueue.topic04.04.04--amqp' => [ | ||
'name' => 'responseQueue.topic04.04.04', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'responseQueue.topic05.05--amqp' => [ | ||
'name' => 'responseQueue.topic05.05', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
], | ||
'responseQueue.topic08.part2.some.test--amqp' => [ | ||
'name' => 'responseQueue.topic08.part2.some.test', | ||
'connection' => 'amqp', | ||
'durable' => true, | ||
'autoDelete' => false, | ||
'arguments' => [], | ||
'arguments' => ['some' => 'arguments'], | ||
] | ||
]; | ||
$this->assertEquals($expectedResult, $actualResult); | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,14 +1,17 @@ | ||||||||
<?php | ||||||||
|
||||||||
declare(strict_types=1); | ||||||||
|
||||||||
/** | ||||||||
* Copyright © Magento, Inc. All rights reserved. | ||||||||
* See COPYING.txt for license details. | ||||||||
*/ | ||||||||
namespace Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem; | ||||||||
|
||||||||
use Magento\Framework\MessageQueue\Topology\Config\Data; | ||||||||
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig; | ||||||||
use Magento\Framework\Exception\LocalizedException; | ||||||||
use Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder; | ||||||||
use Magento\Framework\MessageQueue\Topology\Config\Data; | ||||||||
use Magento\Framework\Phrase; | ||||||||
|
||||||||
/** | ||||||||
|
@@ -59,21 +62,28 @@ public function __construct( | |||||||
* Get mapped config data. | ||||||||
* | ||||||||
* @return array | ||||||||
* @throws LocalizedException | ||||||||
*/ | ||||||||
public function getMappedData() | ||||||||
public function getMappedData(): array | ||||||||
{ | ||||||||
if (null === $this->mappedData) { | ||||||||
$this->mappedData = []; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
foreach ($this->configData->get() as $exchange) { | ||||||||
$connection = $exchange['connection']; | ||||||||
foreach ($exchange['bindings'] as $binding) { | ||||||||
if ($binding['destinationType'] === 'queue') { | ||||||||
$queueItems = $this->createQueueItems($binding['destination'], $binding['topic'], $connection); | ||||||||
$this->mappedData = array_merge($this->mappedData, $queueItems); | ||||||||
$queueItems = $this->createQueueItems( | ||||||||
(string) $binding['destination'], | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
(string) $binding['topic'], | ||||||||
(array) $binding['arguments'], | ||||||||
(string) $connection | ||||||||
); | ||||||||
$this->mappedData += $queueItems; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately it might be not an equal to the array_merge. Could you fix it as described here https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md? |
||||||||
} | ||||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
return $this->mappedData; | ||||||||
} | ||||||||
|
||||||||
|
@@ -82,10 +92,12 @@ public function getMappedData() | |||||||
* | ||||||||
* @param string $name | ||||||||
* @param string $topic | ||||||||
* @param array $arguments | ||||||||
* @param string $connection | ||||||||
* @return array | ||||||||
* @throws LocalizedException | ||||||||
*/ | ||||||||
private function createQueueItems($name, $topic, $connection) | ||||||||
private function createQueueItems(string $name, string $topic, array $arguments, string $connection): array | ||||||||
{ | ||||||||
$output = []; | ||||||||
$synchronousTopics = []; | ||||||||
|
@@ -103,7 +115,7 @@ private function createQueueItems($name, $topic, $connection) | |||||||
'connection' => $connection, | ||||||||
'durable' => true, | ||||||||
'autoDelete' => false, | ||||||||
'arguments' => [], | ||||||||
'arguments' => $arguments, | ||||||||
]; | ||||||||
} | ||||||||
|
||||||||
|
@@ -112,8 +124,9 @@ private function createQueueItems($name, $topic, $connection) | |||||||
'connection' => $connection, | ||||||||
'durable' => true, | ||||||||
'autoDelete' => false, | ||||||||
'arguments' => [], | ||||||||
'arguments' => $arguments, | ||||||||
]; | ||||||||
|
||||||||
return $output; | ||||||||
} | ||||||||
|
||||||||
|
@@ -124,15 +137,14 @@ private function createQueueItems($name, $topic, $connection) | |||||||
* @return bool | ||||||||
* @throws LocalizedException | ||||||||
*/ | ||||||||
private function isSynchronousTopic($topicName) | ||||||||
private function isSynchronousTopic(string $topicName): bool | ||||||||
{ | ||||||||
try { | ||||||||
$topic = $this->communicationConfig->getTopic($topicName); | ||||||||
$isSync = (bool)$topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]; | ||||||||
} catch (LocalizedException $e) { | ||||||||
return (bool) $topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]; | ||||||||
} catch (LocalizedException $exception) { | ||||||||
throw new LocalizedException(new Phrase('Error while checking if topic is synchronous')); | ||||||||
} | ||||||||
return $isSync; | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -141,22 +153,24 @@ private function isSynchronousTopic($topicName) | |||||||
* @param string $wildcard | ||||||||
* @return array | ||||||||
*/ | ||||||||
private function matchSynchronousTopics($wildcard) | ||||||||
private function matchSynchronousTopics(string $wildcard): array | ||||||||
{ | ||||||||
$topicDefinitions = array_filter( | ||||||||
$this->communicationConfig->getTopics(), | ||||||||
function ($item) { | ||||||||
return (bool)$item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]; | ||||||||
return (bool) $item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
); | ||||||||
|
||||||||
$topics = []; | ||||||||
$pattern = $this->buildWildcardPattern($wildcard); | ||||||||
|
||||||||
foreach (array_keys($topicDefinitions) as $topicName) { | ||||||||
if (preg_match($pattern, $topicName)) { | ||||||||
$topics[$topicName] = $topicName; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return $topics; | ||||||||
} | ||||||||
|
||||||||
|
@@ -166,11 +180,10 @@ function ($item) { | |||||||
* @param string $wildcardKey | ||||||||
* @return string | ||||||||
*/ | ||||||||
private function buildWildcardPattern($wildcardKey) | ||||||||
private function buildWildcardPattern(string $wildcardKey): string | ||||||||
{ | ||||||||
$pattern = '/^' . str_replace('.', '\.', $wildcardKey); | ||||||||
$pattern = str_replace('#', '.+', $pattern); | ||||||||
$pattern = str_replace('*', '[^\.]+', $pattern); | ||||||||
$pattern = str_replace(['#', '*'], ['.+', '[^\.]+'], $pattern); | ||||||||
$pattern .= strpos($wildcardKey, '#') === strlen($wildcardKey) ? '/' : '$/'; | ||||||||
return $pattern; | ||||||||
} | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move it back to prev. position
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have corrected rest of comments, but I don't understand this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict type declaration usually goes after copyright notice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, corrected