Skip to content

Commit 4b892d0

Browse files
author
Robert He
committed
MAGETWO-33109 : Refactor code that uses customer builders in Quote module
-- changes from code review -- new unit tests
1 parent 1093d53 commit 4b892d0

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

app/code/Magento/Quote/Model/Quote.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,9 @@ public function addCustomerAddress(\Magento\Customer\Api\Data\AddressInterface $
910910
*/
911911
public function updateCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
912912
{
913-
$this->customerDataFactory->mergeDataObjects(get_class($this->getCustomer()), $this->getCustomer(), $customer);
914-
$this->setCustomer($customer);
913+
$quoteCustomer = $this->getCustomer();
914+
$this->dataObjectHelper->mergeDataObjects(get_class($quoteCustomer), $quoteCustomer, $customer);
915+
$this->setCustomer($quoteCustomer);
915916
return $this;
916917
}
917918

dev/tests/unit/testsuite/Magento/Framework/Api/DataObjectHelperTest.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,137 @@ public function testPopulateWithArrayWithCustomAttributes()
235235
$addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
236236
);
237237
}
238+
239+
/**
240+
* @param array $data1
241+
* @param array $data2
242+
* @dataProvider dataProviderForTestMergeDataObjects
243+
*/
244+
public function testMergeDataObjects($data1, $data2)
245+
{
246+
/** @var \Magento\Customer\Model\Data\Address $addressDataObject */
247+
$firstAddressDataObject = $this->objectManager->getObject(
248+
'Magento\Customer\Model\Data\Address',
249+
[
250+
'dataObjectHelper' => $this->dataObjectHelper,
251+
]
252+
);
253+
254+
/** @var \Magento\Customer\Model\Data\Region $regionDataObject */
255+
$firstRegionDataObject = $this->objectManager->getObject(
256+
'Magento\Customer\Model\Data\Region',
257+
[
258+
'dataObjectHelper' => $this->dataObjectHelper,
259+
]
260+
);
261+
262+
$firstRegionDataObject->setRegionId($data1['region']['region_id']);
263+
$firstRegionDataObject->setRegion($data1['region']['region']);
264+
if(isset($data1['id']))
265+
$firstAddressDataObject->setId($data1['id']);
266+
if(isset($data1['country_id']))
267+
$firstAddressDataObject->setCountryId($data1['country_id']);
268+
$firstAddressDataObject->setStreet($data1['street']);
269+
$firstAddressDataObject->setIsDefaultShipping($data1['default_shipping']);
270+
$firstAddressDataObject->setRegion($firstRegionDataObject);
271+
272+
$secondAddressDataObject = $this->objectManager->getObject(
273+
'Magento\Customer\Model\Data\Address',
274+
[
275+
'dataObjectHelper' => $this->dataObjectHelper,
276+
]
277+
);
278+
279+
/** @var \Magento\Customer\Model\Data\Region $regionDataObject */
280+
$secondRegionDataObject = $this->objectManager->getObject(
281+
'Magento\Customer\Model\Data\Region',
282+
[
283+
'dataObjectHelper' => $this->dataObjectHelper,
284+
]
285+
);
286+
287+
$secondRegionDataObject->setRegionId($data2['region']['region_id']);
288+
$secondRegionDataObject->setRegion($data2['region']['region']);
289+
if(isset($data2['id']))
290+
$secondAddressDataObject->setId($data2['id']);
291+
if(isset($data2['country_id']))
292+
$secondAddressDataObject->setCountryId($data2['country_id']);
293+
$secondAddressDataObject->setStreet($data2['street']);
294+
$secondAddressDataObject->setIsDefaultShipping($data2['default_shipping']);
295+
$secondAddressDataObject->setRegion($secondRegionDataObject);
296+
297+
$this->objectProcessorMock->expects($this->once())
298+
->method('buildOutputDataArray')
299+
->with($secondAddressDataObject, get_class($firstAddressDataObject))
300+
->willReturn($data2);
301+
$this->objectProcessorMock->expects($this->at(1))
302+
->method('getMethodReturnType')
303+
->with('Magento\Customer\Model\Data\Address', 'getStreet')
304+
->willReturn('string[]');
305+
$this->objectProcessorMock->expects($this->at(2))
306+
->method('getMethodReturnType')
307+
->with('Magento\Customer\Model\Data\Address', 'getRegion')
308+
->willReturn('\Magento\Customer\Api\Data\RegionInterface');
309+
$this->objectFactoryMock->expects($this->once())
310+
->method('create')
311+
->with('\Magento\Customer\Api\Data\RegionInterface', [])
312+
->willReturn($secondRegionDataObject);
313+
314+
$this->dataObjectHelper->mergeDataObjects(get_class($firstAddressDataObject), $firstAddressDataObject,
315+
$secondAddressDataObject);
316+
317+
$this->assertSame($firstAddressDataObject->getId(), $secondAddressDataObject->getId());
318+
$this->assertSame($firstAddressDataObject->getCountryId(), $secondAddressDataObject->getCountryId());
319+
$this->assertSame($firstAddressDataObject->getStreet(), $secondAddressDataObject->getStreet());
320+
$this->assertSame($firstAddressDataObject->isDefaultShipping(), $secondAddressDataObject->isDefaultShipping());
321+
$this->assertSame($firstAddressDataObject->getRegion(), $secondAddressDataObject->getRegion());
322+
}
323+
324+
public function dataProviderForTestMergeDataObjects()
325+
{
326+
return [
327+
[
328+
[
329+
'id' => '1',
330+
'country_id' => '1',
331+
'street' => ["7701 W Parmer Lane", "Second Line"],
332+
'default_shipping' => true,
333+
'region' => [
334+
'region_id' => '1',
335+
'region' => 'TX',
336+
]
337+
],
338+
[
339+
'id' => '2',
340+
'country_id' => '2',
341+
'street' => ["7702 W Parmer Lane", "Second Line"],
342+
'default_shipping' => false,
343+
'region' => [
344+
'region_id' => '2',
345+
'region' => 'TX',
346+
]
347+
]
348+
],
349+
[
350+
[
351+
'street' => ["7701 W Parmer Lane", "Second Line"],
352+
'default_shipping' => true,
353+
'region' => [
354+
'region_id' => '1',
355+
'region' => 'TX',
356+
]
357+
],
358+
[
359+
'id' => '2',
360+
'country_id' => '2',
361+
'street' => ["7702 W Parmer Lane", "Second Line"],
362+
'default_shipping' => false,
363+
'region' => [
364+
'region_id' => '2',
365+
'region' => 'TX',
366+
]
367+
]
368+
]
369+
];
370+
}
238371
}

0 commit comments

Comments
 (0)