ExchangeTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Amqp\Test\Unit\Bulk;
  7. /**
  8. * Unit test for Exchange model.
  9. */
  10. class ExchangeTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Amqp\Config|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $amqpConfig;
  16. /**
  17. * @var \Magento\Framework\Communication\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $communicationConfig;
  20. /**
  21. * @var \Magento\Framework\MessageQueue\Publisher\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $publisherConfig;
  24. /**
  25. * @var \Magento\Framework\Amqp\Exchange|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $exchange;
  28. /**
  29. * @var \Magento\Framework\Amqp\Bulk\Exchange
  30. */
  31. private $bulkExchange;
  32. /**
  33. * Set up.
  34. *
  35. * @return void
  36. */
  37. protected function setUp()
  38. {
  39. $this->amqpConfig = $this->getMockBuilder(\Magento\Framework\Amqp\Config::class)
  40. ->disableOriginalConstructor()->getMock();
  41. $this->communicationConfig = $this->getMockBuilder(\Magento\Framework\Communication\ConfigInterface::class)
  42. ->disableOriginalConstructor()->getMock();
  43. $this->publisherConfig = $this
  44. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\ConfigInterface::class)
  45. ->disableOriginalConstructor()->getMock();
  46. $this->exchange = $this->getMockBuilder(\Magento\Framework\Amqp\Exchange::class)
  47. ->disableOriginalConstructor()->getMock();
  48. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->bulkExchange = $objectManager->getObject(
  50. \Magento\Framework\Amqp\Bulk\Exchange::class,
  51. [
  52. 'amqpConfig' => $this->amqpConfig,
  53. 'communicationConfig' => $this->communicationConfig,
  54. 'publisherConfig' => $this->publisherConfig,
  55. 'exchange' => $this->exchange,
  56. ]
  57. );
  58. }
  59. /**
  60. * Test for enqueue method.
  61. *
  62. * @return void
  63. */
  64. public function testEnqueue()
  65. {
  66. $topicName = 'topic.name';
  67. $exchangeName = 'exchangeName';
  68. $envelopeBody = 'envelopeBody';
  69. $envelopeProperties = ['property_key_1' => 'property_value_1'];
  70. $topicData = [
  71. \Magento\Framework\Communication\ConfigInterface::TOPIC_IS_SYNCHRONOUS => false
  72. ];
  73. $this->communicationConfig->expects($this->once())
  74. ->method('getTopic')->with($topicName)->willReturn($topicData);
  75. $channel = $this->getMockBuilder(\AMQPChannel::class)
  76. ->setMethods(['batch_basic_publish', 'publish_batch'])
  77. ->disableOriginalConstructor()->getMock();
  78. $this->amqpConfig->expects($this->once())->method('getChannel')->willReturn($channel);
  79. $publisher = $this
  80. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItemInterface::class)
  81. ->disableOriginalConstructor()->getMock();
  82. $this->publisherConfig->expects($this->once())
  83. ->method('getPublisher')->with($topicName)->willReturn($publisher);
  84. $connection = $this
  85. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConnectionInterface::class)
  86. ->disableOriginalConstructor()->getMock();
  87. $publisher->expects($this->once())->method('getConnection')->with()->willReturn($connection);
  88. $connection->expects($this->once())->method('getExchange')->with()->willReturn($exchangeName);
  89. $envelope = $this
  90. ->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  91. ->disableOriginalConstructor()->getMock();
  92. $envelope->expects($this->once())->method('getBody')->willReturn($envelopeBody);
  93. $envelope->expects($this->once())->method('getProperties')->willReturn($envelopeProperties);
  94. $channel->expects($this->once())->method('batch_basic_publish')
  95. ->with($this->isInstanceOf(\PhpAmqpLib\Message\AMQPMessage::class), $exchangeName, $topicName);
  96. $channel->expects($this->once())->method('publish_batch');
  97. $this->assertNull($this->bulkExchange->enqueue($topicName, [$envelope]));
  98. }
  99. /**
  100. * Test for enqueue method with synchronous topic.
  101. *
  102. * @return void
  103. */
  104. public function testEnqueueWithSynchronousTopic()
  105. {
  106. $topicName = 'topic.name';
  107. $response = 'responseBody';
  108. $topicData = [
  109. \Magento\Framework\Communication\ConfigInterface::TOPIC_IS_SYNCHRONOUS => true
  110. ];
  111. $this->communicationConfig->expects($this->once())
  112. ->method('getTopic')->with($topicName)->willReturn($topicData);
  113. $envelope = $this
  114. ->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  115. ->disableOriginalConstructor()->getMock();
  116. $this->exchange->expects($this->once())->method('enqueue')->with($topicName, $envelope)->willReturn($response);
  117. $this->assertEquals([$response], $this->bulkExchange->enqueue($topicName, [$envelope]));
  118. }
  119. }