objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->encoder = $this->objectManager->create(MessageEncoder::class); $this->setBackwardCompatibleProperty( $this->encoder, 'communicationConfig', $this->getConfig() ); parent::setUp(); } /** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php */ public function testEncode() { /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */ $customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class); $fixtureCustomerId = 1; $customer = $customerRepository->getById($fixtureCustomerId); /** @var \Magento\Customer\Api\Data\CustomerExtensionInterface $customerExtension */ $customerExtension = $this->objectManager->create(\Magento\Customer\Api\Data\CustomerExtension::class); $customerExtension->setTestGroupCode('Some Group Code'); $customer->setExtensionAttributes($customerExtension); $encodedCustomerData = json_decode($this->encoder->encode('customer.created', $customer), true); $createdAt = $customer->getCreatedAt(); $updatedAt = $customer->getUpdatedAt(); $expectedEncodedCustomerData = json_decode($this->getCustomerDataAsJson($createdAt, $updatedAt), true); $this->assertEquals($expectedEncodedCustomerData, $encodedCustomerData); } /** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php */ public function testEncodeArrayOfEntities() { /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */ $customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class); $fixtureCustomerId = 1; $customer = $customerRepository->getById($fixtureCustomerId); /** @var \Magento\Customer\Api\Data\CustomerExtensionInterface $customerExtension */ $customerExtension = $this->objectManager->create(\Magento\Customer\Api\Data\CustomerExtension::class); $customerExtension->setTestGroupCode('Some Group Code'); $customer->setExtensionAttributes($customerExtension); $encodedCustomerData = json_decode($this->encoder->encode('customer.list.retrieved', [$customer]), true); $createdAt = $customer->getCreatedAt(); $updatedAt = $customer->getUpdatedAt(); $expectedEncodedCustomerData = json_decode($this->getCustomerDataAsJson($createdAt, $updatedAt), true); $this->assertEquals($expectedEncodedCustomerData, $encodedCustomerData[0]); } public function testDecode() { $encodedMessage = $this->getCustomerDataAsJson('2015-07-22 12:43:36', '2015-07-22 12:45:36'); /** @var \Magento\Customer\Api\Data\CustomerInterface $decodedCustomerObject */ $decodedCustomerObject = $this->encoder->decode('customer.created', $encodedMessage); $this->assertInstanceOf(\Magento\Customer\Api\Data\CustomerInterface::class, $decodedCustomerObject); $this->assertEquals('customer@example.com', $decodedCustomerObject->getEmail()); $this->assertEquals(1, $decodedCustomerObject->getGroupId()); $this->assertInstanceOf( \Magento\Customer\Api\Data\CustomerExtensionInterface::class, $decodedCustomerObject->getExtensionAttributes() ); $this->assertEquals('Some Group Code', $decodedCustomerObject->getExtensionAttributes()->getTestGroupCode()); $addresses = $decodedCustomerObject->getAddresses(); $this->assertCount(1, $addresses, "Address was not decoded."); $this->assertInstanceOf( \Magento\Customer\Api\Data\AddressInterface::class, $addresses[0] ); $this->assertEquals('3468676', $addresses[0]->getTelephone()); $this->assertEquals(true, $addresses[0]->isDefaultBilling()); $this->assertInstanceOf( \Magento\Customer\Api\Data\RegionInterface::class, $addresses[0]->getRegion() ); $this->assertEquals('AL', $addresses[0]->getRegion()->getRegionCode()); } /** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Error occurred during message decoding */ public function testDecodeInvalidMessageFormat() { $this->encoder->decode('customer.created', "{"); } /** * @expectedException \LogicException */ public function testDecodeInvalidMessage() { $message = 'Property "NotExistingField" does not have accessor method "getNotExistingField" in class ' . '"Magento\Customer\Api\Data\CustomerInterface".'; $this->expectExceptionMessage($message); $this->encoder->decode('customer.created', '{"not_existing_field": "value"}'); } /** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Error occurred during message decoding */ public function testDecodeIncorrectMessage() { $this->encoder->decode('customer.created', "{"); } /** * @return \Magento\Framework\MessageQueue\Config */ protected function getConfig() { $newData = include __DIR__ . '/_files/encoder_communication.php'; /** @var \Magento\Framework\Communication\Config\Data $configData */ $configData = $this->objectManager->create(\Magento\Framework\Communication\Config\Data::class); $configData->reset(); $configData->merge($newData); $config = $this->objectManager->create(Config::class, ['configData' => $configData]); return $config; } /** * Get fixture customer data in Json format * * @param string $createdAt * @param string $updatedAt * @return string */ protected function getCustomerDataAsJson($createdAt, $updatedAt) { return <<getProperty($propertyName); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $propertyValue); } }