Skip to content

Commit fad43a1

Browse files
ENGCOM-8155: Fix for queue numeric argument conversion #26967
- 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
2 parents afd7ecc + 0c6d204 commit fad43a1

File tree

3 files changed

+101
-14
lines changed

3 files changed

+101
-14
lines changed

dev/tests/integration/testsuite/Magento/Framework/MessageQueue/TopologyTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\MessageQueue;
710

11+
use Magento\TestFramework\Helper\Amqp;
812
use Magento\TestFramework\Helper\Bootstrap;
9-
use Magento\TestFramework\MessageQueue\PreconditionFailedException;
13+
use PHPUnit\Framework\TestCase;
1014

1115
/**
1216
* @see dev/tests/integration/_files/Magento/TestModuleMessageQueueConfiguration
1317
* @see dev/tests/integration/_files/Magento/TestModuleMessageQueueConfigOverride
1418
*/
15-
class TopologyTest extends \PHPUnit\Framework\TestCase
19+
class TopologyTest extends TestCase
1620
{
1721
/**
1822
* List of declared exchanges.
@@ -22,13 +26,16 @@ class TopologyTest extends \PHPUnit\Framework\TestCase
2226
private $declaredExchanges;
2327

2428
/**
25-
* @var \Magento\TestFramework\Helper\Amqp
29+
* @var Amqp
2630
*/
2731
private $helper;
2832

33+
/**
34+
* @return void
35+
*/
2936
protected function setUp(): void
3037
{
31-
$this->helper = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Helper\Amqp::class);
38+
$this->helper = Bootstrap::getObjectManager()->create(Amqp::class);
3239

3340
if (!$this->helper->isAvailable()) {
3441
$this->fail('This test relies on RabbitMQ Management Plugin.');
@@ -42,23 +49,27 @@ protected function setUp(): void
4249
* @param array $expectedConfig
4350
* @param array $bindingConfig
4451
*/
45-
public function testTopologyInstallation(array $expectedConfig, array $bindingConfig)
52+
public function testTopologyInstallation(array $expectedConfig, array $bindingConfig): void
4653
{
4754
$name = $expectedConfig['name'];
4855
$this->assertArrayHasKey($name, $this->declaredExchanges);
49-
unset($this->declaredExchanges[$name]['message_stats']);
50-
unset($this->declaredExchanges[$name]['user_who_performed_action']);
56+
unset(
57+
$this->declaredExchanges[$name]['message_stats'],
58+
$this->declaredExchanges[$name]['user_who_performed_action']
59+
);
60+
5161
$this->assertEquals(
5262
$expectedConfig,
5363
$this->declaredExchanges[$name],
5464
'Invalid exchange configuration: ' . $name
5565
);
5666

5767
$bindings = $this->helper->getExchangeBindings($name);
58-
$bindings = array_map(function ($value) {
68+
$bindings = array_map(static function ($value) {
5969
unset($value['properties_key']);
6070
return $value;
6171
}, $bindings);
72+
6273
$this->assertEquals(
6374
$bindingConfig,
6475
$bindings,
@@ -70,7 +81,7 @@ public function testTopologyInstallation(array $expectedConfig, array $bindingCo
7081
* @return array
7182
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
7283
*/
73-
public function exchangeDataProvider()
84+
public function exchangeDataProvider(): array
7485
{
7586
return [
7687
'magento-topic-based-exchange1' => [
@@ -121,7 +132,7 @@ public function exchangeDataProvider()
121132
'arguments' => [
122133
'argument1' => 'value',
123134
'argument2' => true,
124-
'argument3' => '150',
135+
'argument3' => 150,
125136
],
126137
],
127138
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright © Magento, Inc. All rights reserved.
7+
* See COPYING.txt for license details.
8+
*/
9+
10+
namespace Magento\Framework\Amqp\Test\Unit\Topology;
11+
12+
use InvalidArgumentException;
13+
use Magento\Framework\Amqp\Topology\ArgumentProcessor;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class ArgumentProcessorTest extends TestCase
18+
{
19+
/**
20+
* @var ArgumentProcessor|MockObject
21+
*/
22+
private $argumentProcessor;
23+
24+
/**
25+
* @return void
26+
*/
27+
public function testProcessArgumentsWhenAnyArgumentIsIncorrect(): void
28+
{
29+
$arguments = [
30+
'test' => new class {
31+
}
32+
];
33+
34+
$this->expectException(InvalidArgumentException::class);
35+
$this->argumentProcessor->processArguments($arguments);
36+
}
37+
38+
/**
39+
* @return void
40+
*/
41+
public function testProcessArgumentsWhenAllArgumentAreCorrect(): void
42+
{
43+
$arguments = [
44+
'array_type' => ['some_key' => 'some_value'],
45+
'numeric_value' => '25',
46+
'integer_value' => 26,
47+
'boolean_value' => false,
48+
'string_value' => 'test'
49+
];
50+
51+
$expected = [
52+
'array_type' => ['A', ['some_key' => 'some_value']],
53+
'numeric_value' => ['I', 25],
54+
'integer_value' => ['I', 26],
55+
'boolean_value' => ['t', false],
56+
'string_value' => ['S', 'test']
57+
];
58+
59+
$this->assertSame($expected, $this->argumentProcessor->processArguments($arguments));
60+
}
61+
62+
/**
63+
* @return void
64+
*/
65+
protected function setUp(): void
66+
{
67+
parent::setUp();
68+
$this->argumentProcessor = $this->getMockForTrait(ArgumentProcessor::class);
69+
}
70+
}

lib/internal/Magento/Framework/Amqp/Topology/ArgumentProcessor.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
58
*/
69
namespace Magento\Framework\Amqp\Topology;
710

11+
use InvalidArgumentException;
12+
813
/**
914
* @deprecated 103.0.0
1015
* see: https://github.com/php-amqplib/php-amqplib/issues/405
@@ -17,22 +22,23 @@ trait ArgumentProcessor
1722
* @param array $arguments
1823
* @return array
1924
*/
20-
public function processArguments($arguments)
25+
public function processArguments($arguments): array
2126
{
2227
$output = [];
2328
foreach ($arguments as $key => $value) {
2429
if (is_array($value)) {
2530
$output[$key] = ['A', $value];
26-
} elseif (is_int($value)) {
27-
$output[$key] = ['I', $value];
31+
} elseif (is_numeric($value)) {
32+
$output[$key] = ['I', (int) $value];
2833
} elseif (is_bool($value)) {
2934
$output[$key] = ['t', $value];
3035
} elseif (is_string($value)) {
3136
$output[$key] = ['S', $value];
3237
} else {
33-
throw new \InvalidArgumentException('Unknown argument type ' . gettype($value));
38+
throw new InvalidArgumentException('Unknown argument type ' . gettype($value));
3439
}
3540
}
41+
3642
return $output;
3743
}
3844
}

0 commit comments

Comments
 (0)