Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConsumerTest.php
Go to the documentation of this file.
1 <?php
8 
10 
16 class ConsumerTest extends \PHPUnit\Framework\TestCase
17 {
21  private $configuration;
22 
26  private $messageEncoder;
27 
31  private $queueRepository;
32 
36  private $callbackInvoker;
37 
41  private $consumerConfig;
42 
46  private $messageController;
47 
51  private $resource;
52 
56  private $logger;
57 
61  private $communicationConfig;
62 
66  private $consumer;
67 
73  protected function setUp()
74  {
75  $this->configuration = $this
76  ->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerConfigurationInterface::class)
77  ->disableOriginalConstructor()->getMock();
78  $this->messageEncoder = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageEncoder::class)
79  ->disableOriginalConstructor()->getMock();
80  $this->queueRepository = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueRepository::class)
81  ->disableOriginalConstructor()->getMock();
82  $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
83  ->disableOriginalConstructor()->getMock();
84  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
85  ->disableOriginalConstructor()->getMock();
86 
87  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
88  //Hard dependency used because CallbackInvoker invokes closure logic defined inside of Customer class.
89  $this->callbackInvoker = new \Magento\Framework\MessageQueue\CallbackInvoker();
90  $this->consumer = $objectManager->getObject(
91  \Magento\Framework\MessageQueue\Consumer::class,
92  [
93  'configuration' => $this->configuration,
94  'messageEncoder' => $this->messageEncoder,
95  'queueRepository' => $this->queueRepository,
96  'invoker' => $this->callbackInvoker,
97  'resource' => $this->resource,
98  'logger' => $this->logger
99  ]
100  );
101 
102  $this->consumerConfig = $this->getMockBuilder(\Magento\Framework\MessageQueue\Consumer\ConfigInterface::class)
103  ->disableOriginalConstructor()->getMock();
104  $objectManager->setBackwardCompatibleProperty(
105  $this->consumer,
106  'consumerConfig',
107  $this->consumerConfig
108  );
109  $this->messageController = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageController::class)
110  ->disableOriginalConstructor()->getMock();
111  $objectManager->setBackwardCompatibleProperty(
112  $this->consumer,
113  'messageController',
114  $this->messageController
115  );
116  $this->communicationConfig = $this
117  ->createMock(\Magento\Framework\Communication\ConfigInterface::class);
118  $objectManager->setBackwardCompatibleProperty(
119  $this->consumer,
120  'communicationConfig',
121  $this->communicationConfig
122  );
123  }
124 
131  {
132  $properties = ['topic_name' => 'topic.name'];
133  $topicConfig = [];
134  $numberOfMessages = 1;
135  $consumerName = 'consumer.name';
136  $exceptionPhrase = new Phrase('Exception successfully thrown');
137 
138  $queue = $this->getMockBuilder(\Magento\Framework\MessageQueue\QueueInterface::class)
139  ->disableOriginalConstructor()->getMock();
140  $this->configuration->expects($this->once())->method('getQueue')->willReturn($queue);
141  $envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
142  ->disableOriginalConstructor()->getMock();
143  $queue->expects($this->atLeastOnce())->method('dequeue')->willReturn($envelope);
144  $envelope->expects($this->once())->method('getProperties')->willReturn($properties);
145  $this->communicationConfig->expects($this->once())->method('getTopic')->with($properties['topic_name'])
146  ->willReturn($topicConfig);
147  $this->configuration->expects($this->once())->method('getConsumerName')->willReturn($consumerName);
148  $this->messageController->expects($this->once())->method('lock')->with($envelope, $consumerName)
149  ->willThrowException(
150  new \Magento\Framework\Exception\NotFoundException(
151  $exceptionPhrase
152  )
153  );
154  $queue->expects($this->once())->method('acknowledge')->with($envelope);
155  $this->logger->expects($this->once())->method('warning')->with($exceptionPhrase->render());
156 
157  $this->consumer->process($numberOfMessages);
158  }
159 }
$queue
Definition: queue.php:21
$objectManager
Definition: bootstrap.php:17
$properties
Definition: categories.php:26