This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class AsyncMultipleHandlersTest extends QueueTestCaseAbstract
{
/**
* @var string
*/
protected $expectedMessages;
/**
* @var string[]
*/
protected $consumers = [
'mtmh.queue.1.consumer',
'mtmh.queue.2.consumer',
];
/**
* @var string[]
*/
private $topicValueMap = [
'mtmh.topic.1' => 'mtmh.topic.1',
'mtmh.topic.2' => ['mtmh.topic.2-1', 'mtmh.topic.2-2']
];
/**
* @var string[]
*/
private $expectedValues = [
'string-mtmh.topic.1',
'mixed-mtmh.topic.1',
'array-mtmh.topic.2-1',
'array-mtmh.topic.2-2',
'mixed-mtmh.topic.2-1',
'mixed-mtmh.topic.2-2'
];
/**
* Verify that Queue Framework supports multiple topics per queue.
*
* Current test is not test of Web API framework itself,
* it just utilizes its infrastructure to test Message Queue.
*/
public function testAsynchronousRpcCommunication()
{
foreach ($this->topicValueMap as $topic => $data) {
$message = null;
if (is_array($data)) {
foreach ($data as $key => $value) {
/** @var AsyncTestData $testObject */
$testObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
$testObject->setValue($value);
$testObject->setTextFilePath($this->logFilePath);
$message[$key] = $testObject;
}
} else {
$testObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
$testObject->setValue($data);
$testObject->setTextFilePath($this->logFilePath);
$message = $testObject;
}
$this->publisher->publish($topic, $message);
}
$this->waitForAsynchronousResult(count($this->expectedValues), $this->logFilePath);
//assertions
foreach ($this->expectedValues as $item) {
$this->assertStringContainsString($item, file_get_contents($this->logFilePath));
}
}
}
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class AsyncMultipleTopicsWithEachQueueTest extends QueueTestCaseAbstract
{
/**
* @var string[]
*/
protected $uniqueID;
/**
* @var AsyncTestData
*/
protected $msgObject;
/**
* @var string[]
*/
protected $consumers = ['queue.for.multiple.topics.test.y', 'queue.for.multiple.topics.test.z'];
/**
* @var string[]
*/
private $topics = ['multi.topic.queue.topic.y', 'multi.topic.queue.topic.z'];
/**
* Verify that Queue Framework processes multiple asynchronous topics sent to the same queue.
*
* Current test is not test of Web API framework itself, it just utilizes its infrastructure to test Message Queue.
*/
public function testAsyncMultipleTopicsPerQueue()
{
$this->msgObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
foreach ($this->topics as $topic) {
// phpcs:ignore Magento2.Security.InsecureFunction
$this->uniqueID[$topic] = md5(uniqid($topic));
$this->msgObject->setValue($this->uniqueID[$topic] . "_" . $topic);
$this->msgObject->setTextFilePath($this->logFilePath);
$this->publisher->publish($topic, $this->msgObject);
}
$this->waitForAsynchronousResult(count($this->uniqueID), $this->logFilePath);
//assertions
foreach ($this->topics as $item) {
$this->assertStringContainsString(
$this->uniqueID[$item] . "_" . $item,
file_get_contents($this->logFilePath)
);
}
}
}
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase\DeprecatedFormat;
use Magento\Framework\MessageQueue\UseCase\QueueTestCaseAbstract;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class AsyncMultiTopicsSeparateQueuesTest extends QueueTestCaseAbstract
{
/**
* @var string[]
*/
protected $uniqueID;
/**
* @var AsyncTestData
*/
protected $msgObject;
/**
* @var string[]
*/
protected $consumers = [
'queue.for.multiple.topics.test.c.deprecated',
'queue.for.multiple.topics.test.d.deprecated'
];
/**
* @var string[]
*/
private $topics = ['multi.topic.queue.topic.c.deprecated', 'multi.topic.queue.topic.d.deprecated'];
/**
* Verify that Queue Framework processes multiple asynchronous topics sent to the same queue.
*
* Current test is not test of Web API framework itself, it just utilizes its infrastructure to test Message Queue.
*/
public function testAsyncMultipleTopicsPerQueue()
{
$this->msgObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
foreach ($this->topics as $topic) {
// phpcs:ignore Magento2.Security.InsecureFunction
$this->uniqueID[$topic] = md5(uniqid($topic));
$this->msgObject->setValue($this->uniqueID[$topic] . "_" . $topic);
$this->msgObject->setTextFilePath($this->logFilePath);
$this->publisher->publish($topic, $this->msgObject);
}
$this->waitForAsynchronousResult(count($this->uniqueID), $this->logFilePath);
//assertions
foreach ($this->topics as $item) {
$this->assertStringContainsString(
$this->uniqueID[$item] . "_" . $item,
file_get_contents($this->logFilePath)
);
}
}
}
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase\DeprecatedFormat;
use Magento\Framework\MessageQueue\UseCase\QueueTestCaseAbstract;
class RpcCommunicationTest extends QueueTestCaseAbstract
{
/**
* {@inheritdoc}
*/
protected $consumers = ['synchronousRpcTestConsumer.deprecated'];
/**
* Verify that RPC call based on Rabbit MQ is processed correctly.
*
* Current test is not test of Web API framework itself, it just utilizes its infrastructure to test RPC.
*/
public function testSynchronousRpcCommunication()
{
$input = 'Input value';
$response = $this->publisher->publish('synchronous.rpc.test.deprecated', $input);
$this->assertEquals($input . ' processed by RPC handler', $response);
}
}
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class MixSyncAndAsyncSingleQueueTest extends QueueTestCaseAbstract
{
/**
* @var AsyncTestData
*/
protected $msgObject;
/**
* {@inheritdoc}
*/
protected $consumers = ['mixed.sync.and.async.queue.consumer'];
/**
* @var string[]
*/
protected $messages = ['message1', 'message2', 'message3'];
/**
* @var int
*/
protected $maxMessages = 4;
public function testMixSyncAndAsyncSingleQueue()
{
$this->msgObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
// Publish asynchronous messages
foreach ($this->messages as $item) {
$this->msgObject->setValue($item);
$this->msgObject->setTextFilePath($this->logFilePath);
$this->publisher->publish('multi.topic.queue.topic.c', $this->msgObject);
}
// Publish synchronous message to the same queue
$input = 'Input value';
$response = $this->publisher->publish('sync.topic.for.mixed.sync.and.async.queue', $input);
$this->assertEquals($input . ' processed by RPC handler', $response);
$this->waitForAsynchronousResult(count($this->messages), $this->logFilePath);
// Verify that asynchronous messages were processed
foreach ($this->messages as $item) {
$this->assertStringContainsString($item, file_get_contents($this->logFilePath));
}
}
}
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
class MultipleTopicsPerQueueTest extends QueueTestCaseAbstract
{
/**
* {@inheritdoc}
*/
protected $consumers = [
'queue.for.multiple.topics.test.a',
'queue.for.multiple.topics.test.b'
];
/**
* Verify that Queue Framework supports multiple topics per queue.
*
* Current test is not test of Web API framework itself,
* it just utilizes its infrastructure to test Message Queue.
*/
public function testSynchronousRpcCommunication()
{
foreach (['multi.topic.queue.topic.a', 'multi.topic.queue.topic.b'] as $topic) {
$input = "Input value for topic '{$topic}'";
$response = $this->publisher->publish($topic, $input);
$this->assertEquals($input . ' processed by RPC handler', $response);
}
}
}
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\MessageQueue\PublisherInterface;
use Magento\TestFramework\MessageQueue\PublisherConsumerController;
use Magento\TestFramework\MessageQueue\EnvironmentPreconditionException;
use Magento\TestFramework\MessageQueue\PreconditionFailedException;
/**
* Base test case for message queue tests.
*/
class QueueTestCaseAbstract extends \PHPUnit\Framework\TestCase
{
/**
* @var string[]
*/
protected $consumers = [];
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var PublisherInterface
*/
protected $publisher;
/**
* @var string
*/
protected $logFilePath;
/**
* @var int|null
*/
protected $maxMessages = null;
/**
* @var PublisherConsumerController
*/
protected $publisherConsumerController;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
$this->publisherConsumerController = $this->objectManager->create(
PublisherConsumerController::class,
[
'consumers' => $this->consumers,
'logFilePath' => $this->logFilePath,
'maxMessages' => $this->maxMessages,
'appInitParams' => \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams()
]
);
try {
$this->publisherConsumerController->initialize();
} catch (EnvironmentPreconditionException $e) {
$this->markTestSkipped($e->getMessage());
} catch (PreconditionFailedException $e) {
$this->fail(
$e->getMessage()
);
}
$this->publisher = $this->publisherConsumerController->getPublisher();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->publisherConsumerController->stopConsumers();
}
/**
* Wait for asynchronous handlers to log data to file.
*
* @param int $expectedLinesCount
* @param string $logFilePath
*/
protected function waitForAsynchronousResult($expectedLinesCount, $logFilePath)
{
try {
//$expectedLinesCount, $logFilePath
$this->publisherConsumerController->waitForAsynchronousResult(
[$this, 'checkLogsExists'],
[$expectedLinesCount, $logFilePath]
);
} catch (PreconditionFailedException $e) {
$this->fail($e->getMessage());
}
}
/**
* Checks that logs exist
*
* @param int $expectedLinesCount
* @return bool
*/
public function checkLogsExists($expectedLinesCount)
{
//phpcs:ignore Magento2.Functions.DiscouragedFunction
$actualCount = file_exists($this->logFilePath) ? count(file($this->logFilePath)) : 0;
return $expectedLinesCount === $actualCount;
}
/**
* Workaround for https://bugs.php.net/bug.php?id=72286
* phpcs:disable Magento2.Functions.StaticFunction
*/
public static function tearDownAfterClass(): void
{
// phpcs:enable Magento2.Functions.StaticFunction
if (version_compare(phpversion(), '7') == -1) {
$closeConnection = new \ReflectionMethod(\Magento\Amqp\Model\Config::class, 'closeConnection');
$closeConnection->setAccessible(true);
$config = Bootstrap::getObjectManager()->get(\Magento\Amqp\Model\Config::class);
$closeConnection->invoke($config);
}
}
}
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\TestModuleSynchronousAmqp\Api\ServiceInterface;
class RemoteServiceCommunicationTest extends QueueTestCaseAbstract
{
/**
* {@inheritdoc}
*/
protected $consumers = ['RemoteServiceTestConsumer'];
public function testRemoteServiceCommunication()
{
$input = 'Input value';
/** @var ServiceInterface $generatedRemoteService */
$generatedRemoteService = $this->objectManager->get(ServiceInterface::class);
$response = $generatedRemoteService->execute($input);
$this->assertEquals($input . ' processed by RPC handler', $response);
}
}
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
class RpcCommunicationTest extends QueueTestCaseAbstract
{
/**
* {@inheritdoc}
*/
protected $consumers = ['synchronousRpcTestConsumer'];
/**
* Verify that RPC call based on Rabbit MQ is processed correctly.
*
* Current test is not test of Web API framework itself, it just utilizes its infrastructure to test RPC.
*/
public function testSynchronousRpcCommunication()
{
$input = 'Input value';
$response = $this->publisher->publish('synchronous.rpc.test', $input);
$this->assertEquals($input . ' processed by RPC handler', $response);
}
}
@@ -0,0 +1,162 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\Framework\App\DeploymentConfig\FileReader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract
{
/**
* @var FileReader
*/
private $reader;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var array
*/
private $config;
/**
* @var AsyncTestData
*/
protected $msgObject;
/**
* {@inheritdoc}
*/
protected $consumers = ['mixed.sync.and.async.queue.consumer'];
/**
* @var string[]
*/
protected $messages = ['message1', 'message2', 'message3'];
/**
* @var int|null
*/
protected $maxMessages = 4;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
// phpstan:ignore "Class Magento\TestModuleAsyncAmqp\Model\AsyncTestData not found."
$this->msgObject = $this->objectManager->create(AsyncTestData::class);
$this->reader = $this->objectManager->get(FileReader::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->config = $this->loadConfig();
}
/**
* Check if consumers wait for messages from the queue
*/
public function testWaitForMessages()
{
$this->publisherConsumerController->stopConsumers();
$config = $this->config;
$config['queue']['consumers_wait_for_messages'] = 1;
$this->writeConfig($config);
$loadedConfig = $this->loadConfig();
$this->assertArrayHasKey('queue', $loadedConfig);
$this->assertArrayHasKey('consumers_wait_for_messages', $loadedConfig['queue']);
$this->assertEquals(1, $loadedConfig['queue']['consumers_wait_for_messages']);
foreach ($this->messages as $message) {
$this->publishMessage($message);
}
$this->publisherConsumerController->startConsumers();
$this->waitForAsynchronousResult(count($this->messages), $this->logFilePath);
foreach ($this->messages as $item) {
$this->assertStringContainsString($item, file_get_contents($this->logFilePath));
}
$this->publishMessage('message4');
$this->waitForAsynchronousResult(count($this->messages) + 1, $this->logFilePath);
$this->assertStringContainsString('message4', file_get_contents($this->logFilePath));
}
/**
* Check if consumers do not wait for messages from the queue and die
*/
public function testNotWaitForMessages(): void
{
$this->publisherConsumerController->stopConsumers();
$config = $this->config;
$config['queue']['consumers_wait_for_messages'] = 0;
$this->writeConfig($config);
$loadedConfig = $this->loadConfig();
$this->assertArrayHasKey('queue', $loadedConfig);
$this->assertArrayHasKey('consumers_wait_for_messages', $loadedConfig['queue']);
$this->assertEquals(0, $loadedConfig['queue']['consumers_wait_for_messages']);
foreach ($this->messages as $message) {
$this->publishMessage($message);
}
$this->publisherConsumerController->startConsumers();
$this->waitForAsynchronousResult(count($this->messages), $this->logFilePath);
foreach ($this->messages as $item) {
$this->assertStringContainsString($item, file_get_contents($this->logFilePath));
}
// Checks that consumers do not wait 4th message and die
$consumersProcessIds = $this->publisherConsumerController->getConsumersProcessIds();
$this->assertArrayHasKey('mixed.sync.and.async.queue.consumer', $consumersProcessIds);
$this->assertEquals([], $consumersProcessIds['mixed.sync.and.async.queue.consumer']);
}
/**
* @param string $message
*/
private function publishMessage(string $message): void
{
$this->msgObject->setValue($message);
$this->msgObject->setTextFilePath($this->logFilePath);
$this->publisher->publish('multi.topic.queue.topic.c', $this->msgObject);
}
/**
* @return array
*/
private function loadConfig(): array
{
return $this->reader->load(ConfigFilePool::APP_ENV);
}
/**
* @param array $config
*/
private function writeConfig(array $config): void
{
$writer = $this->objectManager->get(Writer::class);
$writer->saveConfig([ConfigFilePool::APP_ENV => $config], true);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();
$this->writeConfig($this->config);
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\UseCase;
use Magento\TestModuleAsyncAmqp\Model\AsyncTestData;
class WildcardTopicTest extends QueueTestCaseAbstract
{
/**
* @var string[]
*/
protected $consumers = [
'wildcard.queue.one.consumer',
'wildcard.queue.two.consumer',
'wildcard.queue.three.consumer',
'wildcard.queue.four.consumer',
];
/**
* @param string $topic
* @param string[] $matchingQueues
* @param string[] $nonMatchingQueues
*
* @dataProvider wildCardTopicsDataProvider
*/
public function testWildCardMatchingTopic($topic, $matchingQueues, $nonMatchingQueues)
{
$testObject = $this->generateTestObject();
$this->publisher->publish($topic, $testObject);
$this->waitForAsynchronousResult(count($matchingQueues), $this->logFilePath);
$this->assertFileExists($this->logFilePath, "No handlers invoked (log file was not created).");
foreach ($nonMatchingQueues as $queueName) {
$this->assertStringNotContainsString($queueName, file_get_contents($this->logFilePath));
}
foreach ($matchingQueues as $queueName) {
$this->assertStringContainsString($queueName, file_get_contents($this->logFilePath));
}
}
public function wildCardTopicsDataProvider()
{
return [
'segment1.segment2.segment3.wildcard' => [
'segment1.segment2.segment3.wildcard',
['wildcard.queue.one', 'wildcard.queue.two', 'wildcard.queue.four'],
['wildcard.queue.three']
],
'segment2.segment3.wildcard' => [
'segment2.segment3.wildcard',
['wildcard.queue.one', 'wildcard.queue.three', 'wildcard.queue.four'],
['wildcard.queue.two']
]
];
}
public function testWildCardNonMatchingTopic()
{
$testObject = $this->generateTestObject();
$this->publisher->publish('not.matching.wildcard.topic', $testObject);
sleep(2);
$this->assertFileDoesNotExist($this->logFilePath, "No log file must be created for non-matching topic.");
}
/**
* @return AsyncTestData
*/
private function generateTestObject()
{
$testObject = $this->objectManager->create(AsyncTestData::class); // @phpstan-ignore-line
$testObject->setValue('||Message Contents||');
$testObject->setTextFilePath($this->logFilePath);
return $testObject;
}
}