Skip to content
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

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

Copy link
Contributor

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

Copy link
Author

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

Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, corrected

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -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;
Expand All @@ -17,17 +21,17 @@
class DataMapperTest extends TestCase
{
/**
* @var MockObject
* @var Data|MockObject
*/
private $configData;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;

Expand All @@ -36,6 +40,9 @@ class DataMapperTest extends TestCase
*/
private $model;

/**
* @return void
*/
protected function setUp(): void
{
$this->configData = $this->createMock(Data::class);
Expand All @@ -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' => [
Expand Down Expand Up @@ -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')
Expand All @@ -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' => [
Expand Down Expand Up @@ -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')
Expand All @@ -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);
Expand Down
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;

/**
Expand Down Expand Up @@ -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 = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->mappedData = [];
$mappedData = [];

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'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Could you remove the space between type casting and variable in all places in order to have similar code formatting as in other places in Magento, like this?
Suggested change
(string) $binding['destination'],
(string)$binding['destination'],
  1. Do we really need this casting? Do we have any case where we'll have another type?

(string) $binding['topic'],
(array) $binding['arguments'],
(string) $connection
);
$this->mappedData += $queueItems;
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
$this->mappedData = array_merge([], ...$mappedData);

}

return $this->mappedData;
}

Expand All @@ -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 = [];
Expand All @@ -103,7 +115,7 @@ private function createQueueItems($name, $topic, $connection)
'connection' => $connection,
'durable' => true,
'autoDelete' => false,
'arguments' => [],
'arguments' => $arguments,
];
}

Expand All @@ -112,8 +124,9 @@ private function createQueueItems($name, $topic, $connection)
'connection' => $connection,
'durable' => true,
'autoDelete' => false,
'arguments' => [],
'arguments' => $arguments,
];

return $output;
}

Expand All @@ -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;
}

/**
Expand All @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (bool) $item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
return (bool)$item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];

}
);

$topics = [];
$pattern = $this->buildWildcardPattern($wildcard);

foreach (array_keys($topicDefinitions) as $topicName) {
if (preg_match($pattern, $topicName)) {
$topics[$topicName] = $topicName;
}
}

return $topics;
}

Expand All @@ -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;
}
Expand Down