ConsumerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Phrase;
  8. /**
  9. * Unit test for Consumer class.
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ConsumerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\MessageQueue\ConsumerConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $configuration;
  19. /**
  20. * @var \Magento\Framework\MessageQueue\MessageEncoder|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $messageEncoder;
  23. /**
  24. * @var \Magento\Framework\MessageQueue\QueueRepository|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $queueRepository;
  27. /**
  28. * @var \Magento\Framework\MessageQueue\CallbackInvoker
  29. */
  30. private $callbackInvoker;
  31. /**
  32. * @var \Magento\Framework\MessageQueue\Consumer\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $consumerConfig;
  35. /**
  36. * @var \Magento\Framework\MessageQueue\MessageController|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $messageController;
  39. /**
  40. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $resource;
  43. /**
  44. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $logger;
  47. /**
  48. * @var \Magento\Framework\Communication\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $communicationConfig;
  51. /**
  52. * @var \Magento\Framework\MessageQueue\Consumer
  53. */
  54. private $consumer;
  55. /**
  56. * Set up.
  57. *
  58. * @return void
  59. */
  60. protected function setUp()
  61. {
  62. $this->configuration = $this
  63. ->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerConfigurationInterface::class)
  64. ->disableOriginalConstructor()->getMock();
  65. $this->messageEncoder = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageEncoder::class)
  66. ->disableOriginalConstructor()->getMock();
  67. $this->queueRepository = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueRepository::class)
  68. ->disableOriginalConstructor()->getMock();
  69. $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  70. ->disableOriginalConstructor()->getMock();
  71. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  74. //Hard dependency used because CallbackInvoker invokes closure logic defined inside of Customer class.
  75. $this->callbackInvoker = new \Magento\Framework\MessageQueue\CallbackInvoker();
  76. $this->consumer = $objectManager->getObject(
  77. \Magento\Framework\MessageQueue\Consumer::class,
  78. [
  79. 'configuration' => $this->configuration,
  80. 'messageEncoder' => $this->messageEncoder,
  81. 'queueRepository' => $this->queueRepository,
  82. 'invoker' => $this->callbackInvoker,
  83. 'resource' => $this->resource,
  84. 'logger' => $this->logger
  85. ]
  86. );
  87. $this->consumerConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\Consumer\ConfigInterface::class)
  88. ->disableOriginalConstructor()->getMock();
  89. $objectManager->setBackwardCompatibleProperty(
  90. $this->consumer,
  91. 'consumerConfig',
  92. $this->consumerConfig
  93. );
  94. $this->messageController = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageController::class)
  95. ->disableOriginalConstructor()->getMock();
  96. $objectManager->setBackwardCompatibleProperty(
  97. $this->consumer,
  98. 'messageController',
  99. $this->messageController
  100. );
  101. $this->communicationConfig = $this
  102. ->createMock(\Magento\Framework\Communication\ConfigInterface::class);
  103. $objectManager->setBackwardCompatibleProperty(
  104. $this->consumer,
  105. 'communicationConfig',
  106. $this->communicationConfig
  107. );
  108. }
  109. /**
  110. * Test for process method with NotFoundException.
  111. *
  112. * @return void
  113. */
  114. public function testProcessWithNotFoundException()
  115. {
  116. $properties = ['topic_name' => 'topic.name'];
  117. $topicConfig = [];
  118. $numberOfMessages = 1;
  119. $consumerName = 'consumer.name';
  120. $exceptionPhrase = new Phrase('Exception successfully thrown');
  121. $queue = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueInterface::class)
  122. ->disableOriginalConstructor()->getMock();
  123. $this->configuration->expects($this->once())->method('getQueue')->willReturn($queue);
  124. $envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  125. ->disableOriginalConstructor()->getMock();
  126. $queue->expects($this->atLeastOnce())->method('dequeue')->willReturn($envelope);
  127. $envelope->expects($this->once())->method('getProperties')->willReturn($properties);
  128. $this->communicationConfig->expects($this->once())->method('getTopic')->with($properties['topic_name'])
  129. ->willReturn($topicConfig);
  130. $this->configuration->expects($this->once())->method('getConsumerName')->willReturn($consumerName);
  131. $this->messageController->expects($this->once())->method('lock')->with($envelope, $consumerName)
  132. ->willThrowException(
  133. new \Magento\Framework\Exception\NotFoundException(
  134. $exceptionPhrase
  135. )
  136. );
  137. $queue->expects($this->once())->method('acknowledge')->with($envelope);
  138. $this->logger->expects($this->once())->method('warning')->with($exceptionPhrase->render());
  139. $this->consumer->process($numberOfMessages);
  140. }
  141. }