|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Framework\MessageQueue; |
| 10 | + |
| 11 | +use Magento\Framework\App\DeploymentConfig\FileReader; |
| 12 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 15 | +use Magento\Framework\Filesystem; |
| 16 | +use Magento\Framework\MessageQueue\Config; |
| 17 | +use Magento\Framework\MessageQueue\Config\Data; |
| 18 | +use Magento\Framework\MessageQueue\Config\Reader\Xml; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +class ConfigGetConsumersTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\ObjectManagerInterface |
| 26 | + */ |
| 27 | + private $objectManager; |
| 28 | + /** |
| 29 | + * @var Config |
| 30 | + */ |
| 31 | + private $configSubject; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var FileReader |
| 35 | + */ |
| 36 | + private $fileReader; |
| 37 | + /** |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + private $envConfigBackup; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Writer |
| 44 | + */ |
| 45 | + private $fileWriter; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Xml |
| 49 | + */ |
| 50 | + private $xmlReader; |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritdoc |
| 54 | + */ |
| 55 | + protected function setUp(): void |
| 56 | + { |
| 57 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 58 | + $this->fileWriter = $this->objectManager->get(Writer::class); |
| 59 | + $this->xmlReader = $this->objectManager->create(Xml::class); |
| 60 | + $this->fileReader = $this->objectManager->get(FileReader::class); |
| 61 | + |
| 62 | + $this->envConfigBackup = $this->fileReader->load(ConfigFilePool::APP_ENV); |
| 63 | + $customEnvConfig = $this->buildCustomEnvConfigWithConsumers(); |
| 64 | + $this->fileWriter->saveConfig([ConfigFilePool::APP_ENV => $customEnvConfig]); |
| 65 | + |
| 66 | + /** @var Data data */ |
| 67 | + $configData = $this->objectManager->create( |
| 68 | + Data::class, |
| 69 | + [ |
| 70 | + 'cacheId' => uniqid(microtime()) |
| 71 | + ] |
| 72 | + ); |
| 73 | + |
| 74 | + $this->configSubject = $this->objectManager->create( |
| 75 | + Config::class, |
| 76 | + [ |
| 77 | + 'queueConfigData' => $configData |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGetConsumers(): void |
| 83 | + { |
| 84 | + $consumers = $this->configSubject->getConsumers(); |
| 85 | + |
| 86 | + foreach ($consumers as $consumer) { |
| 87 | + $this->assertIsString($consumer['name']); |
| 88 | + $this->assertIsArray($consumer['handlers']); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheritdoc |
| 94 | + */ |
| 95 | + protected function tearDown(): void |
| 96 | + { |
| 97 | + $filesystem = $this->objectManager->get(Filesystem::class); |
| 98 | + $configFilePool = $this->objectManager->get(ConfigFilePool::class); |
| 99 | + $filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile( |
| 100 | + $configFilePool->getPath(ConfigFilePool::APP_ENV), |
| 101 | + "<?php\n return array();\n" |
| 102 | + ); |
| 103 | + $this->fileWriter->saveConfig([ConfigFilePool::APP_ENV => $this->envConfigBackup]); |
| 104 | + } |
| 105 | + |
| 106 | + private function buildCustomEnvConfigWithConsumers(): array |
| 107 | + { |
| 108 | + $data = $this->xmlReader->read(); |
| 109 | + $names = array_keys($data['consumers']); |
| 110 | + $consumers = []; |
| 111 | + foreach ($names as $name) { |
| 112 | + $consumers[$name] = ['connection' => 'amqp']; |
| 113 | + } |
| 114 | + |
| 115 | + return [ |
| 116 | + 'queue' => [ |
| 117 | + 'amqp' => [ |
| 118 | + 'host' => 'localhost', |
| 119 | + 'port' => '5672', |
| 120 | + 'user' => 'guest', |
| 121 | + 'password' => 'guest', |
| 122 | + 'virtualhost' => '/', |
| 123 | + 'ssl' => '' |
| 124 | + ], |
| 125 | + 'consumers' => $consumers |
| 126 | + ], |
| 127 | + ]; |
| 128 | + } |
| 129 | +} |
0 commit comments