Skip to content

Commit 51e8d90

Browse files
committed
Merge remote-tracking branch 'local/ACP2E-2244' into PR_06_SEP_2023
2 parents ea257d6 + 9aaaeb4 commit 51e8d90

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

lib/internal/Magento/Framework/MessageQueue/MessageValidator.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\MessageQueue;
77

88
use InvalidArgumentException;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\Phrase;
1112
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
@@ -20,6 +21,15 @@ class MessageValidator
2021
*/
2122
private $communicationConfig;
2223

24+
/**
25+
* @param CommunicationConfig|null $communicationConfig
26+
*/
27+
public function __construct(CommunicationConfig $communicationConfig = null)
28+
{
29+
$this->communicationConfig = $communicationConfig
30+
?? ObjectManager::getInstance()->get(CommunicationConfig::class);
31+
}
32+
2333
/**
2434
* Identify message data schema by topic.
2535
*
@@ -30,7 +40,7 @@ class MessageValidator
3040
*/
3141
protected function getTopicSchema($topic, $requestType)
3242
{
33-
$topicConfig = $this->getCommunicationConfig()->getTopic($topic);
43+
$topicConfig = $this->communicationConfig->getTopic($topic);
3444
if ($topicConfig === null) {
3545
throw new LocalizedException(new Phrase('Specified topic "%topic" is not declared.', ['topic' => $topic]));
3646
}
@@ -117,12 +127,14 @@ protected function validatePrimitiveType($message, $messageType, $topic)
117127
{
118128
$compareType = $messageType;
119129
$realType = $this->getRealType($message);
120-
if ($realType == 'array' && count($message) == 0) {
121-
return;
122-
} elseif ($realType == 'array' && isset($message[0])) {
123-
$realType = $this->getRealType($message[0]);
130+
if ($realType == 'array') {
124131
$compareType = preg_replace('/\[\]/', '', $messageType);
132+
foreach ($message as $subMessage) {
133+
$this->validatePrimitiveType($subMessage, $compareType, $topic);
134+
}
135+
return;
125136
}
137+
126138
if ($realType !== $compareType) {
127139
throw new InvalidArgumentException(
128140
new Phrase(
@@ -151,12 +163,14 @@ protected function validateClassType($message, $messageType, $topic)
151163
$origMessage = $message;
152164
$compareType = $messageType;
153165
$realType = $this->getRealType($message);
154-
if ($realType == 'array' && count($message) == 0) {
155-
return;
156-
} elseif ($realType == 'array' && isset($message[0])) {
157-
$message = $message[0];
166+
if ($realType == 'array') {
158167
$compareType = preg_replace('/\[\]/', '', $messageType);
168+
foreach ($message as $subMessage) {
169+
$this->validateClassType($subMessage, $compareType, $topic);
170+
}
171+
return;
159172
}
173+
160174
if (!($message instanceof $compareType)) {
161175
throw new InvalidArgumentException(
162176
new Phrase(
@@ -185,21 +199,4 @@ private function getRealType($message)
185199
$type = $type == 'double' ? 'float' : $type;
186200
return $type == "integer" ? "int" : $type;
187201
}
188-
189-
/**
190-
* Get communication config.
191-
*
192-
* @return CommunicationConfig
193-
*
194-
* @deprecated 103.0.0
195-
*/
196-
private function getCommunicationConfig()
197-
{
198-
if ($this->communicationConfig === null) {
199-
$this->communicationConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(
200-
CommunicationConfig::class
201-
);
202-
}
203-
return $this->communicationConfig;
204-
}
205202
}

lib/internal/Magento/Framework/MessageQueue/Test/Unit/MessageValidatorTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Customer\Api\Data\CustomerInterface;
1111
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
1212
use Magento\Framework\MessageQueue\MessageValidator;
13-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1413
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
1615

@@ -28,17 +27,10 @@ class MessageValidatorTest extends TestCase
2827

2928
protected function setUp(): void
3029
{
31-
$objectManager = new ObjectManager($this);
32-
33-
$this->model = $objectManager->getObject(MessageValidator::class);
3430
$this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
3531
->disableOriginalConstructor()
3632
->getMock();
37-
$objectManager->setBackwardCompatibleProperty(
38-
$this->model,
39-
'communicationConfig',
40-
$this->communicationConfigMock
41-
);
33+
$this->model = new MessageValidator($this->communicationConfigMock);
4234
}
4335

4436
public function testValidateInvalidTopic()
@@ -191,6 +183,14 @@ public function getQueueConfigRequestType()
191183
['string1', 'string2'],
192184
null
193185
],
186+
[
187+
[
188+
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
189+
CommunicationConfig::TOPIC_REQUEST => 'string[]'
190+
],
191+
[10 => 'string1', 20 => 'string2'],
192+
null
193+
],
194194
[
195195
[
196196
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
@@ -207,6 +207,14 @@ public function getQueueConfigRequestType()
207207
'single string',
208208
'Data in topic "topic" must be of type "string[]". "string" given.'
209209
],
210+
[
211+
[
212+
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
213+
CommunicationConfig::TOPIC_REQUEST => 'string[]'
214+
],
215+
['string1', 2],
216+
'Data in topic "topic" must be of type "string". "int" given.'
217+
],
210218
[
211219
[
212220
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
@@ -231,6 +239,14 @@ public function getQueueConfigRequestType()
231239
[$customerMock, $customerMockTwo],
232240
null
233241
],
242+
[
243+
[
244+
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
245+
CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
246+
],
247+
[10 => $customerMock, 20 => $customerMockTwo],
248+
null
249+
],
234250
[
235251
[
236252
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
@@ -261,7 +277,15 @@ public function getQueueConfigRequestType()
261277
CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
262278
],
263279
[1=>23, 3=>545],
264-
'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface[]". '
280+
'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface".'
281+
],
282+
[
283+
[
284+
CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
285+
CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
286+
],
287+
[$customerMock, 545],
288+
'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface".'
265289
],
266290
];
267291
}

0 commit comments

Comments
 (0)