MessageEncoderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Communication\ConfigInterface as CommunicationConfig;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\MessageQueue\MessageEncoder;
  10. /**
  11. * Test class for Magento\Framework\MessageQueue\MessageEncoder
  12. */
  13. class MessageEncoderTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var MessageEncoder */
  16. protected $encoder;
  17. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  18. protected $objectManager;
  19. /** @var CommunicationConfig|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $communicationConfigMock;
  21. /** @var \Magento\Framework\Webapi\ServiceOutputProcessor|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $dataObjectEncoderMock;
  23. protected function setUp()
  24. {
  25. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  26. $this->dataObjectEncoderMock = $this->getMockBuilder(\Magento\Framework\Webapi\ServiceOutputProcessor::class)
  27. ->disableOriginalConstructor()
  28. ->setMethods([])
  29. ->getMock();
  30. $this->encoder = $this->objectManager->getObject(
  31. MessageEncoder::class,
  32. ['dataObjectEncoder' => $this->dataObjectEncoderMock]
  33. );
  34. $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->objectManager->setBackwardCompatibleProperty(
  38. $this->encoder,
  39. 'communicationConfig',
  40. $this->communicationConfigMock
  41. );
  42. parent::setUp();
  43. }
  44. /**
  45. * @expectedException \Magento\Framework\Exception\LocalizedException
  46. * @expectedExceptionMessage Specified topic "customer.created" is not declared.
  47. */
  48. public function testEncodeInvalidTopic()
  49. {
  50. $this->encoder->encode('customer.created', 'Some message');
  51. }
  52. /**
  53. * @expectedException \Magento\Framework\Exception\LocalizedException
  54. * @expectedExceptionMessage Specified topic "customer.created" is not declared.
  55. */
  56. public function testDecodeInvalidTopic()
  57. {
  58. $this->encoder->decode('customer.created', 'Some message');
  59. }
  60. /**
  61. * @expectedException \Magento\Framework\Exception\LocalizedException
  62. * @expectedExceptionMessage Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data
  63. */
  64. public function testEncodeInvalidMessage()
  65. {
  66. $exceptionMessage = 'Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data"';
  67. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  68. $this->getQueueConfigData()
  69. );
  70. $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods([])
  73. ->getMock();
  74. $this->dataObjectEncoderMock
  75. ->expects($this->once())
  76. ->method('convertValue')
  77. ->willThrowException(new LocalizedException(__($exceptionMessage)));
  78. $this->encoder->encode('customer.created', $object);
  79. }
  80. /**
  81. * @expectedException \Magento\Framework\Exception\LocalizedException
  82. * @expectedExceptionMessage Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data
  83. */
  84. public function testEncodeInvalidMessageArray()
  85. {
  86. $exceptionMessage = 'Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data"';
  87. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  88. $this->getQueueConfigData()
  89. );
  90. $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  91. ->disableOriginalConstructor()
  92. ->setMethods([])
  93. ->getMock();
  94. $this->dataObjectEncoderMock
  95. ->expects($this->once())
  96. ->method('convertValue')
  97. ->willThrowException(new LocalizedException(__($exceptionMessage)));
  98. $this->encoder->encode('customer.created', [$object]);
  99. }
  100. /**
  101. * Data provider for queue config
  102. *
  103. * @return array
  104. */
  105. private function getQueueConfigData()
  106. {
  107. return [
  108. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  109. CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
  110. ];
  111. }
  112. }