ConsumerFactoryTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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;
  7. use Magento\Framework\MessageQueue\ConsumerFactory;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
  10. use Magento\Framework\MessageQueue\Consumer\ConfigInterface as ConsumerConfig;
  11. use Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItem;
  12. class ConsumerFactoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ObjectManager
  16. */
  17. private $objectManager;
  18. /** @var CommunicationConfig|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $communicationConfigMock;
  20. /** @var ConsumerConfig|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $consumerConfigMock;
  22. const TEST_CONSUMER_NAME = "test_consumer_name";
  23. const TEST_CONSUMER_QUEUE = "test_consumer_queue";
  24. const TEST_CONSUMER_METHOD = "test_consumer_method";
  25. protected function setUp()
  26. {
  27. $this->objectManager = new ObjectManager($this);
  28. $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->consumerConfigMock = $this->getMockBuilder(ConsumerConfig::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. }
  35. /**
  36. * @expectedException \Magento\Framework\Exception\LocalizedException
  37. * @expectedExceptionMessage pecified consumer "test_consumer_name" is not declared.
  38. */
  39. public function testUndeclaredConsumerName()
  40. {
  41. $consumerFactory = $this->objectManager->getObject(ConsumerFactory::class);
  42. $this->objectManager->setBackwardCompatibleProperty(
  43. $consumerFactory,
  44. 'communicationConfig',
  45. $this->communicationConfigMock
  46. );
  47. $this->objectManager->setBackwardCompatibleProperty(
  48. $consumerFactory,
  49. 'consumerConfig',
  50. $this->consumerConfigMock
  51. );
  52. $consumerFactory->get(self::TEST_CONSUMER_NAME);
  53. }
  54. public function testConnectionInjectedForConsumer()
  55. {
  56. $consumerType = 'async';
  57. $consumerTypeValue = \Magento\Framework\MessageQueue\Model\TestConsumer::class;
  58. $consumers = [
  59. [
  60. 'type' => [$consumerType => $consumerTypeValue]
  61. ]
  62. ];
  63. $consumerFactory = $this->getConsumerFactoryInstance($consumers);
  64. $consumerInstanceMock = $this->getMockBuilder($consumerTypeValue)->getMock();
  65. $this->assertInstanceOf(get_class($consumerInstanceMock), $consumerFactory->get(self::TEST_CONSUMER_NAME));
  66. }
  67. /**
  68. * Return Consumer Factory with mocked objects
  69. *
  70. * @param array $consumers
  71. * @return ConsumerFactory
  72. */
  73. private function getConsumerFactoryInstance($consumers)
  74. {
  75. $consumerTypeValue = \Magento\Framework\MessageQueue\Model\TestConsumer::class;
  76. $handlerTypeValue = \Magento\Framework\DataObject::class;
  77. $consumerType = 'async';
  78. /** @var ConsumerConfigItem|\PHPUnit_Framework_MockObject_MockObject $consumerConfigItemMock */
  79. $consumerConfigItemMock = $this->getMockBuilder(ConsumerConfigItem::class)->disableOriginalConstructor()
  80. ->getMock();
  81. $consumerConfigItemMock->expects($this->any())->method('getName')->willReturn(self::TEST_CONSUMER_NAME);
  82. $consumerConfigItemMock->expects($this->any())->method('getQueue')->willReturn(self::TEST_CONSUMER_QUEUE);
  83. $consumerConfigItemMock->expects($this->any())->method('getConsumerInstance')->willReturn($consumerTypeValue);
  84. $consumerConfigItemMock->expects($this->any())->method('getHandlers')->willReturn([]);
  85. $this->consumerConfigMock->expects($this->any())
  86. ->method('getConsumer')
  87. ->with('test_consumer_name')
  88. ->willReturn($consumerConfigItemMock);
  89. $this->communicationConfigMock->expects($this->any())
  90. ->method('getTopics')
  91. ->willReturn(
  92. [
  93. [
  94. CommunicationConfig::TOPIC_NAME => 'topicName',
  95. CommunicationConfig::TOPIC_IS_SYNCHRONOUS => false
  96. ]
  97. ]
  98. );
  99. $this->communicationConfigMock->expects($this->any())
  100. ->method('getTopic')
  101. ->with('topicName')
  102. ->willReturn(
  103. [
  104. CommunicationConfig::TOPIC_HANDLERS => [
  105. [
  106. CommunicationConfig::HANDLER_TYPE => $handlerTypeValue,
  107. CommunicationConfig::HANDLER_METHOD => self::TEST_CONSUMER_METHOD
  108. ]
  109. ],
  110. ]
  111. );
  112. $consumerInstanceMock = $this->getMockBuilder($consumerTypeValue)->getMock();
  113. $consumerMock = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerInterface::class)
  114. ->setMethods(['configure'])
  115. ->getMockForAbstractClass();
  116. $consumerConfigurationMock =
  117. $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerConfigurationInterface::class)
  118. ->disableOriginalConstructor()
  119. ->getMockForAbstractClass();
  120. $consumerConfigurationMock->expects($this->any())->method('getType')->willReturn($consumerType);
  121. $objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  122. ->setMethods(['create'])
  123. ->getMockForAbstractClass();
  124. $objectManagerMock->expects($this->any())
  125. ->method('create')
  126. ->willReturnOnConsecutiveCalls($consumerMock, $consumerConfigurationMock, $consumerInstanceMock);
  127. $consumerFactory = $this->objectManager->getObject(
  128. \Magento\Framework\MessageQueue\ConsumerFactory::class,
  129. [
  130. 'objectManager' => $objectManagerMock,
  131. 'consumers' => $consumers
  132. ]
  133. );
  134. $this->objectManager->setBackwardCompatibleProperty(
  135. $consumerFactory,
  136. 'communicationConfig',
  137. $this->communicationConfigMock
  138. );
  139. $this->objectManager->setBackwardCompatibleProperty(
  140. $consumerFactory,
  141. 'consumerConfig',
  142. $this->consumerConfigMock
  143. );
  144. return $consumerFactory;
  145. }
  146. }