Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObjectHelperTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class DataObjectHelperTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $dataObjectHelper;
21 
25  protected $objectManager;
26 
30  protected $objectFactoryMock;
31 
35  protected $typeProcessor;
36 
41 
46 
51 
55  protected $joinProcessorMock;
56 
57  protected function setUp()
58  {
59  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
60 
61  $this->objectFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\ObjectFactory::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64  $this->objectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->methodsMapProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\MethodsMap::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->joinProcessorMock = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class)
74  ->setMethods(['extractExtensionAttributes'])
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->joinProcessorMock->expects($this->any())
78  ->method('extractExtensionAttributes')
79  ->willReturnArgument(1);
80  $this->typeProcessor = $this->objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class);
81 
82  $this->dataObjectHelper = $this->objectManager->getObject(
83  \Magento\Framework\Api\DataObjectHelper::class,
84  [
85  'objectFactory' => $this->objectFactoryMock,
86  'typeProcessor' => $this->typeProcessor,
87  'objectProcessor' => $this->objectProcessorMock,
88  'methodsMapProcessor' => $this->methodsMapProcessor,
89  'joinProcessor' => $this->joinProcessorMock
90  ]
91  );
92  }
93 
94  public function testPopulateWithArrayWithSimpleAttributes()
95  {
96  $id = 5;
97  $countryId = 15;
98  $street = ["7700 W Parmer Lane", "second line"];
99  $isDefaultShipping = true;
100 
101  $regionId = 7;
102  $region = "TX";
103 
105  $addressDataObject = $this->objectManager->getObject(
106  \Magento\Customer\Model\Data\Address::class,
107  [
108  'dataObjectHelper' => $this->dataObjectHelper,
109  ]
110  );
111 
113  $regionDataObject = $this->objectManager->getObject(
114  \Magento\Customer\Model\Data\Region::class,
115  [
116  'dataObjectHelper' => $this->dataObjectHelper,
117  ]
118  );
119  $data = [
120  'id' => $id,
121  'country_id' => $countryId,
122  'street' => $street,
123  'default_shipping' => $isDefaultShipping,
124  'region' => [
125  'region_id' => $regionId,
126  'region' => $region,
127  ],
128  ];
129 
130  $this->methodsMapProcessor->expects($this->at(0))
131  ->method('getMethodReturnType')
132  ->with(\Magento\Customer\Api\Data\AddressInterface::class, 'getStreet')
133  ->willReturn('string[]');
134  $this->methodsMapProcessor->expects($this->at(1))
135  ->method('getMethodReturnType')
136  ->with(\Magento\Customer\Api\Data\AddressInterface::class, 'getRegion')
137  ->willReturn(\Magento\Customer\Api\Data\RegionInterface::class);
138  $this->objectFactoryMock->expects($this->once())
139  ->method('create')
140  ->with(\Magento\Customer\Api\Data\RegionInterface::class, [])
141  ->willReturn($regionDataObject);
142 
143  $this->dataObjectHelper->populateWithArray(
144  $addressDataObject,
145  $data,
146  \Magento\Customer\Api\Data\AddressInterface::class
147  );
148 
149  $this->assertEquals($id, $addressDataObject->getId());
150  $this->assertEquals($countryId, $addressDataObject->getCountryId());
151  $this->assertEquals($street, $addressDataObject->getStreet());
152  $this->assertEquals($isDefaultShipping, $addressDataObject->isDefaultShipping());
153  $this->assertEquals($region, $addressDataObject->getRegion()->getRegion());
154  $this->assertEquals($regionId, $addressDataObject->getRegion()->getRegionId());
155  }
156 
157  public function testPopulateWithArrayWithCustomAttribute()
158  {
159  $id = 5;
160 
161  $customAttributeCode = 'custom_attribute_code_1';
162  $customAttributeValue = 'custom_attribute_value_1';
163 
164  $attributeMetaDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
165  ->getMock();
166  $attributeMetaDataMock->expects($this->once())
167  ->method('getAttributeCode')
168  ->willReturn($customAttributeCode);
169  $metadataServiceMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\AddressMetadata::class)
170  ->disableOriginalConstructor()
171  ->getMock();
172  $metadataServiceMock->expects($this->once())
173  ->method('getCustomAttributesMetadata')
174  ->with(\Magento\Customer\Model\Data\Address::class)
175  ->willReturn(
176  [$attributeMetaDataMock]
177  );
178 
180  $addressDataObject = $this->objectManager->getObject(
181  \Magento\Customer\Model\Data\Address::class,
182  [
183  'dataObjectHelper' => $this->dataObjectHelper,
184  'metadataService' => $metadataServiceMock,
185  'attributeValueFactory' => $this->attributeValueFactoryMock,
186  ]
187  );
188 
189  $data = [
190  'id' => $id,
191  $customAttributeCode => $customAttributeValue,
192  ];
193 
194  $customAttribute = $this->objectManager->getObject(\Magento\Framework\Api\AttributeValue::class);
195  $this->attributeValueFactoryMock->expects($this->once())
196  ->method('create')
197  ->willReturn($customAttribute);
198  $this->dataObjectHelper->populateWithArray(
199  $addressDataObject,
200  $data,
201  \Magento\Customer\Api\Data\AddressInterface::class
202  );
203 
204  $this->assertEquals($id, $addressDataObject->getId());
205  $this->assertEquals(
206  $customAttributeValue,
207  $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
208  );
209  $this->assertEquals(
210  $customAttributeCode,
211  $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
212  );
213  }
214 
215  public function testPopulateWithArrayWithCustomAttributes()
216  {
217  $id = 5;
218 
219  $customAttributeCode = 'custom_attribute_code_1';
220  $customAttributeValue = 'custom_attribute_value_1';
221 
222  $attributeMetaDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
223  ->getMock();
224  $attributeMetaDataMock->expects($this->once())
225  ->method('getAttributeCode')
226  ->willReturn($customAttributeCode);
227  $metadataServiceMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\AddressMetadata::class)
228  ->disableOriginalConstructor()
229  ->getMock();
230  $metadataServiceMock->expects($this->once())
231  ->method('getCustomAttributesMetadata')
232  ->with(\Magento\Customer\Model\Data\Address::class)
233  ->willReturn(
234  [$attributeMetaDataMock]
235  );
236 
238  $addressDataObject = $this->objectManager->getObject(
239  \Magento\Customer\Model\Data\Address::class,
240  [
241  'dataObjectHelper' => $this->dataObjectHelper,
242  'metadataService' => $metadataServiceMock,
243  'attributeValueFactory' => $this->attributeValueFactoryMock,
244  ]
245  );
246 
247  $data = [
248  'id' => $id,
250  [
251  AttributeInterface::ATTRIBUTE_CODE => $customAttributeCode,
252  AttributeInterface::VALUE => $customAttributeValue,
253  ],
254  ],
255  ];
256 
257  $customAttribute = $this->objectManager->getObject(\Magento\Framework\Api\AttributeValue::class);
258  $this->attributeValueFactoryMock->expects($this->once())
259  ->method('create')
260  ->willReturn($customAttribute);
261  $this->dataObjectHelper->populateWithArray(
262  $addressDataObject,
263  $data,
264  \Magento\Customer\Api\Data\AddressInterface::class
265  );
266 
267  $this->assertEquals($id, $addressDataObject->getId());
268  $this->assertEquals(
269  $customAttributeValue,
270  $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
271  );
272  $this->assertEquals(
273  $customAttributeCode,
274  $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
275  );
276  }
277 
283  public function testMergeDataObjects($data1, $data2)
284  {
286  $firstAddressDataObject = $this->objectManager->getObject(
287  \Magento\Customer\Model\Data\Address::class,
288  [
289  'dataObjectHelper' => $this->dataObjectHelper,
290  ]
291  );
292 
294  $firstRegionDataObject = $this->objectManager->getObject(
295  \Magento\Customer\Model\Data\Region::class,
296  [
297  'dataObjectHelper' => $this->dataObjectHelper,
298  ]
299  );
300 
301  $firstRegionDataObject->setRegionId($data1['region']['region_id']);
302  $firstRegionDataObject->setRegion($data1['region']['region']);
303  if (isset($data1['id'])) {
304  $firstAddressDataObject->setId($data1['id']);
305  }
306  if (isset($data1['country_id'])) {
307  $firstAddressDataObject->setCountryId($data1['country_id']);
308  }
309  $firstAddressDataObject->setStreet($data1['street']);
310  $firstAddressDataObject->setIsDefaultShipping($data1['default_shipping']);
311  $firstAddressDataObject->setRegion($firstRegionDataObject);
312 
313  $secondAddressDataObject = $this->objectManager->getObject(
314  \Magento\Customer\Model\Data\Address::class,
315  [
316  'dataObjectHelper' => $this->dataObjectHelper,
317  ]
318  );
319 
321  $secondRegionDataObject = $this->objectManager->getObject(
322  \Magento\Customer\Model\Data\Region::class,
323  [
324  'dataObjectHelper' => $this->dataObjectHelper,
325  ]
326  );
327 
328  $secondRegionDataObject->setRegionId($data2['region']['region_id']);
329  $secondRegionDataObject->setRegion($data2['region']['region']);
330  if (isset($data2['id'])) {
331  $secondAddressDataObject->setId($data2['id']);
332  }
333  if (isset($data2['country_id'])) {
334  $secondAddressDataObject->setCountryId($data2['country_id']);
335  }
336  $secondAddressDataObject->setStreet($data2['street']);
337  $secondAddressDataObject->setIsDefaultShipping($data2['default_shipping']);
338  $secondAddressDataObject->setRegion($secondRegionDataObject);
339 
340  $this->objectProcessorMock->expects($this->once())
341  ->method('buildOutputDataArray')
342  ->with($secondAddressDataObject, get_class($firstAddressDataObject))
343  ->willReturn($data2);
344  $this->methodsMapProcessor->expects($this->at(0))
345  ->method('getMethodReturnType')
346  ->with(\Magento\Customer\Model\Data\Address::class, 'getStreet')
347  ->willReturn('string[]');
348  $this->methodsMapProcessor->expects($this->at(1))
349  ->method('getMethodReturnType')
350  ->with(\Magento\Customer\Model\Data\Address::class, 'getRegion')
351  ->willReturn(\Magento\Customer\Api\Data\RegionInterface::class);
352  $this->objectFactoryMock->expects($this->once())
353  ->method('create')
354  ->with(\Magento\Customer\Api\Data\RegionInterface::class, [])
355  ->willReturn($secondRegionDataObject);
356 
357  $this->dataObjectHelper->mergeDataObjects(
358  get_class($firstAddressDataObject),
359  $firstAddressDataObject,
360  $secondAddressDataObject
361  );
362 
363  $this->assertSame($firstAddressDataObject->getId(), $secondAddressDataObject->getId());
364  $this->assertSame($firstAddressDataObject->getCountryId(), $secondAddressDataObject->getCountryId());
365  $this->assertSame($firstAddressDataObject->getStreet(), $secondAddressDataObject->getStreet());
366  $this->assertSame($firstAddressDataObject->isDefaultShipping(), $secondAddressDataObject->isDefaultShipping());
367  $this->assertSame($firstAddressDataObject->getRegion(), $secondAddressDataObject->getRegion());
368  }
369 
374  {
375  return [
376  [
377  [
378  'id' => '1',
379  'country_id' => '1',
380  'street' => ["7701 W Parmer Lane", "Second Line"],
381  'default_shipping' => true,
382  'region' => [
383  'region_id' => '1',
384  'region' => 'TX',
385  ]
386  ],
387  [
388  'id' => '2',
389  'country_id' => '2',
390  'street' => ["7702 W Parmer Lane", "Second Line"],
391  'default_shipping' => false,
392  'region' => [
393  'region_id' => '2',
394  'region' => 'TX',
395  ]
396  ]
397  ],
398  [
399  [
400  'street' => ["7701 W Parmer Lane", "Second Line"],
401  'default_shipping' => true,
402  'region' => [
403  'region_id' => '1',
404  'region' => 'TX',
405  ]
406  ],
407  [
408  'id' => '2',
409  'country_id' => '2',
410  'street' => ["7702 W Parmer Lane", "Second Line"],
411  'default_shipping' => false,
412  'region' => [
413  'region_id' => '2',
414  'region' => 'TX',
415  ]
416  ]
417  ]
418  ];
419  }
420 }
$id
Definition: fieldset.phtml:14