ExchangeFactoryTest.php 3.83 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\MessageQueue\Test\Unit\Bulk;

/**
 * Unit test for ExchangeFactory.
 */
class ExchangeFactoryTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Framework\MessageQueue\ConnectionTypeResolver|\PHPUnit_Framework_MockObject_MockObject
     */
    private $connectionTypeResolver;

    /**
     * @var \Magento\Framework\MessageQueue\Bulk\ExchangeInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $amqpExchangeFactory;

    /**
     * @var \Magento\Framework\MessageQueue\Bulk\ExchangeFactory
     */
    private $exchangeFactory;

    /**
     * Set up.
     *
     * @return void
     */
    protected function setUp()
    {
        $this->connectionTypeResolver = $this
            ->getMockBuilder(\Magento\Framework\MessageQueue\ConnectionTypeResolver::class)
            ->disableOriginalConstructor()->getMock();

        $this->amqpExchangeFactory = $this
            ->getMockBuilder(\Magento\Framework\Amqp\ExchangeFactory::class)
            ->disableOriginalConstructor()
            ->getMock();

        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->exchangeFactory = $objectManager->getObject(
            \Magento\Framework\MessageQueue\Bulk\ExchangeFactory::class,
            [
                'connectionTypeResolver' => $this->connectionTypeResolver,
                'exchangeFactories' => ['amqp' => $this->amqpExchangeFactory],
            ]
        );
    }

    /**
     * Test for create method.
     *
     * @return void
     */
    public function testCreate()
    {
        $connectionName = 'amqp';
        $data = ['key1' => 'value1'];
        $this->connectionTypeResolver->expects($this->once())
            ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
        $exchange = $this
            ->getMockBuilder(\Magento\Framework\Amqp\Bulk\Exchange::class)
            ->disableOriginalConstructor()->getMock();
        $this->amqpExchangeFactory->expects($this->once())
            ->method('create')->with($connectionName, $data)->willReturn($exchange);
        $this->assertEquals($exchange, $this->exchangeFactory->create($connectionName, $data));
    }

    /**
     * Test for create method with undefined connection type.
     *
     * @return void
     * @expectedException \LogicException
     * @expectedExceptionMessage Not found exchange for connection name 'db' in config
     */
    public function testCreateWithUndefinedConnectionType()
    {
        $connectionName = 'db';
        $data = ['key1' => 'value1'];
        $this->connectionTypeResolver->expects($this->once())
            ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
        $this->amqpExchangeFactory->expects($this->never())->method('create');
        $this->exchangeFactory->create($connectionName, $data);
    }

    /**
     * Test for create method with wrong exchange type.
     *
     * @return void
     * @expectedException \LogicException
     * @expectedExceptionMessage Exchange for connection name 'amqp' does not implement interface
     */
    public function testCreateWithWrongExchangeType()
    {
        $connectionName = 'amqp';
        $data = ['key1' => 'value1'];
        $this->connectionTypeResolver->expects($this->once())
            ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
        $exchange = $this
            ->getMockBuilder(\Magento\Framework\Amqp\Exchange::class)
            ->disableOriginalConstructor()->getMock();
        $this->amqpExchangeFactory->expects($this->once())
            ->method('create')->with($connectionName, $data)->willReturn($exchange);
        $this->exchangeFactory->create($connectionName, $data);
    }
}