CompositeReaderTest.php 3.96 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 101 102 103 104 105 106 107 108 109 110 111 112 113
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\MessageQueue\Test\Unit\Publisher\Config;

use Magento\Framework\MessageQueue\Publisher\Config\CompositeReader;
use Magento\Framework\MessageQueue\Publisher\Config\ValidatorInterface;
use Magento\Framework\MessageQueue\Publisher\Config\ReaderInterface;

class CompositeReaderTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var CompositeReader
     */
    private $reader;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    private $validatorMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    private $readerOneMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    private $readerTwoMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    private $readerThreeMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $defaultConfigProviderMock;

    /**
     * Initialize parameters
     */
    protected function setUp()
    {
        $this->validatorMock = $this->createMock(ValidatorInterface::class);
        $this->readerOneMock = $this->createMock(ReaderInterface::class);
        $this->readerTwoMock = $this->createMock(ReaderInterface::class);
        $this->readerThreeMock = $this->createMock(ReaderInterface::class);
        $this->defaultConfigProviderMock =
            $this->createMock(\Magento\Framework\MessageQueue\DefaultValueProvider::class);

        $this->reader = new CompositeReader(
            $this->validatorMock,
            $this->defaultConfigProviderMock,
            [
                'readerOne' => $this->readerOneMock,
                'readerThree' => $this->readerThreeMock,
                'readerTwo' => $this->readerTwoMock,
            ]
        );
    }

    public function testRead()
    {
        $this->defaultConfigProviderMock->expects($this->any())->method('getConnection')->willReturn('amqp');
        $this->defaultConfigProviderMock->expects($this->any())->method('getExchange')->willReturn('magento');

        $dataOne = include __DIR__ . '/../../_files/queue_publisher/reader_one.php';
        $dataTwo = include __DIR__ . '/../../_files/queue_publisher/reader_two.php';
        $expectedValidationData = include __DIR__ . '/../../_files/queue_publisher/data_to_validate.php';

        $this->readerOneMock->expects($this->once())->method('read')->with(null)->willReturn($dataOne);
        $this->readerTwoMock->expects($this->once())->method('read')->with(null)->willReturn($dataTwo);
        $this->readerThreeMock->expects($this->once())->method('read')->with(null)->willReturn([]);

        $this->validatorMock->expects($this->once())->method('validate')->with($expectedValidationData);

        $data = $this->reader->read();

        $expectedData = [
            //disabling existing connection and adding new
            'top04' => [
                'topic' => 'top04',
                'disabled' => false,
                'connection' => ['name' => 'db', 'disabled' => false, 'exchange' => 'magento2'],
            ],
            //two disabled connections are ignored
            'top05' => [
                'topic' => 'top05',
                'disabled' => false,
                'connection' => ['name' => 'amqp', 'exchange' => 'exch01', 'disabled' => false],
            ],
            //added default connection if not declared
            'top06' => [
                'topic' => 'top06',
                'disabled' => false,
                'connection' => ['name' => 'amqp', 'exchange' => 'magento', 'disabled' => false],
            ],
            //added default connection if all declared connections are disabled
            'top07' => [
                'topic' => 'top07',
                'disabled' => false,
                'connection' => ['name' => 'amqp', 'exchange' => 'magento', 'disabled' => false],
            ],
        ];

        $this->assertEquals($expectedData, $data);
    }
}