Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessageEncoderTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
12 
16 class MessageEncoderTest extends \PHPUnit\Framework\TestCase
17 {
19  protected $encoder;
20 
22  protected $objectManager;
23 
26 
29 
30  protected function setUp()
31  {
32  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33 
34  $this->dataObjectEncoderMock = $this->getMockBuilder(\Magento\Framework\Webapi\ServiceOutputProcessor::class)
35  ->disableOriginalConstructor()
36  ->setMethods([])
37  ->getMock();
38  $this->encoder = $this->objectManager->getObject(
39  MessageEncoder::class,
40  ['dataObjectEncoder' => $this->dataObjectEncoderMock]
41  );
42  $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $this->objectManager->setBackwardCompatibleProperty(
46  $this->encoder,
47  'communicationConfig',
48  $this->communicationConfigMock
49  );
50  parent::setUp();
51  }
52 
57  public function testEncodeInvalidTopic()
58  {
59  $this->encoder->encode('customer.created', 'Some message');
60  }
61 
66  public function testDecodeInvalidTopic()
67  {
68  $this->encoder->decode('customer.created', 'Some message');
69  }
70 
75  public function testEncodeInvalidMessage()
76  {
77  $exceptionMessage = 'Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data"';
78  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
79  $this->getQueueConfigData()
80  );
81  $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
82  ->disableOriginalConstructor()
83  ->setMethods([])
84  ->getMock();
85  $this->dataObjectEncoderMock
86  ->expects($this->once())
87  ->method('convertValue')
88  ->willThrowException(new LocalizedException(__($exceptionMessage)));
89 
90  $this->encoder->encode('customer.created', $object);
91  }
92 
98  {
99  $exceptionMessage = 'Message with topic "customer.created" must be an instance of "Magento\Customer\Api\Data"';
100  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
101  $this->getQueueConfigData()
102  );
103  $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
104  ->disableOriginalConstructor()
105  ->setMethods([])
106  ->getMock();
107  $this->dataObjectEncoderMock
108  ->expects($this->once())
109  ->method('convertValue')
110  ->willThrowException(new LocalizedException(__($exceptionMessage)));
111 
112  $this->encoder->encode('customer.created', [$object]);
113  }
114 
120  private function getQueueConfigData()
121  {
122  return [
123  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
124  CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
125  ];
126  }
127 }
__()
Definition: __.php:13