MessageEncoderTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\MessageQueue\MessageEncoder;
  9. use Magento\Framework\Communication\Config;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class MessageEncoderTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var MessageEncoder */
  16. protected $encoder;
  17. /** @var \Magento\Framework\ObjectManagerInterface */
  18. protected $objectManager;
  19. protected function setUp()
  20. {
  21. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  22. $this->encoder = $this->objectManager->create(MessageEncoder::class);
  23. $this->setBackwardCompatibleProperty(
  24. $this->encoder,
  25. 'communicationConfig',
  26. $this->getConfig()
  27. );
  28. parent::setUp();
  29. }
  30. /**
  31. * @magentoDataFixture Magento/Customer/_files/customer.php
  32. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  33. */
  34. public function testEncode()
  35. {
  36. /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
  37. $customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  38. $fixtureCustomerId = 1;
  39. $customer = $customerRepository->getById($fixtureCustomerId);
  40. /** @var \Magento\Customer\Api\Data\CustomerExtensionInterface $customerExtension */
  41. $customerExtension = $this->objectManager->create(\Magento\Customer\Api\Data\CustomerExtension::class);
  42. $customerExtension->setTestGroupCode('Some Group Code');
  43. $customer->setExtensionAttributes($customerExtension);
  44. $encodedCustomerData = json_decode($this->encoder->encode('customer.created', $customer), true);
  45. $createdAt = $customer->getCreatedAt();
  46. $updatedAt = $customer->getUpdatedAt();
  47. $expectedEncodedCustomerData = json_decode($this->getCustomerDataAsJson($createdAt, $updatedAt), true);
  48. $this->assertEquals($expectedEncodedCustomerData, $encodedCustomerData);
  49. }
  50. /**
  51. * @magentoDataFixture Magento/Customer/_files/customer.php
  52. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  53. */
  54. public function testEncodeArrayOfEntities()
  55. {
  56. /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
  57. $customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  58. $fixtureCustomerId = 1;
  59. $customer = $customerRepository->getById($fixtureCustomerId);
  60. /** @var \Magento\Customer\Api\Data\CustomerExtensionInterface $customerExtension */
  61. $customerExtension = $this->objectManager->create(\Magento\Customer\Api\Data\CustomerExtension::class);
  62. $customerExtension->setTestGroupCode('Some Group Code');
  63. $customer->setExtensionAttributes($customerExtension);
  64. $encodedCustomerData = json_decode($this->encoder->encode('customer.list.retrieved', [$customer]), true);
  65. $createdAt = $customer->getCreatedAt();
  66. $updatedAt = $customer->getUpdatedAt();
  67. $expectedEncodedCustomerData = json_decode($this->getCustomerDataAsJson($createdAt, $updatedAt), true);
  68. $this->assertEquals($expectedEncodedCustomerData, $encodedCustomerData[0]);
  69. }
  70. public function testDecode()
  71. {
  72. $encodedMessage = $this->getCustomerDataAsJson('2015-07-22 12:43:36', '2015-07-22 12:45:36');
  73. /** @var \Magento\Customer\Api\Data\CustomerInterface $decodedCustomerObject */
  74. $decodedCustomerObject = $this->encoder->decode('customer.created', $encodedMessage);
  75. $this->assertInstanceOf(\Magento\Customer\Api\Data\CustomerInterface::class, $decodedCustomerObject);
  76. $this->assertEquals('customer@example.com', $decodedCustomerObject->getEmail());
  77. $this->assertEquals(1, $decodedCustomerObject->getGroupId());
  78. $this->assertInstanceOf(
  79. \Magento\Customer\Api\Data\CustomerExtensionInterface::class,
  80. $decodedCustomerObject->getExtensionAttributes()
  81. );
  82. $this->assertEquals('Some Group Code', $decodedCustomerObject->getExtensionAttributes()->getTestGroupCode());
  83. $addresses = $decodedCustomerObject->getAddresses();
  84. $this->assertCount(1, $addresses, "Address was not decoded.");
  85. $this->assertInstanceOf(
  86. \Magento\Customer\Api\Data\AddressInterface::class,
  87. $addresses[0]
  88. );
  89. $this->assertEquals('3468676', $addresses[0]->getTelephone());
  90. $this->assertEquals(true, $addresses[0]->isDefaultBilling());
  91. $this->assertInstanceOf(
  92. \Magento\Customer\Api\Data\RegionInterface::class,
  93. $addresses[0]->getRegion()
  94. );
  95. $this->assertEquals('AL', $addresses[0]->getRegion()->getRegionCode());
  96. }
  97. /**
  98. * @expectedException \Magento\Framework\Exception\LocalizedException
  99. * @expectedExceptionMessage Error occurred during message decoding
  100. */
  101. public function testDecodeInvalidMessageFormat()
  102. {
  103. $this->encoder->decode('customer.created', "{");
  104. }
  105. /**
  106. * @expectedException \LogicException
  107. */
  108. public function testDecodeInvalidMessage()
  109. {
  110. $message = 'Property "NotExistingField" does not have accessor method "getNotExistingField" in class '
  111. . '"Magento\Customer\Api\Data\CustomerInterface".';
  112. $this->expectExceptionMessage($message);
  113. $this->encoder->decode('customer.created', '{"not_existing_field": "value"}');
  114. }
  115. /**
  116. * @expectedException \Magento\Framework\Exception\LocalizedException
  117. * @expectedExceptionMessage Error occurred during message decoding
  118. */
  119. public function testDecodeIncorrectMessage()
  120. {
  121. $this->encoder->decode('customer.created', "{");
  122. }
  123. /**
  124. * @return \Magento\Framework\MessageQueue\Config
  125. */
  126. protected function getConfig()
  127. {
  128. $newData = include __DIR__ . '/_files/encoder_communication.php';
  129. /** @var \Magento\Framework\Communication\Config\Data $configData */
  130. $configData = $this->objectManager->create(\Magento\Framework\Communication\Config\Data::class);
  131. $configData->reset();
  132. $configData->merge($newData);
  133. $config = $this->objectManager->create(Config::class, ['configData' => $configData]);
  134. return $config;
  135. }
  136. /**
  137. * Get fixture customer data in Json format
  138. *
  139. * @param string $createdAt
  140. * @param string $updatedAt
  141. * @return string
  142. */
  143. protected function getCustomerDataAsJson($createdAt, $updatedAt)
  144. {
  145. return <<<JSON
  146. {
  147. "id": 1,
  148. "group_id": 1,
  149. "default_billing": "1",
  150. "default_shipping": "1",
  151. "created_at": "{$createdAt}",
  152. "updated_at": "{$updatedAt}",
  153. "email": "customer@example.com",
  154. "firstname": "John",
  155. "lastname": "Smith",
  156. "middlename": "A",
  157. "prefix": "Mr.",
  158. "suffix": "Esq.",
  159. "gender": 0,
  160. "store_id": 1,
  161. "taxvat": "12",
  162. "website_id": 1,
  163. "addresses": [
  164. {
  165. "id": 1,
  166. "customer_id": 1,
  167. "region": {
  168. "region_code": "AL",
  169. "region": "Alabama",
  170. "region_id": 1
  171. },
  172. "region_id": 1,
  173. "country_id": "US",
  174. "street": [
  175. "Green str, 67"
  176. ],
  177. "company": "CompanyName",
  178. "telephone": "3468676",
  179. "postcode": "75477",
  180. "city": "CityM",
  181. "firstname": "John",
  182. "lastname": "Smith",
  183. "default_shipping": true,
  184. "default_billing": true
  185. }
  186. ],
  187. "disable_auto_group_change": 0,
  188. "extension_attributes": {
  189. "test_group_code": "Some Group Code"
  190. }
  191. }
  192. JSON;
  193. }
  194. /**
  195. * Set mocked property
  196. *
  197. * @param object $object
  198. * @param string $propertyName
  199. * @param object $propertyValue
  200. * @return void
  201. */
  202. public function setBackwardCompatibleProperty($object, $propertyName, $propertyValue)
  203. {
  204. $reflection = new \ReflectionClass(get_class($object));
  205. $reflectionProperty = $reflection->getProperty($propertyName);
  206. $reflectionProperty->setAccessible(true);
  207. $reflectionProperty->setValue($object, $propertyValue);
  208. }
  209. }