PublisherTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Bulk\Rpc;
  7. /**
  8. * Unit test for Publisher.
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class PublisherTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\MessageQueue\Bulk\ExchangeRepository|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $exchangeRepository;
  18. /**
  19. * @var \Magento\Framework\MessageQueue\EnvelopeFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $envelopeFactory;
  22. /**
  23. * @var \Magento\Framework\MessageQueue\MessageEncoder|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $messageEncoder;
  26. /**
  27. * @var \Magento\Framework\MessageQueue\MessageValidator|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $messageValidator;
  30. /**
  31. * @var \Magento\Framework\MessageQueue\Publisher\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $publisherConfig;
  34. /**
  35. * @var \Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $responseQueueNameBuilder;
  38. /**
  39. * @var \Magento\Framework\MessageQueue\MessageIdGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $messageIdGenerator;
  42. /**
  43. * @var \Magento\Framework\MessageQueue\Bulk\Rpc\Publisher
  44. */
  45. private $publisher;
  46. /**
  47. * Set up.
  48. *
  49. * @return void
  50. */
  51. protected function setUp()
  52. {
  53. $this->exchangeRepository = $this
  54. ->getMockBuilder(\Magento\Framework\MessageQueue\Bulk\ExchangeRepository::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $this->envelopeFactory = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeFactory::class)
  57. ->setMethods(['create'])
  58. ->disableOriginalConstructor()->getMock();
  59. $this->messageEncoder = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageEncoder::class)
  60. ->disableOriginalConstructor()->getMock();
  61. $this->messageValidator = $this->getMockBuilder(\Magento\Framework\MessageQueue\MessageValidator::class)
  62. ->disableOriginalConstructor()->getMock();
  63. $this->publisherConfig = $this
  64. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\ConfigInterface::class)
  65. ->disableOriginalConstructor()->getMock();
  66. $this->responseQueueNameBuilder = $this
  67. ->getMockBuilder(\Magento\Framework\MessageQueue\Rpc\ResponseQueueNameBuilder::class)
  68. ->disableOriginalConstructor()->getMock();
  69. $this->messageIdGenerator = $this
  70. ->getMockBuilder(\Magento\Framework\MessageQueue\MessageIdGeneratorInterface::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  73. $this->publisher = $objectManager->getObject(
  74. \Magento\Framework\MessageQueue\Bulk\Rpc\Publisher::class,
  75. [
  76. 'exchangeRepository' => $this->exchangeRepository,
  77. 'envelopeFactory' => $this->envelopeFactory,
  78. 'messageEncoder' => $this->messageEncoder,
  79. 'messageValidator' => $this->messageValidator,
  80. 'publisherConfig' => $this->publisherConfig,
  81. 'responseQueueNameBuilder' => $this->responseQueueNameBuilder,
  82. 'messageIdGenerator' => $this->messageIdGenerator,
  83. ]
  84. );
  85. }
  86. /**
  87. * Test for publish method.
  88. *
  89. * @return void
  90. */
  91. public function testPublish()
  92. {
  93. $messageId = 'message-id-001';
  94. $topicName = 'topic.name';
  95. $message = 'messageBody';
  96. $encodedMessage = 'encodedMessageBody';
  97. $connectionName = 'amqp';
  98. $queueName = 'queueName';
  99. $this->responseQueueNameBuilder->expects($this->once())
  100. ->method('getQueueName')->with($topicName)->willReturn($queueName);
  101. $this->messageValidator->expects($this->once())->method('validate')->with($topicName, $message);
  102. $this->messageEncoder->expects($this->once())
  103. ->method('encode')->with($topicName, $message)->willReturn($encodedMessage);
  104. $envelope = $this->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
  105. ->disableOriginalConstructor()->getMock();
  106. $this->messageIdGenerator->expects($this->once())
  107. ->method('generate')->with($topicName)->willReturn($messageId);
  108. $this->envelopeFactory->expects($this->once())->method('create')->with(
  109. $this->logicalAnd(
  110. $this->arrayHasKey('body'),
  111. $this->arrayHasKey('properties'),
  112. $this->contains($encodedMessage)
  113. )
  114. )->willReturn($envelope);
  115. $publisher = $this
  116. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItemInterface::class)
  117. ->disableOriginalConstructor()->getMock();
  118. $this->publisherConfig->expects($this->once())
  119. ->method('getPublisher')->with($topicName)->willReturn($publisher);
  120. $connection = $this
  121. ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConnectionInterface::class)
  122. ->disableOriginalConstructor()->getMock();
  123. $publisher->expects($this->once())->method('getConnection')->with()->willReturn($connection);
  124. $connection->expects($this->once())->method('getName')->with()->willReturn($connectionName);
  125. $exchange = $this
  126. ->getMockBuilder(\Magento\Framework\Amqp\Bulk\Exchange::class)
  127. ->disableOriginalConstructor()->getMock();
  128. $this->exchangeRepository->expects($this->once())
  129. ->method('getByConnectionName')->with($connectionName)->willReturn($exchange);
  130. $exchange->expects($this->once())->method('enqueue')->with($topicName, [$envelope])->willReturn(null);
  131. $this->assertNull($this->publisher->publish($topicName, [$message]));
  132. }
  133. }