MapperTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Test\Unit;
  7. class MapperTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\EntityManager\Mapper
  11. */
  12. private $mapper;
  13. public function setUp()
  14. {
  15. $config = [
  16. \Magento\Customer\Api\Data\CustomerInterface::class => ['entity_id' => 'id'],
  17. \Magento\Customer\Api\Data\AddressInterface::class => ['parent_id' => 'customer_id', 'invalid' => '']
  18. ];
  19. $this->mapper = new \Magento\Framework\EntityManager\Mapper($config);
  20. }
  21. public function testEntityToDatabase()
  22. {
  23. $inputData = [
  24. 'group_id' => 1,
  25. 'extension_attributes' => ['extension_attribute' => ['value' => 'some value']],
  26. 'id' => 123
  27. ];
  28. $expectedOutput = $inputData;
  29. $expectedOutput['entity_id'] = 123;
  30. unset($expectedOutput['id']);
  31. $actualOutput = $this->mapper->entityToDatabase(
  32. \Magento\Customer\Api\Data\CustomerInterface::class,
  33. $inputData
  34. );
  35. $this->assertEquals($expectedOutput, $actualOutput);
  36. }
  37. /**
  38. * @expectedException \LogicException
  39. * @expectedExceptionMessage Incorrect configuration for Magento\Customer\Api\Data\AddressInterface
  40. */
  41. public function testEntityToDatabaseException()
  42. {
  43. $inputData = [
  44. 'group_id' => 1,
  45. 'extension_attributes' => ['extension_attribute' => ['value' => 'some value']],
  46. ];
  47. $this->mapper->entityToDatabase(\Magento\Customer\Api\Data\AddressInterface::class, $inputData);
  48. }
  49. public function testDatabaseToEntity()
  50. {
  51. $inputData = [
  52. 'group_id' => 1,
  53. 'extension_attributes' => ['extension_attribute' => ['value' => 'some value']],
  54. 'entity_id' => 123
  55. ];
  56. $expectedOutput = $inputData;
  57. $expectedOutput['id'] = 123;
  58. unset($expectedOutput['entity_id']);
  59. $actualOutput = $this->mapper->databaseToEntity(
  60. \Magento\Customer\Api\Data\CustomerInterface::class,
  61. $inputData
  62. );
  63. $this->assertEquals($expectedOutput, $actualOutput);
  64. }
  65. /**
  66. * @expectedException \LogicException
  67. * @expectedExceptionMessage Incorrect configuration for Magento\Customer\Api\Data\AddressInterface
  68. */
  69. public function testDatabaseToEntityException()
  70. {
  71. $inputData = [
  72. 'group_id' => 1,
  73. 'extension_attributes' => ['extension_attribute' => ['value' => 'some value']],
  74. 'invalid' => 123
  75. ];
  76. $this->mapper->databaseToEntity(\Magento\Customer\Api\Data\AddressInterface::class, $inputData);
  77. }
  78. }