CustomAttributesMapperTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model;
  7. /**
  8. * Class CustomAttributesMapperTest
  9. */
  10. class CustomAttributesMapperTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. private $objectManager;
  16. public function setUp()
  17. {
  18. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  19. }
  20. public function testEntityToDatabase()
  21. {
  22. $searchResult = $this->getMockBuilder(\Magento\Framework\Api\SearchResults::class)
  23. ->disableOriginalConstructor()
  24. ->setMethods(['getItems'])
  25. ->getMock();
  26. $searchResult->expects($this->any())
  27. ->method('getItems')
  28. ->will($this->returnValue($this->getAttributes()));
  29. $attributeRepository = $this->getMockBuilder(\Magento\Eav\Model\AttributeRepository::class)
  30. ->disableOriginalConstructor()
  31. ->setMethods(['getList'])
  32. ->getMock();
  33. $attributeRepository->expects($this->any())
  34. ->method('getList')
  35. ->will($this->returnValue($searchResult));
  36. $metadata = $this->objectManager->getObject(
  37. \Magento\Framework\EntityManager\EntityMetadata::class,
  38. [
  39. 'entityTableName' => 'test',
  40. 'identifierField' => 'entity_id',
  41. 'eavEntityType' => 'customer_address'
  42. ]
  43. );
  44. $metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods(['getMetadata', 'hasConfiguration'])
  47. ->getMock();
  48. $metadataPool->expects($this->any())
  49. ->method('hasConfiguration')
  50. ->willReturn(true);
  51. $metadataPool->expects($this->any())
  52. ->method('getMetadata')
  53. ->with($this->equalTo(\Magento\Framework\Api\CustomAttributesDataInterface::class))
  54. ->will($this->returnValue($metadata));
  55. $metadataPool->expects($this->once())
  56. ->method('hasConfiguration')
  57. ->willReturn(true);
  58. $searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['addFilter', 'create'])
  61. ->getMock();
  62. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $searchCriteriaBuilder->expects($this->any())
  66. ->method('addFilter')
  67. ->will($this->returnValue($searchCriteriaBuilder));
  68. $searchCriteriaBuilder->expects($this->any())
  69. ->method('create')
  70. ->will($this->returnValue($searchCriteria));
  71. /** @var \Magento\Eav\Model\CustomAttributesMapper $customAttributesMapper */
  72. $customAttributesMapper = $this->objectManager
  73. ->getObject(\Magento\Eav\Model\CustomAttributesMapper::class, [
  74. 'attributeRepository' => $attributeRepository,
  75. 'metadataPool' => $metadataPool,
  76. 'searchCriteriaBuilder' => $searchCriteriaBuilder
  77. ]);
  78. $actual = $customAttributesMapper->entityToDatabase(
  79. \Magento\Framework\Api\CustomAttributesDataInterface::class,
  80. [
  81. \Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => [
  82. 'test' => [
  83. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test',
  84. \Magento\Framework\Api\AttributeInterface::VALUE => 'test'
  85. ],
  86. 'test1' => [
  87. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test4',
  88. \Magento\Framework\Api\AttributeInterface::VALUE => 'test4'
  89. ],
  90. 'test2' => [
  91. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test2',
  92. \Magento\Framework\Api\AttributeInterface::VALUE => 'test2'
  93. ]
  94. ]
  95. ]
  96. );
  97. $expected = [
  98. \Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => [
  99. 'test1' => [
  100. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test4',
  101. \Magento\Framework\Api\AttributeInterface::VALUE => 'test4'
  102. ],
  103. 'test2' => [
  104. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test2',
  105. \Magento\Framework\Api\AttributeInterface::VALUE => 'test2'
  106. ],
  107. ],
  108. 'test' => 'test'
  109. ];
  110. $this->assertEquals($expected, $actual);
  111. }
  112. public function testDatabaseToEntity()
  113. {
  114. $searchResult = $this->getMockBuilder(\Magento\Framework\Api\SearchResults::class)
  115. ->disableOriginalConstructor()
  116. ->setMethods(['getItems'])
  117. ->getMock();
  118. $searchResult->expects($this->any())
  119. ->method('getItems')
  120. ->will($this->returnValue($this->getAttributes()));
  121. $attributeRepository = $this->getMockBuilder(\Magento\Eav\Model\AttributeRepository::class)
  122. ->disableOriginalConstructor()
  123. ->setMethods(['getList'])
  124. ->getMock();
  125. $attributeRepository->expects($this->any())
  126. ->method('getList')
  127. ->will($this->returnValue($searchResult));
  128. $metadata = $this->objectManager->getObject(
  129. \Magento\Framework\EntityManager\EntityMetadata::class,
  130. [
  131. 'entityTableName' => 'test',
  132. 'identifierField' => 'entity_id',
  133. 'eavEntityType' => 'customer_address'
  134. ]
  135. );
  136. $metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  137. ->disableOriginalConstructor()
  138. ->setMethods(['getMetadata'])
  139. ->getMock();
  140. $metadataPool->expects($this->any())
  141. ->method('getMetadata')
  142. ->with($this->equalTo(\Magento\Framework\Api\CustomAttributesDataInterface::class))
  143. ->will($this->returnValue($metadata));
  144. $searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
  145. ->disableOriginalConstructor()
  146. ->setMethods(['addFilter', 'create'])
  147. ->getMock();
  148. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $searchCriteriaBuilder->expects($this->any())
  152. ->method('addFilter')
  153. ->will($this->returnValue($searchCriteriaBuilder));
  154. $searchCriteriaBuilder->expects($this->any())
  155. ->method('create')
  156. ->will($this->returnValue($searchCriteria));
  157. /** @var \Magento\Eav\Model\CustomAttributesMapper $customAttributesMapper */
  158. $customAttributesMapper = $this->objectManager
  159. ->getObject(\Magento\Eav\Model\CustomAttributesMapper::class, [
  160. 'attributeRepository' => $attributeRepository,
  161. 'metadataPool' => $metadataPool,
  162. 'searchCriteriaBuilder' => $searchCriteriaBuilder
  163. ]);
  164. $actual = $customAttributesMapper->databaseToEntity(
  165. \Magento\Framework\Api\CustomAttributesDataInterface::class,
  166. [
  167. 'test' => 'test',
  168. 'test4' => 'test4',
  169. 'test2' => 'test2'
  170. ]
  171. );
  172. $expected = [
  173. 'test4' => 'test4',
  174. 'test2' => 'test2',
  175. \Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => [
  176. [
  177. \Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE => 'test',
  178. \Magento\Framework\Api\AttributeInterface::VALUE => 'test'
  179. ]
  180. ],
  181. 'test' => 'test'
  182. ];
  183. $this->assertEquals($expected, $actual);
  184. }
  185. /**
  186. * @return array
  187. */
  188. private function getAttributes()
  189. {
  190. /* Attribute with the code we want to copy */
  191. $attribute = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  192. ->disableOriginalConstructor()
  193. ->setMethods(['isStatic', 'getAttributeCode'])
  194. ->getMockForAbstractClass();
  195. $attribute->expects($this->any())
  196. ->method('isStatic')
  197. ->will($this->returnValue(false));
  198. $attribute->expects($this->any())
  199. ->method('getAttributeCode')
  200. ->will($this->returnValue('test'));
  201. /* Attribute with the code we don't want to copy */
  202. $attribute1 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  203. ->disableOriginalConstructor()
  204. ->setMethods(['isStatic', 'getAttributeCode'])
  205. ->getMockForAbstractClass();
  206. $attribute1->expects($this->any())
  207. ->method('isStatic')
  208. ->will($this->returnValue(false));
  209. $attribute1->expects($this->any())
  210. ->method('getAttributeCode')
  211. ->will($this->returnValue('test1'));
  212. /* Static attribute but with the code which exists in custom attributes */
  213. $attribute2 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  214. ->disableOriginalConstructor()
  215. ->setMethods(['isStatic', 'getAttributeCode'])
  216. ->getMockForAbstractClass();
  217. $attribute2->expects($this->any())
  218. ->method('isStatic')
  219. ->will($this->returnValue(true));
  220. $attribute2->expects($this->any())
  221. ->method('getAttributeCode')
  222. ->will($this->returnValue('test2'));
  223. return [$attribute, $attribute1, $attribute2];
  224. }
  225. }