59 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
61 $this->objectFactoryMock = $this->getMockBuilder(\
Magento\Framework\Api\ObjectFactory::class)
62 ->disableOriginalConstructor()
64 $this->objectProcessorMock = $this->getMockBuilder(\
Magento\Framework\Reflection\DataObjectProcessor::class)
65 ->disableOriginalConstructor()
67 $this->methodsMapProcessor = $this->getMockBuilder(\
Magento\Framework\Reflection\MethodsMap::class)
68 ->disableOriginalConstructor()
70 $this->attributeValueFactoryMock = $this->getMockBuilder(\
Magento\Framework\Api\AttributeValueFactory::class)
71 ->disableOriginalConstructor()
73 $this->joinProcessorMock = $this->getMockBuilder(\
Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class)
74 ->setMethods([
'extractExtensionAttributes'])
75 ->disableOriginalConstructor()
77 $this->joinProcessorMock->expects($this->any())
78 ->method(
'extractExtensionAttributes')
79 ->willReturnArgument(1);
80 $this->typeProcessor = $this->objectManager->getObject(\
Magento\Framework\Reflection\TypeProcessor::class);
82 $this->dataObjectHelper = $this->objectManager->getObject(
83 \
Magento\Framework\Api\DataObjectHelper::class,
85 'objectFactory' => $this->objectFactoryMock,
86 'typeProcessor' => $this->typeProcessor,
87 'objectProcessor' => $this->objectProcessorMock,
88 'methodsMapProcessor' => $this->methodsMapProcessor,
89 'joinProcessor' => $this->joinProcessorMock
94 public function testPopulateWithArrayWithSimpleAttributes()
98 $street = [
"7700 W Parmer Lane",
"second line"];
99 $isDefaultShipping =
true;
105 $addressDataObject = $this->objectManager->getObject(
106 \
Magento\Customer\Model\Data\Address::class,
108 'dataObjectHelper' => $this->dataObjectHelper,
113 $regionDataObject = $this->objectManager->getObject(
114 \
Magento\Customer\Model\Data\Region::class,
116 'dataObjectHelper' => $this->dataObjectHelper,
121 'country_id' => $countryId,
123 'default_shipping' => $isDefaultShipping,
125 'region_id' => $regionId,
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())
140 ->with(\
Magento\Customer\Api\Data\RegionInterface::class, [])
141 ->willReturn($regionDataObject);
143 $this->dataObjectHelper->populateWithArray(
146 \
Magento\Customer\Api\Data\AddressInterface::class
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());
157 public function testPopulateWithArrayWithCustomAttribute()
161 $customAttributeCode =
'custom_attribute_code_1';
162 $customAttributeValue =
'custom_attribute_value_1';
164 $attributeMetaDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\AttributeMetadataInterface::class)
166 $attributeMetaDataMock->expects($this->once())
167 ->method(
'getAttributeCode')
168 ->willReturn($customAttributeCode);
169 $metadataServiceMock = $this->getMockBuilder(\
Magento\Customer\Model\Metadata\AddressMetadata::class)
170 ->disableOriginalConstructor()
172 $metadataServiceMock->expects($this->once())
173 ->method(
'getCustomAttributesMetadata')
174 ->with(\
Magento\Customer\Model\Data\Address::class)
176 [$attributeMetaDataMock]
180 $addressDataObject = $this->objectManager->getObject(
181 \
Magento\Customer\Model\Data\Address::class,
183 'dataObjectHelper' => $this->dataObjectHelper,
184 'metadataService' => $metadataServiceMock,
185 'attributeValueFactory' => $this->attributeValueFactoryMock,
191 $customAttributeCode => $customAttributeValue,
194 $customAttribute = $this->objectManager->getObject(\
Magento\Framework\Api\AttributeValue::class);
195 $this->attributeValueFactoryMock->expects($this->once())
197 ->willReturn($customAttribute);
198 $this->dataObjectHelper->populateWithArray(
201 \
Magento\Customer\Api\Data\AddressInterface::class
204 $this->assertEquals(
$id, $addressDataObject->getId());
206 $customAttributeValue,
207 $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
210 $customAttributeCode,
211 $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
215 public function testPopulateWithArrayWithCustomAttributes()
219 $customAttributeCode =
'custom_attribute_code_1';
220 $customAttributeValue =
'custom_attribute_value_1';
222 $attributeMetaDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\AttributeMetadataInterface::class)
224 $attributeMetaDataMock->expects($this->once())
225 ->method(
'getAttributeCode')
226 ->willReturn($customAttributeCode);
227 $metadataServiceMock = $this->getMockBuilder(\
Magento\Customer\Model\Metadata\AddressMetadata::class)
228 ->disableOriginalConstructor()
230 $metadataServiceMock->expects($this->once())
231 ->method(
'getCustomAttributesMetadata')
232 ->with(\
Magento\Customer\Model\Data\Address::class)
234 [$attributeMetaDataMock]
238 $addressDataObject = $this->objectManager->getObject(
239 \
Magento\Customer\Model\Data\Address::class,
241 'dataObjectHelper' => $this->dataObjectHelper,
242 'metadataService' => $metadataServiceMock,
243 'attributeValueFactory' => $this->attributeValueFactoryMock,
257 $customAttribute = $this->objectManager->getObject(\
Magento\Framework\Api\AttributeValue::class);
258 $this->attributeValueFactoryMock->expects($this->once())
260 ->willReturn($customAttribute);
261 $this->dataObjectHelper->populateWithArray(
264 \
Magento\Customer\Api\Data\AddressInterface::class
267 $this->assertEquals(
$id, $addressDataObject->getId());
269 $customAttributeValue,
270 $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
273 $customAttributeCode,
274 $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
283 public function testMergeDataObjects($data1,
$data2)
286 $firstAddressDataObject = $this->objectManager->getObject(
287 \
Magento\Customer\Model\Data\Address::class,
289 'dataObjectHelper' => $this->dataObjectHelper,
294 $firstRegionDataObject = $this->objectManager->getObject(
295 \
Magento\Customer\Model\Data\Region::class,
297 'dataObjectHelper' => $this->dataObjectHelper,
301 $firstRegionDataObject->setRegionId($data1[
'region'][
'region_id']);
302 $firstRegionDataObject->setRegion($data1[
'region'][
'region']);
303 if (isset($data1[
'id'])) {
304 $firstAddressDataObject->setId($data1[
'id']);
306 if (isset($data1[
'country_id'])) {
307 $firstAddressDataObject->setCountryId($data1[
'country_id']);
309 $firstAddressDataObject->setStreet($data1[
'street']);
310 $firstAddressDataObject->setIsDefaultShipping($data1[
'default_shipping']);
311 $firstAddressDataObject->setRegion($firstRegionDataObject);
313 $secondAddressDataObject = $this->objectManager->getObject(
314 \
Magento\Customer\Model\Data\Address::class,
316 'dataObjectHelper' => $this->dataObjectHelper,
321 $secondRegionDataObject = $this->objectManager->getObject(
322 \
Magento\Customer\Model\Data\Region::class,
324 'dataObjectHelper' => $this->dataObjectHelper,
328 $secondRegionDataObject->setRegionId(
$data2[
'region'][
'region_id']);
329 $secondRegionDataObject->setRegion(
$data2[
'region'][
'region']);
330 if (isset(
$data2[
'id'])) {
331 $secondAddressDataObject->setId(
$data2[
'id']);
333 if (isset(
$data2[
'country_id'])) {
334 $secondAddressDataObject->setCountryId(
$data2[
'country_id']);
336 $secondAddressDataObject->setStreet(
$data2[
'street']);
337 $secondAddressDataObject->setIsDefaultShipping(
$data2[
'default_shipping']);
338 $secondAddressDataObject->setRegion($secondRegionDataObject);
340 $this->objectProcessorMock->expects($this->once())
341 ->method(
'buildOutputDataArray')
342 ->with($secondAddressDataObject, get_class($firstAddressDataObject))
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())
354 ->with(\
Magento\Customer\Api\Data\RegionInterface::class, [])
355 ->willReturn($secondRegionDataObject);
357 $this->dataObjectHelper->mergeDataObjects(
358 get_class($firstAddressDataObject),
359 $firstAddressDataObject,
360 $secondAddressDataObject
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());
380 'street' => [
"7701 W Parmer Lane",
"Second Line"],
381 'default_shipping' =>
true,
390 'street' => [
"7702 W Parmer Lane",
"Second Line"],
391 'default_shipping' =>
false,
400 'street' => [
"7701 W Parmer Lane",
"Second Line"],
401 'default_shipping' =>
true,
410 'street' => [
"7702 W Parmer Lane",
"Second Line"],
411 'default_shipping' =>
false,
dataProviderForTestMergeDataObjects()
$attributeValueFactoryMock