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