ExchangeFactoryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Test\Unit\Bulk;
  7. /**
  8. * Unit test for ExchangeFactory.
  9. */
  10. class ExchangeFactoryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\MessageQueue\ConnectionTypeResolver|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $connectionTypeResolver;
  16. /**
  17. * @var \Magento\Framework\MessageQueue\Bulk\ExchangeInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $amqpExchangeFactory;
  20. /**
  21. * @var \Magento\Framework\MessageQueue\Bulk\ExchangeFactory
  22. */
  23. private $exchangeFactory;
  24. /**
  25. * Set up.
  26. *
  27. * @return void
  28. */
  29. protected function setUp()
  30. {
  31. $this->connectionTypeResolver = $this
  32. ->getMockBuilder(\Magento\Framework\MessageQueue\ConnectionTypeResolver::class)
  33. ->disableOriginalConstructor()->getMock();
  34. $this->amqpExchangeFactory = $this
  35. ->getMockBuilder(\Magento\Framework\Amqp\ExchangeFactory::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  39. $this->exchangeFactory = $objectManager->getObject(
  40. \Magento\Framework\MessageQueue\Bulk\ExchangeFactory::class,
  41. [
  42. 'connectionTypeResolver' => $this->connectionTypeResolver,
  43. 'exchangeFactories' => ['amqp' => $this->amqpExchangeFactory],
  44. ]
  45. );
  46. }
  47. /**
  48. * Test for create method.
  49. *
  50. * @return void
  51. */
  52. public function testCreate()
  53. {
  54. $connectionName = 'amqp';
  55. $data = ['key1' => 'value1'];
  56. $this->connectionTypeResolver->expects($this->once())
  57. ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
  58. $exchange = $this
  59. ->getMockBuilder(\Magento\Framework\Amqp\Bulk\Exchange::class)
  60. ->disableOriginalConstructor()->getMock();
  61. $this->amqpExchangeFactory->expects($this->once())
  62. ->method('create')->with($connectionName, $data)->willReturn($exchange);
  63. $this->assertEquals($exchange, $this->exchangeFactory->create($connectionName, $data));
  64. }
  65. /**
  66. * Test for create method with undefined connection type.
  67. *
  68. * @return void
  69. * @expectedException \LogicException
  70. * @expectedExceptionMessage Not found exchange for connection name 'db' in config
  71. */
  72. public function testCreateWithUndefinedConnectionType()
  73. {
  74. $connectionName = 'db';
  75. $data = ['key1' => 'value1'];
  76. $this->connectionTypeResolver->expects($this->once())
  77. ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
  78. $this->amqpExchangeFactory->expects($this->never())->method('create');
  79. $this->exchangeFactory->create($connectionName, $data);
  80. }
  81. /**
  82. * Test for create method with wrong exchange type.
  83. *
  84. * @return void
  85. * @expectedException \LogicException
  86. * @expectedExceptionMessage Exchange for connection name 'amqp' does not implement interface
  87. */
  88. public function testCreateWithWrongExchangeType()
  89. {
  90. $connectionName = 'amqp';
  91. $data = ['key1' => 'value1'];
  92. $this->connectionTypeResolver->expects($this->once())
  93. ->method('getConnectionType')->with($connectionName)->willReturn($connectionName);
  94. $exchange = $this
  95. ->getMockBuilder(\Magento\Framework\Amqp\Exchange::class)
  96. ->disableOriginalConstructor()->getMock();
  97. $this->amqpExchangeFactory->expects($this->once())
  98. ->method('create')->with($connectionName, $data)->willReturn($exchange);
  99. $this->exchangeFactory->create($connectionName, $data);
  100. }
  101. }