AddressRepositoryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class AddressRepositoryTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Directory\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $directoryData;
  17. /**
  18. * @var \Magento\Customer\Model\AddressFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $addressFactory;
  21. /**
  22. * @var \Magento\Customer\Model\AddressRegistry|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $addressRegistry;
  25. /**
  26. * @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $customerRegistry;
  29. /**
  30. * @var \Magento\Customer\Model\ResourceModel\Address|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $addressResourceModel;
  33. /**
  34. * @var \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $addressSearchResultsFactory;
  37. /**
  38. * @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $addressCollectionFactory;
  41. /**
  42. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $extensionAttributesJoinProcessor;
  45. /**
  46. * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $customer;
  49. /**
  50. * @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $address;
  53. /**
  54. * @var \Magento\Customer\Model\ResourceModel\AddressRepository
  55. */
  56. protected $repository;
  57. /**
  58. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $collectionProcessor;
  61. protected function setUp()
  62. {
  63. $this->addressFactory = $this->createPartialMock(\Magento\Customer\Model\AddressFactory::class, ['create']);
  64. $this->addressRegistry = $this->createMock(\Magento\Customer\Model\AddressRegistry::class);
  65. $this->customerRegistry = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
  66. $this->addressResourceModel = $this->createMock(\Magento\Customer\Model\ResourceModel\Address::class);
  67. $this->directoryData = $this->createMock(\Magento\Directory\Helper\Data::class);
  68. $this->addressSearchResultsFactory = $this->createPartialMock(
  69. \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory::class,
  70. ['create']
  71. );
  72. $this->addressCollectionFactory = $this->createPartialMock(
  73. \Magento\Customer\Model\ResourceModel\Address\CollectionFactory::class,
  74. ['create']
  75. );
  76. $this->extensionAttributesJoinProcessor = $this->getMockForAbstractClass(
  77. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class,
  78. [],
  79. '',
  80. false
  81. );
  82. $this->customer = $this->createMock(\Magento\Customer\Model\Customer::class);
  83. $this->address = $this->createPartialMock(\Magento\Customer\Model\Address::class, [
  84. 'getId',
  85. 'getCountryId',
  86. 'getFirstname',
  87. 'getLastname',
  88. 'getStreetLine',
  89. 'getCity',
  90. 'getTelephone',
  91. 'getRegionId',
  92. 'getRegion',
  93. 'updateData',
  94. 'setCustomer',
  95. 'getCountryModel',
  96. 'getShouldIgnoreValidation',
  97. 'validate',
  98. 'save',
  99. 'getDataModel',
  100. 'getCustomerId',
  101. ]);
  102. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  103. ->getMockForAbstractClass();
  104. $this->repository = new \Magento\Customer\Model\ResourceModel\AddressRepository(
  105. $this->addressFactory,
  106. $this->addressRegistry,
  107. $this->customerRegistry,
  108. $this->addressResourceModel,
  109. $this->directoryData,
  110. $this->addressSearchResultsFactory,
  111. $this->addressCollectionFactory,
  112. $this->extensionAttributesJoinProcessor,
  113. $this->collectionProcessor
  114. );
  115. }
  116. public function testSave()
  117. {
  118. $customerId = 34;
  119. $addressId = 53;
  120. $customerAddress = $this->getMockForAbstractClass(
  121. \Magento\Customer\Api\Data\AddressInterface::class,
  122. [],
  123. '',
  124. false
  125. );
  126. $addressCollection =
  127. $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
  128. $customerAddress->expects($this->atLeastOnce())
  129. ->method('getCustomerId')
  130. ->willReturn($customerId);
  131. $customerAddress->expects($this->atLeastOnce())
  132. ->method('getId')
  133. ->willReturn($addressId);
  134. $this->customerRegistry->expects($this->once())
  135. ->method('retrieve')
  136. ->with($customerId)
  137. ->willReturn($this->customer);
  138. $this->address->expects($this->atLeastOnce())
  139. ->method("getId")
  140. ->willReturn($addressId);
  141. $this->addressRegistry->expects($this->once())
  142. ->method('retrieve')
  143. ->with($addressId)
  144. ->willReturn(null);
  145. $this->addressFactory->expects($this->once())
  146. ->method('create')
  147. ->willReturn($this->address);
  148. $this->address->expects($this->once())
  149. ->method('updateData')
  150. ->with($customerAddress);
  151. $this->address->expects($this->once())
  152. ->method('setCustomer')
  153. ->with($this->customer);
  154. $this->address->expects($this->once())
  155. ->method('validate')
  156. ->willReturn(true);
  157. $this->address->expects($this->once())
  158. ->method('save');
  159. $this->addressRegistry->expects($this->once())
  160. ->method('push')
  161. ->with($this->address);
  162. $this->customer->expects($this->exactly(2))
  163. ->method('getAddressesCollection')
  164. ->willReturn($addressCollection);
  165. $addressCollection->expects($this->once())
  166. ->method("removeItemByKey")
  167. ->with($addressId);
  168. $addressCollection->expects($this->once())
  169. ->method("addItem")
  170. ->with($this->address);
  171. $this->address->expects($this->once())
  172. ->method('getDataModel')
  173. ->willReturn($customerAddress);
  174. $this->repository->save($customerAddress);
  175. }
  176. /**
  177. * @expectedException \Magento\Framework\Exception\InputException
  178. */
  179. public function testSaveWithException()
  180. {
  181. $customerId = 34;
  182. $addressId = 53;
  183. $errors[] = __('Please enter the state/province.');
  184. $customerAddress = $this->getMockForAbstractClass(
  185. \Magento\Customer\Api\Data\AddressInterface::class,
  186. [],
  187. '',
  188. false
  189. );
  190. $customerAddress->expects($this->atLeastOnce())
  191. ->method('getCustomerId')
  192. ->willReturn($customerId);
  193. $customerAddress->expects($this->atLeastOnce())
  194. ->method('getId')
  195. ->willReturn($addressId);
  196. $this->customerRegistry->expects($this->once())
  197. ->method('retrieve')
  198. ->with($customerId)
  199. ->willReturn($this->customer);
  200. $this->addressRegistry->expects($this->once())
  201. ->method('retrieve')
  202. ->with($addressId)
  203. ->willReturn($this->address);
  204. $this->address->expects($this->once())
  205. ->method('updateData')
  206. ->with($customerAddress);
  207. $this->address->expects($this->once())
  208. ->method('validate')
  209. ->willReturn($errors);
  210. $this->repository->save($customerAddress);
  211. }
  212. /**
  213. * @expectedException \Magento\Framework\Exception\InputException
  214. * @expectedExceptionMessage region is a required field.
  215. */
  216. public function testSaveWithInvalidRegion()
  217. {
  218. $customerId = 34;
  219. $addressId = 53;
  220. $errors[] = __('region is a required field.');
  221. $customerAddress = $this->getMockForAbstractClass(
  222. \Magento\Customer\Api\Data\AddressInterface::class,
  223. [],
  224. '',
  225. false
  226. );
  227. $customerAddress->expects($this->atLeastOnce())
  228. ->method('getCustomerId')
  229. ->willReturn($customerId);
  230. $customerAddress->expects($this->atLeastOnce())
  231. ->method('getId')
  232. ->willReturn($addressId);
  233. $this->customerRegistry->expects($this->once())
  234. ->method('retrieve')
  235. ->with($customerId)
  236. ->willReturn($this->customer);
  237. $this->addressRegistry->expects($this->once())
  238. ->method('retrieve')
  239. ->with($addressId)
  240. ->willReturn($this->address);
  241. $this->address->expects($this->once())
  242. ->method('updateData')
  243. ->with($customerAddress);
  244. $this->address->expects($this->never())
  245. ->method('getRegionId')
  246. ->willReturn(null);
  247. $this->address->expects($this->once())
  248. ->method('validate')
  249. ->willReturn($errors);
  250. $this->repository->save($customerAddress);
  251. }
  252. /**
  253. * @expectedException \Magento\Framework\Exception\InputException
  254. * @expectedExceptionMessage "regionId" is required. Enter and try again.
  255. */
  256. public function testSaveWithInvalidRegionId()
  257. {
  258. $customerId = 34;
  259. $addressId = 53;
  260. $errors[] = __('"regionId" is required. Enter and try again.');
  261. $customerAddress = $this->getMockForAbstractClass(
  262. \Magento\Customer\Api\Data\AddressInterface::class,
  263. [],
  264. '',
  265. false
  266. );
  267. $customerAddress->expects($this->atLeastOnce())
  268. ->method('getCustomerId')
  269. ->willReturn($customerId);
  270. $customerAddress->expects($this->atLeastOnce())
  271. ->method('getId')
  272. ->willReturn($addressId);
  273. $this->customerRegistry->expects($this->once())
  274. ->method('retrieve')
  275. ->with($customerId)
  276. ->willReturn($this->customer);
  277. $this->addressRegistry->expects($this->once())
  278. ->method('retrieve')
  279. ->with($addressId)
  280. ->willReturn($this->address);
  281. $this->address->expects($this->once())
  282. ->method('updateData')
  283. ->with($customerAddress);
  284. $this->address->expects($this->never())
  285. ->method('getRegion')
  286. ->willReturn('');
  287. $this->address->expects($this->once())
  288. ->method('validate')
  289. ->willReturn($errors);
  290. $this->repository->save($customerAddress);
  291. }
  292. public function testGetById()
  293. {
  294. $customerAddress = $this->getMockForAbstractClass(
  295. \Magento\Customer\Api\Data\AddressInterface::class,
  296. [],
  297. '',
  298. false
  299. );
  300. $this->addressRegistry->expects($this->once())
  301. ->method('retrieve')
  302. ->with(12)
  303. ->willReturn($this->address);
  304. $this->address->expects($this->once())
  305. ->method('getDataModel')
  306. ->willReturn($customerAddress);
  307. $this->assertSame($customerAddress, $this->repository->getById(12));
  308. }
  309. public function testGetList()
  310. {
  311. $collection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
  312. $searchResults = $this->getMockForAbstractClass(
  313. \Magento\Customer\Api\Data\AddressSearchResultsInterface::class,
  314. [],
  315. '',
  316. false
  317. );
  318. $searchCriteria = $this->getMockForAbstractClass(
  319. \Magento\Framework\Api\SearchCriteriaInterface::class,
  320. [],
  321. '',
  322. false
  323. );
  324. $this->addressSearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults);
  325. $this->addressCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
  326. $this->extensionAttributesJoinProcessor->expects($this->once())
  327. ->method('process')
  328. ->with($collection, \Magento\Customer\Api\Data\AddressInterface::class);
  329. $this->collectionProcessor->expects($this->once())
  330. ->method('process')
  331. ->with($searchCriteria, $collection)
  332. ->willReturnSelf();
  333. $collection->expects($this->once())->method('getSize')->willReturn(23);
  334. $searchResults->expects($this->once())
  335. ->method('setTotalCount')
  336. ->with(23);
  337. $collection->expects($this->once())
  338. ->method('getItems')
  339. ->willReturn([$this->address]);
  340. $this->address->expects($this->once())
  341. ->method('getId')
  342. ->willReturn(12);
  343. $customerAddress = $this->getMockForAbstractClass(
  344. \Magento\Customer\Api\Data\AddressInterface::class,
  345. [],
  346. '',
  347. false
  348. );
  349. $this->addressRegistry->expects($this->once())
  350. ->method('retrieve')
  351. ->with(12)
  352. ->willReturn($this->address);
  353. $this->address->expects($this->once())
  354. ->method('getDataModel')
  355. ->willReturn($customerAddress);
  356. $searchResults->expects($this->once())
  357. ->method('setItems')
  358. ->with([$customerAddress]);
  359. $searchResults->expects($this->once())
  360. ->method('setSearchCriteria')
  361. ->with($searchCriteria);
  362. $this->assertSame($searchResults, $this->repository->getList($searchCriteria));
  363. }
  364. public function testDelete()
  365. {
  366. $addressId = 12;
  367. $customerId = 43;
  368. $addressCollection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
  369. $customerAddress = $this->getMockForAbstractClass(
  370. \Magento\Customer\Api\Data\AddressInterface::class,
  371. [],
  372. '',
  373. false
  374. );
  375. $customerAddress->expects($this->once())
  376. ->method('getId')
  377. ->willReturn($addressId);
  378. $this->address->expects($this->once())
  379. ->method('getCustomerId')
  380. ->willReturn($customerId);
  381. $this->addressRegistry->expects($this->once())
  382. ->method('retrieve')
  383. ->with($addressId)
  384. ->willReturn($this->address);
  385. $this->customerRegistry->expects($this->once())
  386. ->method('retrieve')
  387. ->with($customerId)
  388. ->willReturn($this->customer);
  389. $this->customer->expects($this->once())
  390. ->method('getAddressesCollection')
  391. ->willReturn($addressCollection);
  392. $addressCollection->expects($this->once())
  393. ->method('clear');
  394. $this->addressResourceModel->expects($this->once())
  395. ->method('delete')
  396. ->with($this->address);
  397. $this->addressRegistry->expects($this->once())
  398. ->method('remove')
  399. ->with($addressId);
  400. $this->assertTrue($this->repository->delete($customerAddress));
  401. }
  402. public function testDeleteById()
  403. {
  404. $addressId = 12;
  405. $customerId = 43;
  406. $this->address->expects($this->once())
  407. ->method('getCustomerId')
  408. ->willReturn($customerId);
  409. $addressCollection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
  410. $this->addressRegistry->expects($this->once())
  411. ->method('retrieve')
  412. ->with($addressId)
  413. ->willReturn($this->address);
  414. $this->customerRegistry->expects($this->once())
  415. ->method('retrieve')
  416. ->with($customerId)
  417. ->willReturn($this->customer);
  418. $this->customer->expects($this->once())
  419. ->method('getAddressesCollection')
  420. ->willReturn($addressCollection);
  421. $addressCollection->expects($this->once())
  422. ->method('removeItemByKey')
  423. ->with($addressId);
  424. $this->addressResourceModel->expects($this->once())
  425. ->method('delete')
  426. ->with($this->address);
  427. $this->addressRegistry->expects($this->once())
  428. ->method('remove')
  429. ->with($addressId);
  430. $this->assertTrue($this->repository->deleteById($addressId));
  431. }
  432. }