EditTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Address;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class EditTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. protected $objectManager;
  16. /**
  17. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $requestMock;
  20. /**
  21. * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $addressRepositoryMock;
  24. /**
  25. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $customerSessionMock;
  28. /**
  29. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $pageConfigMock;
  32. /**
  33. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $dataObjectHelperMock;
  36. /**
  37. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $addressDataFactoryMock;
  40. /**
  41. * @var \Magento\Customer\Helper\Session\CurrentCustomer|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $currentCustomerMock;
  44. /**
  45. * @var \Magento\Customer\Block\Address\Edit
  46. */
  47. protected $model;
  48. protected function setUp()
  49. {
  50. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  52. ->getMock();
  53. $this->addressRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
  54. ->getMock();
  55. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['getAddressFormData', 'getCustomerId'])
  58. ->getMock();
  59. $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->addressDataFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
  66. ->setMethods(['create'])
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->currentCustomerMock = $this->getMockBuilder(\Magento\Customer\Helper\Session\CurrentCustomer::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->model = $this->objectManager->getObject(
  73. \Magento\Customer\Block\Address\Edit::class,
  74. [
  75. 'request' => $this->requestMock,
  76. 'addressRepository' => $this->addressRepositoryMock,
  77. 'customerSession' => $this->customerSessionMock,
  78. 'pageConfig' => $this->pageConfigMock,
  79. 'dataObjectHelper' => $this->dataObjectHelperMock,
  80. 'addressDataFactory' => $this->addressDataFactoryMock,
  81. 'currentCustomer' => $this->currentCustomerMock,
  82. ]
  83. );
  84. }
  85. public function testSetLayoutWithOwnAddressAndPostedData()
  86. {
  87. $addressId = 1;
  88. $customerId = 1;
  89. $title = __('Edit Address');
  90. $postedData = [
  91. 'region_id' => 1,
  92. 'region' => 'region',
  93. ];
  94. $newPostedData = $postedData;
  95. $newPostedData['region'] = $postedData;
  96. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  97. ->getMock();
  98. $this->requestMock->expects($this->once())
  99. ->method('getParam')
  100. ->with('id', null)
  101. ->willReturn($addressId);
  102. $addressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  103. ->getMock();
  104. $this->addressRepositoryMock->expects($this->once())
  105. ->method('getById')
  106. ->with($addressId)
  107. ->willReturn($addressMock);
  108. $addressMock->expects($this->once())
  109. ->method('getCustomerId')
  110. ->willReturn($customerId);
  111. $this->customerSessionMock->expects($this->at(0))
  112. ->method('getCustomerId')
  113. ->willReturn($customerId);
  114. $addressMock->expects($this->exactly(2))
  115. ->method('getId')
  116. ->willReturn($addressId);
  117. $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $this->pageConfigMock->expects($this->once())
  121. ->method('getTitle')
  122. ->willReturn($pageTitleMock);
  123. $pageTitleMock->expects($this->once())
  124. ->method('set')
  125. ->with($title)
  126. ->willReturnSelf();
  127. $this->customerSessionMock->expects($this->at(1))
  128. ->method('getAddressFormData')
  129. ->with(true)
  130. ->willReturn($postedData);
  131. $this->dataObjectHelperMock->expects($this->once())
  132. ->method('populateWithArray')
  133. ->with(
  134. $addressMock,
  135. $newPostedData,
  136. \Magento\Customer\Api\Data\AddressInterface::class
  137. )->willReturnSelf();
  138. $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
  139. $this->assertEquals($layoutMock, $this->model->getLayout());
  140. }
  141. /**
  142. * @throws \Magento\Framework\Exception\LocalizedException
  143. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  144. */
  145. public function testSetLayoutWithAlienAddress()
  146. {
  147. $addressId = 1;
  148. $customerId = 1;
  149. $customerPrefix = 'prefix';
  150. $customerFirstName = 'firstname';
  151. $customerMiddlename = 'middlename';
  152. $customerLastname = 'lastname';
  153. $customerSuffix = 'suffix';
  154. $title = __('Add New Address');
  155. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  156. ->getMock();
  157. $this->requestMock->expects($this->once())
  158. ->method('getParam')
  159. ->with('id', null)
  160. ->willReturn($addressId);
  161. $addressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  162. ->getMock();
  163. $this->addressRepositoryMock->expects($this->once())
  164. ->method('getById')
  165. ->with($addressId)
  166. ->willReturn($addressMock);
  167. $addressMock->expects($this->once())
  168. ->method('getCustomerId')
  169. ->willReturn($customerId);
  170. $this->customerSessionMock->expects($this->at(0))
  171. ->method('getCustomerId')
  172. ->willReturn($customerId + 1);
  173. $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  174. ->getMock();
  175. $this->addressDataFactoryMock->expects($this->once())
  176. ->method('create')
  177. ->willReturn($newAddressMock);
  178. $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  179. ->getMock();
  180. $this->currentCustomerMock->expects($this->once())
  181. ->method('getCustomer')
  182. ->willReturn($customerMock);
  183. $customerMock->expects($this->once())
  184. ->method('getPrefix')
  185. ->willReturn($customerPrefix);
  186. $customerMock->expects($this->once())
  187. ->method('getFirstname')
  188. ->willReturn($customerFirstName);
  189. $customerMock->expects($this->once())
  190. ->method('getMiddlename')
  191. ->willReturn($customerMiddlename);
  192. $customerMock->expects($this->once())
  193. ->method('getLastname')
  194. ->willReturn($customerLastname);
  195. $customerMock->expects($this->once())
  196. ->method('getSuffix')
  197. ->willReturn($customerSuffix);
  198. $newAddressMock->expects($this->once())
  199. ->method('setPrefix')
  200. ->with($customerPrefix)
  201. ->willReturnSelf();
  202. $newAddressMock->expects($this->once())
  203. ->method('setFirstname')
  204. ->with($customerFirstName)
  205. ->willReturnSelf();
  206. $newAddressMock->expects($this->once())
  207. ->method('setMiddlename')
  208. ->with($customerMiddlename)
  209. ->willReturnSelf();
  210. $newAddressMock->expects($this->once())
  211. ->method('setLastname')
  212. ->with($customerLastname)
  213. ->willReturnSelf();
  214. $newAddressMock->expects($this->once())
  215. ->method('setSuffix')
  216. ->with($customerSuffix)
  217. ->willReturnSelf();
  218. $newAddressMock->expects($this->once())
  219. ->method('getId')
  220. ->willReturn(null);
  221. $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  222. ->disableOriginalConstructor()
  223. ->getMock();
  224. $this->pageConfigMock->expects($this->once())
  225. ->method('getTitle')
  226. ->willReturn($pageTitleMock);
  227. $pageTitleMock->expects($this->once())
  228. ->method('set')
  229. ->with($title)
  230. ->willReturnSelf();
  231. $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
  232. $this->assertEquals($layoutMock, $this->model->getLayout());
  233. }
  234. public function testSetLayoutWithoutAddressId()
  235. {
  236. $customerPrefix = 'prefix';
  237. $customerFirstName = 'firstname';
  238. $customerMiddlename = 'middlename';
  239. $customerLastname = 'lastname';
  240. $customerSuffix = 'suffix';
  241. $title = 'title';
  242. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  243. ->getMock();
  244. $this->requestMock->expects($this->once())
  245. ->method('getParam')
  246. ->with('id', null)
  247. ->willReturn('');
  248. $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  249. ->getMock();
  250. $this->addressDataFactoryMock->expects($this->once())
  251. ->method('create')
  252. ->willReturn($newAddressMock);
  253. $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  254. ->getMock();
  255. $this->currentCustomerMock->expects($this->once())
  256. ->method('getCustomer')
  257. ->willReturn($customerMock);
  258. $customerMock->expects($this->once())
  259. ->method('getPrefix')
  260. ->willReturn($customerPrefix);
  261. $customerMock->expects($this->once())
  262. ->method('getFirstname')
  263. ->willReturn($customerFirstName);
  264. $customerMock->expects($this->once())
  265. ->method('getMiddlename')
  266. ->willReturn($customerMiddlename);
  267. $customerMock->expects($this->once())
  268. ->method('getLastname')
  269. ->willReturn($customerLastname);
  270. $customerMock->expects($this->once())
  271. ->method('getSuffix')
  272. ->willReturn($customerSuffix);
  273. $newAddressMock->expects($this->once())
  274. ->method('setPrefix')
  275. ->with($customerPrefix)
  276. ->willReturnSelf();
  277. $newAddressMock->expects($this->once())
  278. ->method('setFirstname')
  279. ->with($customerFirstName)
  280. ->willReturnSelf();
  281. $newAddressMock->expects($this->once())
  282. ->method('setMiddlename')
  283. ->with($customerMiddlename)
  284. ->willReturnSelf();
  285. $newAddressMock->expects($this->once())
  286. ->method('setLastname')
  287. ->with($customerLastname)
  288. ->willReturnSelf();
  289. $newAddressMock->expects($this->once())
  290. ->method('setSuffix')
  291. ->with($customerSuffix)
  292. ->willReturnSelf();
  293. $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  294. ->disableOriginalConstructor()
  295. ->getMock();
  296. $this->pageConfigMock->expects($this->once())
  297. ->method('getTitle')
  298. ->willReturn($pageTitleMock);
  299. $this->model->setData('title', $title);
  300. $pageTitleMock->expects($this->once())
  301. ->method('set')
  302. ->with($title)
  303. ->willReturnSelf();
  304. $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
  305. $this->assertEquals($layoutMock, $this->model->getLayout());
  306. }
  307. public function testSetLayoutWithoutAddress()
  308. {
  309. $addressId = 1;
  310. $customerPrefix = 'prefix';
  311. $customerFirstName = 'firstname';
  312. $customerMiddlename = 'middlename';
  313. $customerLastname = 'lastname';
  314. $customerSuffix = 'suffix';
  315. $title = 'title';
  316. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  317. ->getMock();
  318. $this->requestMock->expects($this->once())
  319. ->method('getParam')
  320. ->with('id', null)
  321. ->willReturn($addressId);
  322. $this->addressRepositoryMock->expects($this->once())
  323. ->method('getById')
  324. ->with($addressId)
  325. ->willThrowException(
  326. \Magento\Framework\Exception\NoSuchEntityException::singleField('addressId', $addressId)
  327. );
  328. $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  329. ->getMock();
  330. $this->addressDataFactoryMock->expects($this->once())
  331. ->method('create')
  332. ->willReturn($newAddressMock);
  333. $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  334. ->getMock();
  335. $this->currentCustomerMock->expects($this->once())
  336. ->method('getCustomer')
  337. ->willReturn($customerMock);
  338. $customerMock->expects($this->once())
  339. ->method('getPrefix')
  340. ->willReturn($customerPrefix);
  341. $customerMock->expects($this->once())
  342. ->method('getFirstname')
  343. ->willReturn($customerFirstName);
  344. $customerMock->expects($this->once())
  345. ->method('getMiddlename')
  346. ->willReturn($customerMiddlename);
  347. $customerMock->expects($this->once())
  348. ->method('getLastname')
  349. ->willReturn($customerLastname);
  350. $customerMock->expects($this->once())
  351. ->method('getSuffix')
  352. ->willReturn($customerSuffix);
  353. $newAddressMock->expects($this->once())
  354. ->method('setPrefix')
  355. ->with($customerPrefix)
  356. ->willReturnSelf();
  357. $newAddressMock->expects($this->once())
  358. ->method('setFirstname')
  359. ->with($customerFirstName)
  360. ->willReturnSelf();
  361. $newAddressMock->expects($this->once())
  362. ->method('setMiddlename')
  363. ->with($customerMiddlename)
  364. ->willReturnSelf();
  365. $newAddressMock->expects($this->once())
  366. ->method('setLastname')
  367. ->with($customerLastname)
  368. ->willReturnSelf();
  369. $newAddressMock->expects($this->once())
  370. ->method('setSuffix')
  371. ->with($customerSuffix)
  372. ->willReturnSelf();
  373. $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  374. ->disableOriginalConstructor()
  375. ->getMock();
  376. $this->pageConfigMock->expects($this->once())
  377. ->method('getTitle')
  378. ->willReturn($pageTitleMock);
  379. $this->model->setData('title', $title);
  380. $pageTitleMock->expects($this->once())
  381. ->method('set')
  382. ->with($title)
  383. ->willReturnSelf();
  384. $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
  385. $this->assertEquals($layoutMock, $this->model->getLayout());
  386. }
  387. }