ConfigReaderPluginTest.php 3.2 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\MessageQueue\Test\Unit\Config\Consumer;

use Magento\Framework\MessageQueue\Config\Consumer\ConfigReaderPlugin as ConsumerConfigReaderPlugin;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\MessageQueue\ConfigInterface;
use Magento\Framework\MessageQueue\Consumer\Config\CompositeReader as ConsumerConfigCompositeReader;

class ConfigReaderPluginTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ConsumerConfigReaderPlugin
     */
    private $plugin;

    /**
     * @var ObjectManagerHelper
     */
    private $objectManagerHelper;

    /**
     * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $configMock;

    /**
     * @var ConsumerConfigCompositeReader|\PHPUnit_Framework_MockObject_MockObject
     */
    private $subjectMock;

    protected function setUp()
    {
        $this->configMock = $this->getMockBuilder(ConfigInterface::class)
            ->getMockForAbstractClass();
        $this->subjectMock = $this->getMockBuilder(ConsumerConfigCompositeReader::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->objectManagerHelper = new ObjectManagerHelper($this);
        $this->plugin = $this->objectManagerHelper->getObject(
            ConsumerConfigReaderPlugin::class,
            ['config' => $this->configMock]
        );
    }

    public function testAfterRead()
    {
        $result = ['consumer0' => []];
        $consumers = [
            [
                'name' => 'consumer1',
                'handlers' => [
                    ['handlerConfig1_1_1', 'handlerConfig1_1_2'],
                    ['handlerConfig1_2_1']
                ],
                'queue' => ['item1_1', 'item1_2'],
                'instance_type' => 'type1',
                'connection' => 'connection1',
                'max_messages' => 100
            ],
            [
                'name' => 'consumer2',
                'handlers' => [],
                'queue' => ['item2_1'],
                'instance_type' => 'type2',
                'connection' => 'connection2',
                'max_messages' => 2
            ]
        ];
        $finalResult = [
            'consumer1' => [
                'name' => 'consumer1',
                'queue' => ['item1_1', 'item1_2'],
                'consumerInstance' => 'type1',
                'handlers' => ['handlerConfig1_1_1', 'handlerConfig1_1_2', 'handlerConfig1_2_1'],
                'connection' => 'connection1',
                'maxMessages' => 100
            ],
            'consumer2' => [
                'name' => 'consumer2',
                'queue' => ['item2_1'],
                'consumerInstance' => 'type2',
                'handlers' => [],
                'connection' => 'connection2',
                'maxMessages' => 2
            ],
            'consumer0' => []
        ];

        $this->configMock->expects(static::atLeastOnce())
            ->method('getConsumers')
            ->willReturn($consumers);

        $this->assertEquals($finalResult, $this->plugin->afterRead($this->subjectMock, $result));
    }
}