Skip to content

Commit

Permalink
ENGCOM-8155: Fix for queue numeric argument conversion #26967
Browse files Browse the repository at this point in the history
 - Merge Pull Request #26967 from bartoszkubicki/magento2:queue-exchange-argument-processor-fix
 - Merged commits:
   1. d60cbb9
   2. 5e7d9f6
   3. 314ec92
   4. 69eca07
   5. 1fd7015
   6. 7e97fb4
   7. 8c8b9cf
   8. 0c6d204
  • Loading branch information
magento-engcom-team committed Oct 16, 2020
2 parents afd7ecc + 0c6d204 commit fad43a1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
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;
}
}

0 comments on commit fad43a1

Please sign in to comment.