DataObjectHelperTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api\Test\Unit;
  7. use Magento\Framework\Api\CustomAttributesDataInterface;
  8. use Magento\Framework\Api\AttributeInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class DataObjectHelperTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\Api\DataObjectHelper
  16. */
  17. protected $dataObjectHelper;
  18. /**
  19. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  20. */
  21. protected $objectManager;
  22. /**
  23. * @var \Magento\Framework\Api\ObjectFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $objectFactoryMock;
  26. /**
  27. * @var \Magento\Framework\Reflection\TypeProcessor
  28. */
  29. protected $typeProcessor;
  30. /**
  31. * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $objectProcessorMock;
  34. /**
  35. * @var \Magento\Framework\Api\AttributeValueFactory|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $attributeValueFactoryMock;
  38. /**
  39. * @var \Magento\Framework\Reflection\MethodsMap|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $methodsMapProcessor;
  42. /**
  43. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessor|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $joinProcessorMock;
  46. protected function setUp()
  47. {
  48. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->objectFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\ObjectFactory::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->objectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->methodsMapProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\MethodsMap::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->joinProcessorMock = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class)
  62. ->setMethods(['extractExtensionAttributes'])
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->joinProcessorMock->expects($this->any())
  66. ->method('extractExtensionAttributes')
  67. ->willReturnArgument(1);
  68. $this->typeProcessor = $this->objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class);
  69. $this->dataObjectHelper = $this->objectManager->getObject(
  70. \Magento\Framework\Api\DataObjectHelper::class,
  71. [
  72. 'objectFactory' => $this->objectFactoryMock,
  73. 'typeProcessor' => $this->typeProcessor,
  74. 'objectProcessor' => $this->objectProcessorMock,
  75. 'methodsMapProcessor' => $this->methodsMapProcessor,
  76. 'joinProcessor' => $this->joinProcessorMock
  77. ]
  78. );
  79. }
  80. public function testPopulateWithArrayWithSimpleAttributes()
  81. {
  82. $id = 5;
  83. $countryId = 15;
  84. $street = ["7700 W Parmer Lane", "second line"];
  85. $isDefaultShipping = true;
  86. $regionId = 7;
  87. $region = "TX";
  88. /** @var \Magento\Customer\Model\Data\Address $addressDataObject */
  89. $addressDataObject = $this->objectManager->getObject(
  90. \Magento\Customer\Model\Data\Address::class,
  91. [
  92. 'dataObjectHelper' => $this->dataObjectHelper,
  93. ]
  94. );
  95. /** @var \Magento\Customer\Model\Data\Region $regionDataObject */
  96. $regionDataObject = $this->objectManager->getObject(
  97. \Magento\Customer\Model\Data\Region::class,
  98. [
  99. 'dataObjectHelper' => $this->dataObjectHelper,
  100. ]
  101. );
  102. $data = [
  103. 'id' => $id,
  104. 'country_id' => $countryId,
  105. 'street' => $street,
  106. 'default_shipping' => $isDefaultShipping,
  107. 'region' => [
  108. 'region_id' => $regionId,
  109. 'region' => $region,
  110. ],
  111. ];
  112. $this->methodsMapProcessor->expects($this->at(0))
  113. ->method('getMethodReturnType')
  114. ->with(\Magento\Customer\Api\Data\AddressInterface::class, 'getStreet')
  115. ->willReturn('string[]');
  116. $this->methodsMapProcessor->expects($this->at(1))
  117. ->method('getMethodReturnType')
  118. ->with(\Magento\Customer\Api\Data\AddressInterface::class, 'getRegion')
  119. ->willReturn(\Magento\Customer\Api\Data\RegionInterface::class);
  120. $this->objectFactoryMock->expects($this->once())
  121. ->method('create')
  122. ->with(\Magento\Customer\Api\Data\RegionInterface::class, [])
  123. ->willReturn($regionDataObject);
  124. $this->dataObjectHelper->populateWithArray(
  125. $addressDataObject,
  126. $data,
  127. \Magento\Customer\Api\Data\AddressInterface::class
  128. );
  129. $this->assertEquals($id, $addressDataObject->getId());
  130. $this->assertEquals($countryId, $addressDataObject->getCountryId());
  131. $this->assertEquals($street, $addressDataObject->getStreet());
  132. $this->assertEquals($isDefaultShipping, $addressDataObject->isDefaultShipping());
  133. $this->assertEquals($region, $addressDataObject->getRegion()->getRegion());
  134. $this->assertEquals($regionId, $addressDataObject->getRegion()->getRegionId());
  135. }
  136. public function testPopulateWithArrayWithCustomAttribute()
  137. {
  138. $id = 5;
  139. $customAttributeCode = 'custom_attribute_code_1';
  140. $customAttributeValue = 'custom_attribute_value_1';
  141. $attributeMetaDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  142. ->getMock();
  143. $attributeMetaDataMock->expects($this->once())
  144. ->method('getAttributeCode')
  145. ->willReturn($customAttributeCode);
  146. $metadataServiceMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\AddressMetadata::class)
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $metadataServiceMock->expects($this->once())
  150. ->method('getCustomAttributesMetadata')
  151. ->with(\Magento\Customer\Model\Data\Address::class)
  152. ->willReturn(
  153. [$attributeMetaDataMock]
  154. );
  155. /** @var \Magento\Customer\Model\Data\Address $addressDataObject */
  156. $addressDataObject = $this->objectManager->getObject(
  157. \Magento\Customer\Model\Data\Address::class,
  158. [
  159. 'dataObjectHelper' => $this->dataObjectHelper,
  160. 'metadataService' => $metadataServiceMock,
  161. 'attributeValueFactory' => $this->attributeValueFactoryMock,
  162. ]
  163. );
  164. $data = [
  165. 'id' => $id,
  166. $customAttributeCode => $customAttributeValue,
  167. ];
  168. $customAttribute = $this->objectManager->getObject(\Magento\Framework\Api\AttributeValue::class);
  169. $this->attributeValueFactoryMock->expects($this->once())
  170. ->method('create')
  171. ->willReturn($customAttribute);
  172. $this->dataObjectHelper->populateWithArray(
  173. $addressDataObject,
  174. $data,
  175. \Magento\Customer\Api\Data\AddressInterface::class
  176. );
  177. $this->assertEquals($id, $addressDataObject->getId());
  178. $this->assertEquals(
  179. $customAttributeValue,
  180. $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
  181. );
  182. $this->assertEquals(
  183. $customAttributeCode,
  184. $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
  185. );
  186. }
  187. public function testPopulateWithArrayWithCustomAttributes()
  188. {
  189. $id = 5;
  190. $customAttributeCode = 'custom_attribute_code_1';
  191. $customAttributeValue = 'custom_attribute_value_1';
  192. $attributeMetaDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  193. ->getMock();
  194. $attributeMetaDataMock->expects($this->once())
  195. ->method('getAttributeCode')
  196. ->willReturn($customAttributeCode);
  197. $metadataServiceMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\AddressMetadata::class)
  198. ->disableOriginalConstructor()
  199. ->getMock();
  200. $metadataServiceMock->expects($this->once())
  201. ->method('getCustomAttributesMetadata')
  202. ->with(\Magento\Customer\Model\Data\Address::class)
  203. ->willReturn(
  204. [$attributeMetaDataMock]
  205. );
  206. /** @var \Magento\Customer\Model\Data\Address $addressDataObject */
  207. $addressDataObject = $this->objectManager->getObject(
  208. \Magento\Customer\Model\Data\Address::class,
  209. [
  210. 'dataObjectHelper' => $this->dataObjectHelper,
  211. 'metadataService' => $metadataServiceMock,
  212. 'attributeValueFactory' => $this->attributeValueFactoryMock,
  213. ]
  214. );
  215. $data = [
  216. 'id' => $id,
  217. CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => [
  218. [
  219. AttributeInterface::ATTRIBUTE_CODE => $customAttributeCode,
  220. AttributeInterface::VALUE => $customAttributeValue,
  221. ],
  222. ],
  223. ];
  224. $customAttribute = $this->objectManager->getObject(\Magento\Framework\Api\AttributeValue::class);
  225. $this->attributeValueFactoryMock->expects($this->once())
  226. ->method('create')
  227. ->willReturn($customAttribute);
  228. $this->dataObjectHelper->populateWithArray(
  229. $addressDataObject,
  230. $data,
  231. \Magento\Customer\Api\Data\AddressInterface::class
  232. );
  233. $this->assertEquals($id, $addressDataObject->getId());
  234. $this->assertEquals(
  235. $customAttributeValue,
  236. $addressDataObject->getCustomAttribute($customAttributeCode)->getValue()
  237. );
  238. $this->assertEquals(
  239. $customAttributeCode,
  240. $addressDataObject->getCustomAttribute($customAttributeCode)->getAttributeCode()
  241. );
  242. }
  243. /**
  244. * @param array $data1
  245. * @param array $data2
  246. * @dataProvider dataProviderForTestMergeDataObjects
  247. */
  248. public function testMergeDataObjects($data1, $data2)
  249. {
  250. /** @var \Magento\Customer\Model\Data\Address $addressDataObject */
  251. $firstAddressDataObject = $this->objectManager->getObject(
  252. \Magento\Customer\Model\Data\Address::class,
  253. [
  254. 'dataObjectHelper' => $this->dataObjectHelper,
  255. ]
  256. );
  257. /** @var \Magento\Customer\Model\Data\Region $regionDataObject */
  258. $firstRegionDataObject = $this->objectManager->getObject(
  259. \Magento\Customer\Model\Data\Region::class,
  260. [
  261. 'dataObjectHelper' => $this->dataObjectHelper,
  262. ]
  263. );
  264. $firstRegionDataObject->setRegionId($data1['region']['region_id']);
  265. $firstRegionDataObject->setRegion($data1['region']['region']);
  266. if (isset($data1['id'])) {
  267. $firstAddressDataObject->setId($data1['id']);
  268. }
  269. if (isset($data1['country_id'])) {
  270. $firstAddressDataObject->setCountryId($data1['country_id']);
  271. }
  272. $firstAddressDataObject->setStreet($data1['street']);
  273. $firstAddressDataObject->setIsDefaultShipping($data1['default_shipping']);
  274. $firstAddressDataObject->setRegion($firstRegionDataObject);
  275. $secondAddressDataObject = $this->objectManager->getObject(
  276. \Magento\Customer\Model\Data\Address::class,
  277. [
  278. 'dataObjectHelper' => $this->dataObjectHelper,
  279. ]
  280. );
  281. /** @var \Magento\Customer\Model\Data\Region $regionDataObject */
  282. $secondRegionDataObject = $this->objectManager->getObject(
  283. \Magento\Customer\Model\Data\Region::class,
  284. [
  285. 'dataObjectHelper' => $this->dataObjectHelper,
  286. ]
  287. );
  288. $secondRegionDataObject->setRegionId($data2['region']['region_id']);
  289. $secondRegionDataObject->setRegion($data2['region']['region']);
  290. if (isset($data2['id'])) {
  291. $secondAddressDataObject->setId($data2['id']);
  292. }
  293. if (isset($data2['country_id'])) {
  294. $secondAddressDataObject->setCountryId($data2['country_id']);
  295. }
  296. $secondAddressDataObject->setStreet($data2['street']);
  297. $secondAddressDataObject->setIsDefaultShipping($data2['default_shipping']);
  298. $secondAddressDataObject->setRegion($secondRegionDataObject);
  299. $this->objectProcessorMock->expects($this->once())
  300. ->method('buildOutputDataArray')
  301. ->with($secondAddressDataObject, get_class($firstAddressDataObject))
  302. ->willReturn($data2);
  303. $this->methodsMapProcessor->expects($this->at(0))
  304. ->method('getMethodReturnType')
  305. ->with(\Magento\Customer\Model\Data\Address::class, 'getStreet')
  306. ->willReturn('string[]');
  307. $this->methodsMapProcessor->expects($this->at(1))
  308. ->method('getMethodReturnType')
  309. ->with(\Magento\Customer\Model\Data\Address::class, 'getRegion')
  310. ->willReturn(\Magento\Customer\Api\Data\RegionInterface::class);
  311. $this->objectFactoryMock->expects($this->once())
  312. ->method('create')
  313. ->with(\Magento\Customer\Api\Data\RegionInterface::class, [])
  314. ->willReturn($secondRegionDataObject);
  315. $this->dataObjectHelper->mergeDataObjects(
  316. get_class($firstAddressDataObject),
  317. $firstAddressDataObject,
  318. $secondAddressDataObject
  319. );
  320. $this->assertSame($firstAddressDataObject->getId(), $secondAddressDataObject->getId());
  321. $this->assertSame($firstAddressDataObject->getCountryId(), $secondAddressDataObject->getCountryId());
  322. $this->assertSame($firstAddressDataObject->getStreet(), $secondAddressDataObject->getStreet());
  323. $this->assertSame($firstAddressDataObject->isDefaultShipping(), $secondAddressDataObject->isDefaultShipping());
  324. $this->assertSame($firstAddressDataObject->getRegion(), $secondAddressDataObject->getRegion());
  325. }
  326. /**
  327. * @return array
  328. */
  329. public function dataProviderForTestMergeDataObjects()
  330. {
  331. return [
  332. [
  333. [
  334. 'id' => '1',
  335. 'country_id' => '1',
  336. 'street' => ["7701 W Parmer Lane", "Second Line"],
  337. 'default_shipping' => true,
  338. 'region' => [
  339. 'region_id' => '1',
  340. 'region' => 'TX',
  341. ]
  342. ],
  343. [
  344. 'id' => '2',
  345. 'country_id' => '2',
  346. 'street' => ["7702 W Parmer Lane", "Second Line"],
  347. 'default_shipping' => false,
  348. 'region' => [
  349. 'region_id' => '2',
  350. 'region' => 'TX',
  351. ]
  352. ]
  353. ],
  354. [
  355. [
  356. 'street' => ["7701 W Parmer Lane", "Second Line"],
  357. 'default_shipping' => true,
  358. 'region' => [
  359. 'region_id' => '1',
  360. 'region' => 'TX',
  361. ]
  362. ],
  363. [
  364. 'id' => '2',
  365. 'country_id' => '2',
  366. 'street' => ["7702 W Parmer Lane", "Second Line"],
  367. 'default_shipping' => false,
  368. 'region' => [
  369. 'region_id' => '2',
  370. 'region' => 'TX',
  371. ]
  372. ]
  373. ]
  374. ];
  375. }
  376. }