BatchConsumerTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  8. * Unit test for BatchConsumer class.
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class BatchConsumerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\MessageQueue\ConsumerConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $configuration;
  18. /**
  19. * @var \Magento\Framework\MessageQueue\MessageEncoder|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $messageEncoder;
  22. /**
  23. * @var \Magento\Framework\MessageQueue\QueueRepository|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $queueRepository;
  26. /**
  27. * @var \Magento\Framework\MessageQueue\MergerFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $mergerFactory;
  30. /**
  31. * @var \Magento\Framework\MessageQueue\Consumer\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $consumerConfig;
  34. /**
  35. * @var \Magento\Framework\MessageQueue\MessageController|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $messageController;
  38. /**
  39. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $resource;
  42. /**
  43. * @var \Magento\Framework\MessageQueue\BatchConsumer
  44. */
  45. private $batchConsumer;
  46. /**
  47. * @var int
  48. */
  49. private $batchSize = 10;
  50. /**
  51. * @var \Magento\Framework\MessageQueue\MessageProcessorLoader|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $messageProcessorLoader;
  54. /**
  55. * Set up.
  56. *
  57. * @return void
  58. */
  59. protected function setUp()
  60. {
  61. $this->configuration = $this
  62. ->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerConfigurationInterface::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->messageEncoder = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageEncoder::class)
  65. ->disableOriginalConstructor()->getMock();
  66. $this->queueRepository = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueRepository::class)
  67. ->disableOriginalConstructor()->getMock();
  68. $this->mergerFactory = $this->getMockBuilder(\Magento\Framework\MessageQueue\MergerFactory::class)
  69. ->setMethods(['create'])
  70. ->disableOriginalConstructor()->getMock();
  71. $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $this->messageProcessorLoader = $this
  74. ->getMockBuilder(\Magento\Framework\MessageQueue\MessageProcessorLoader::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  78. $this->batchConsumer = $objectManager->getObject(
  79. \Magento\Framework\MessageQueue\BatchConsumer::class,
  80. [
  81. 'configuration' => $this->configuration,
  82. 'messageEncoder' => $this->messageEncoder,
  83. 'queueRepository' => $this->queueRepository,
  84. 'mergerFactory' => $this->mergerFactory,
  85. 'resource' => $this->resource,
  86. 'batchSize' => $this->batchSize,
  87. 'messageProcessorLoader' => $this->messageProcessorLoader
  88. ]
  89. );
  90. $this->consumerConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\Consumer\ConfigInterface::class)
  91. ->disableOriginalConstructor()->getMock();
  92. $objectManager->setBackwardCompatibleProperty(
  93. $this->batchConsumer,
  94. 'consumerConfig',
  95. $this->consumerConfig
  96. );
  97. $this->messageController = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageController::class)
  98. ->disableOriginalConstructor()->getMock();
  99. $objectManager->setBackwardCompatibleProperty(
  100. $this->batchConsumer,
  101. 'messageController',
  102. $this->messageController
  103. );
  104. }
  105. /**
  106. * Test for process().
  107. *
  108. * @return void
  109. */
  110. public function testProcess()
  111. {
  112. $queueName = 'queue.name';
  113. $consumerName = 'consumerName';
  114. $connectionName = 'connection_name';
  115. $topicName = 'topicName';
  116. $messageBody = 'messageBody';
  117. $message = ['message_data'];
  118. $numberOfMessages = 2;
  119. $this->configuration->expects($this->once())->method('getQueueName')->willReturn($queueName);
  120. $this->configuration->expects($this->atLeastOnce())->method('getConsumerName')->willReturn($consumerName);
  121. $consumerConfigItem = $this
  122. ->getMockBuilder(\Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItemInterface::class)
  123. ->disableOriginalConstructor()->getMock();
  124. $this->consumerConfig->expects($this->once())
  125. ->method('getConsumer')->with($consumerName)->willReturn($consumerConfigItem);
  126. $consumerConfigItem->expects($this->once())->method('getConnection')->willReturn($connectionName);
  127. $queue = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueInterface::class)
  128. ->disableOriginalConstructor()->getMock();
  129. $this->queueRepository->expects($this->once())
  130. ->method('get')->with($connectionName, $queueName)->willReturn($queue);
  131. $merger = $this->getMockBuilder(\Magento\Framework\MessageQueue\MergerInterface::class)
  132. ->disableOriginalConstructor()->getMock();
  133. $this->mergerFactory->expects($this->once())->method('create')->with($consumerName)->willReturn($merger);
  134. $envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  135. ->disableOriginalConstructor()->getMock();
  136. $queue->expects($this->exactly($numberOfMessages))->method('dequeue')->willReturn($envelope);
  137. $this->messageController->expects($this->exactly($numberOfMessages))
  138. ->method('lock')->with($envelope, $consumerName);
  139. $envelope->expects($this->exactly($numberOfMessages))
  140. ->method('getProperties')->willReturn(['topic_name' => $topicName]);
  141. $envelope->expects($this->exactly($numberOfMessages))
  142. ->method('getBody')->willReturn($messageBody);
  143. $this->messageEncoder->expects($this->exactly($numberOfMessages))
  144. ->method('decode')->with($topicName, $messageBody)->willReturn($message);
  145. $messageProcessor = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageProcessorInterface::class)
  146. ->disableOriginalConstructor()
  147. ->getMockForAbstractClass();
  148. $this->messageProcessorLoader->expects($this->atLeastOnce())->method('load')->willReturn($messageProcessor);
  149. $merger->expects($this->once())->method('merge')
  150. ->with([$topicName => [$message, $message]])->willReturnArgument(0);
  151. $this->batchConsumer->process($numberOfMessages);
  152. }
  153. /**
  154. * Test for process() with MessageLockException.
  155. *
  156. * @return void
  157. */
  158. public function testProcessWithMessageLockException()
  159. {
  160. $queueName = 'queue.name';
  161. $consumerName = 'consumerName';
  162. $connectionName = 'connection_name';
  163. $numberOfMessages = 2;
  164. $this->configuration->expects($this->once())->method('getQueueName')->willReturn($queueName);
  165. $this->configuration->expects($this->atLeastOnce())->method('getConsumerName')->willReturn($consumerName);
  166. $consumerConfigItem = $this
  167. ->getMockBuilder(\Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItemInterface::class)
  168. ->disableOriginalConstructor()->getMock();
  169. $this->consumerConfig->expects($this->once())
  170. ->method('getConsumer')->with($consumerName)->willReturn($consumerConfigItem);
  171. $consumerConfigItem->expects($this->once())->method('getConnection')->willReturn($connectionName);
  172. $queue = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueInterface::class)
  173. ->disableOriginalConstructor()->getMock();
  174. $this->queueRepository->expects($this->once())
  175. ->method('get')->with($connectionName, $queueName)->willReturn($queue);
  176. $merger = $this->getMockBuilder(\Magento\Framework\MessageQueue\MergerInterface::class)
  177. ->disableOriginalConstructor()->getMock();
  178. $this->mergerFactory->expects($this->once())->method('create')->with($consumerName)->willReturn($merger);
  179. $envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  180. ->disableOriginalConstructor()->getMock();
  181. $queue->expects($this->exactly($numberOfMessages))->method('dequeue')->willReturn($envelope);
  182. $exception = new \Magento\Framework\MessageQueue\MessageLockException(__('Exception Message'));
  183. $this->messageController->expects($this->atLeastOnce())
  184. ->method('lock')->with($envelope, $consumerName)->willThrowException($exception);
  185. $messageProcessor = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageProcessorInterface::class)
  186. ->disableOriginalConstructor()
  187. ->getMockForAbstractClass();
  188. $this->messageProcessorLoader->expects($this->atLeastOnce())->method('load')->willReturn($messageProcessor);
  189. $merger->expects($this->once())->method('merge')->willReturn([]);
  190. $this->batchConsumer->process($numberOfMessages);
  191. }
  192. }