Skip to content

Fix for queue numeric argument conversion #26967

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

Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -3,16 +3,20 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\MessageQueue;

use Magento\TestFramework\Helper\Amqp;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\MessageQueue\PreconditionFailedException;
use PHPUnit\Framework\TestCase;

/**
* @see dev/tests/integration/_files/Magento/TestModuleMessageQueueConfiguration
* @see dev/tests/integration/_files/Magento/TestModuleMessageQueueConfigOverride
*/
class TopologyTest extends \PHPUnit\Framework\TestCase
class TopologyTest extends TestCase
{
/**
* List of declared exchanges.
Expand All @@ -22,13 +26,16 @@ class TopologyTest extends \PHPUnit\Framework\TestCase
private $declaredExchanges;

/**
* @var \Magento\TestFramework\Helper\Amqp
* @var Amqp
*/
private $helper;

/**
* @return void
*/
protected function setUp(): void
{
$this->helper = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Helper\Amqp::class);
$this->helper = Bootstrap::getObjectManager()->create(Amqp::class);

if (!$this->helper->isAvailable()) {
$this->fail('This test relies on RabbitMQ Management Plugin.');
Expand All @@ -42,23 +49,27 @@ protected function setUp(): void
* @param array $expectedConfig
* @param array $bindingConfig
*/
public function testTopologyInstallation(array $expectedConfig, array $bindingConfig)
public function testTopologyInstallation(array $expectedConfig, array $bindingConfig): void
{
$name = $expectedConfig['name'];
$this->assertArrayHasKey($name, $this->declaredExchanges);
unset($this->declaredExchanges[$name]['message_stats']);
unset($this->declaredExchanges[$name]['user_who_performed_action']);
unset(
$this->declaredExchanges[$name]['message_stats'],
$this->declaredExchanges[$name]['user_who_performed_action']
);

$this->assertEquals(
$expectedConfig,
$this->declaredExchanges[$name],
'Invalid exchange configuration: ' . $name
);

$bindings = $this->helper->getExchangeBindings($name);
$bindings = array_map(function ($value) {
$bindings = array_map(static function ($value) {
unset($value['properties_key']);
return $value;
}, $bindings);

$this->assertEquals(
$bindingConfig,
$bindings,
Expand All @@ -70,7 +81,7 @@ public function testTopologyInstallation(array $expectedConfig, array $bindingCo
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function exchangeDataProvider()
public function exchangeDataProvider(): array
{
return [
'magento-topic-based-exchange1' => [
Expand Down Expand Up @@ -121,7 +132,7 @@ public function exchangeDataProvider()
'arguments' => [
'argument1' => 'value',
'argument2' => true,
'argument3' => '150',
'argument3' => 150,
],
],
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Amqp\Test\Unit\Topology;

use InvalidArgumentException;
use Magento\Framework\Amqp\Topology\ArgumentProcessor;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ArgumentProcessorTest extends TestCase
{
/**
* @var ArgumentProcessor|MockObject
*/
private $argumentProcessor;

/**
* @return void
*/
public function testProcessArgumentsWhenAnyArgumentIsIncorrect(): void
{
$arguments = [
'test' => new class {
}
];

$this->expectException(InvalidArgumentException::class);
$this->argumentProcessor->processArguments($arguments);
}

/**
* @return void
*/
public function testProcessArgumentsWhenAllArgumentAreCorrect(): void
{
$arguments = [
'array_type' => ['some_key' => 'some_value'],
'numeric_value' => '25',
'integer_value' => 26,
'boolean_value' => false,
'string_value' => 'test'
];

$expected = [
'array_type' => ['A', ['some_key' => 'some_value']],
'numeric_value' => ['I', 25],
'integer_value' => ['I', 26],
'boolean_value' => ['t', false],
'string_value' => ['S', 'test']
];

$this->assertSame($expected, $this->argumentProcessor->processArguments($arguments));
}

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->argumentProcessor = $this->getMockForTrait(ArgumentProcessor::class);
}
}
14 changes: 10 additions & 4 deletions lib/internal/Magento/Framework/Amqp/Topology/ArgumentProcessor.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

declare(strict_types=1);

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Amqp\Topology;

use InvalidArgumentException;

/**
* @deprecated 103.0.0
* see: https://github.com/php-amqplib/php-amqplib/issues/405
Expand All @@ -17,22 +22,23 @@ trait ArgumentProcessor
* @param array $arguments
* @return array
*/
public function processArguments($arguments)
public function processArguments($arguments): array
{
$output = [];
foreach ($arguments as $key => $value) {
if (is_array($value)) {
$output[$key] = ['A', $value];
} elseif (is_int($value)) {
$output[$key] = ['I', $value];
} elseif (is_numeric($value)) {
$output[$key] = ['I', (int) $value];
} elseif (is_bool($value)) {
$output[$key] = ['t', $value];
} elseif (is_string($value)) {
$output[$key] = ['S', $value];
} else {
throw new \InvalidArgumentException('Unknown argument type ' . gettype($value));
throw new InvalidArgumentException('Unknown argument type ' . gettype($value));
}
}

return $output;
}
}