FormPostTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Address;
  7. use Magento\Customer\Api\AddressRepositoryInterface;
  8. use Magento\Customer\Api\Data\AddressInterface;
  9. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  10. use Magento\Customer\Api\Data\RegionInterface;
  11. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  12. use Magento\Customer\Controller\Address\FormPost;
  13. use Magento\Customer\Model\Metadata\Form;
  14. use Magento\Customer\Model\Metadata\FormFactory;
  15. use Magento\Customer\Model\Session;
  16. use Magento\Directory\Helper\Data as HelperData;
  17. use Magento\Directory\Model\Region;
  18. use Magento\Directory\Model\RegionFactory;
  19. use Magento\Framework\Api\DataObjectHelper;
  20. use Magento\Framework\App\Action\Context;
  21. use Magento\Framework\App\RequestInterface;
  22. use Magento\Framework\App\Response\RedirectInterface;
  23. use Magento\Framework\Controller\Result\ForwardFactory;
  24. use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
  25. use Magento\Framework\Controller\Result\RedirectFactory;
  26. use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
  27. use Magento\Framework\Exception\InputException;
  28. use Magento\Framework\Message\ManagerInterface;
  29. use Magento\Framework\ObjectManagerInterface;
  30. use Magento\Framework\Reflection\DataObjectProcessor;
  31. use Magento\Framework\View\Result\PageFactory;
  32. /**
  33. * @SuppressWarnings(PHPMD.TooManyFields)
  34. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  35. */
  36. class FormPostTest extends \PHPUnit\Framework\TestCase
  37. {
  38. /**
  39. * @var FormPost
  40. */
  41. protected $model;
  42. /**
  43. * @var Context |\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $context;
  46. /**
  47. * @var Session |\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $session;
  50. /**
  51. * @var FormKeyValidator |\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $formKeyValidator;
  54. /**
  55. * @var FormFactory |\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $formFactory;
  58. /**
  59. * @var AddressRepositoryInterface |\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $addressRepository;
  62. /**
  63. * @var AddressInterfaceFactory |\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $addressDataFactory;
  66. /**
  67. * @var RegionInterfaceFactory |\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $regionDataFactory;
  70. /**
  71. * @var DataObjectProcessor |\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $dataProcessor;
  74. /**
  75. * @var DataObjectHelper |\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $dataObjectHelper;
  78. /**
  79. * @var ForwardFactory |\PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $resultForwardFactory;
  82. /**
  83. * @var PageFactory |\PHPUnit_Framework_MockObject_MockObject
  84. */
  85. protected $resultPageFactory;
  86. /**
  87. * @var RegionFactory |\PHPUnit_Framework_MockObject_MockObject
  88. */
  89. protected $regionFactory;
  90. /**
  91. * @var RequestInterface |\PHPUnit_Framework_MockObject_MockObject
  92. */
  93. protected $request;
  94. /**
  95. * @var ResultRedirect |\PHPUnit_Framework_MockObject_MockObject
  96. */
  97. protected $resultRedirect;
  98. /**
  99. * @var RedirectFactory |\PHPUnit_Framework_MockObject_MockObject
  100. */
  101. protected $resultRedirectFactory;
  102. /**
  103. * @var RedirectInterface |\PHPUnit_Framework_MockObject_MockObject
  104. */
  105. protected $redirect;
  106. /**
  107. * @var ObjectManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  108. */
  109. protected $objectManager;
  110. /**
  111. * @var AddressInterface |\PHPUnit_Framework_MockObject_MockObject
  112. */
  113. protected $addressData;
  114. /**
  115. * @var RegionInterface |\PHPUnit_Framework_MockObject_MockObject
  116. */
  117. protected $regionData;
  118. /**
  119. * @var Form |\PHPUnit_Framework_MockObject_MockObject
  120. */
  121. protected $form;
  122. /**
  123. * @var HelperData |\PHPUnit_Framework_MockObject_MockObject
  124. */
  125. protected $helperData;
  126. /**
  127. * @var Region |\PHPUnit_Framework_MockObject_MockObject
  128. */
  129. protected $region;
  130. /**
  131. * @var ManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  132. */
  133. protected $messageManager;
  134. /**
  135. * @var \Magento\Customer\Model\Address\Mapper|\PHPUnit_Framework_MockObject_MockObject
  136. */
  137. private $customerAddressMapper;
  138. /**
  139. * {@inheritDoc}
  140. */
  141. protected function setUp()
  142. {
  143. $this->prepareContext();
  144. $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  145. ->disableOriginalConstructor()
  146. ->setMethods([
  147. 'setAddressFormData',
  148. 'getCustomerId',
  149. ])
  150. ->getMock();
  151. $this->formKeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $this->prepareForm();
  155. $this->prepareAddress();
  156. $this->prepareRegion();
  157. $this->dataProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $this->resultForwardFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $this->resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. $this->helperData = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $this->customerAddressMapper = $this->getMockBuilder(\Magento\Customer\Model\Address\Mapper::class)
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. $this->model = new FormPost(
  176. $this->context,
  177. $this->session,
  178. $this->formKeyValidator,
  179. $this->formFactory,
  180. $this->addressRepository,
  181. $this->addressDataFactory,
  182. $this->regionDataFactory,
  183. $this->dataProcessor,
  184. $this->dataObjectHelper,
  185. $this->resultForwardFactory,
  186. $this->resultPageFactory,
  187. $this->regionFactory,
  188. $this->helperData
  189. );
  190. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  191. $objectManager->setBackwardCompatibleProperty(
  192. $this->model,
  193. 'customerAddressMapper',
  194. $this->customerAddressMapper
  195. );
  196. }
  197. /**
  198. * Prepares context
  199. */
  200. protected function prepareContext(): void
  201. {
  202. $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  206. ->setMethods([
  207. 'isPost',
  208. 'getPostValue',
  209. 'getParam',
  210. ])
  211. ->getMockForAbstractClass();
  212. $this->context->expects($this->any())
  213. ->method('getRequest')
  214. ->willReturn($this->request);
  215. $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
  216. ->getMockForAbstractClass();
  217. $this->context->expects($this->any())
  218. ->method('getRedirect')
  219. ->willReturn($this->redirect);
  220. $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  221. ->disableOriginalConstructor()
  222. ->getMock();
  223. $this->resultRedirectFactory = $this->getMockBuilder(
  224. \Magento\Framework\Controller\Result\RedirectFactory::class
  225. )->disableOriginalConstructor()->getMock();
  226. $this->resultRedirectFactory->expects($this->any())
  227. ->method('create')
  228. ->willReturn($this->resultRedirect);
  229. $this->context->expects($this->any())
  230. ->method('getResultRedirectFactory')
  231. ->willReturn($this->resultRedirectFactory);
  232. $this->objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  233. ->getMockForAbstractClass();
  234. $this->context->expects($this->any())
  235. ->method('getObjectManager')
  236. ->willReturn($this->objectManager);
  237. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  238. ->getMockForAbstractClass();
  239. $this->context->expects($this->any())
  240. ->method('getMessageManager')
  241. ->willReturn($this->messageManager);
  242. }
  243. /**
  244. * Prepare address
  245. */
  246. protected function prepareAddress(): void
  247. {
  248. $this->addressRepository = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
  249. ->getMockForAbstractClass();
  250. $this->addressData = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  251. ->getMockForAbstractClass();
  252. $this->addressDataFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
  253. ->disableOriginalConstructor()
  254. ->setMethods([
  255. 'create',
  256. ])
  257. ->getMock();
  258. $this->addressDataFactory->expects($this->any())
  259. ->method('create')
  260. ->willReturn($this->addressData);
  261. }
  262. /**
  263. * Prepare region
  264. */
  265. protected function prepareRegion(): void
  266. {
  267. $this->region = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
  268. ->disableOriginalConstructor()
  269. ->setMethods([
  270. 'load',
  271. 'getCode',
  272. 'getDefaultName',
  273. ])
  274. ->getMock();
  275. $this->regionFactory = $this->getMockBuilder(\Magento\Directory\Model\RegionFactory::class)
  276. ->disableOriginalConstructor()
  277. ->getMock();
  278. $this->regionFactory->expects($this->any())
  279. ->method('create')
  280. ->willReturn($this->region);
  281. $this->regionData = $this->getMockBuilder(\Magento\Customer\Api\Data\RegionInterface::class)
  282. ->getMockForAbstractClass();
  283. $this->regionDataFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\RegionInterfaceFactory::class)
  284. ->disableOriginalConstructor()
  285. ->setMethods([
  286. 'create',
  287. ])
  288. ->getMock();
  289. $this->regionDataFactory->expects($this->any())
  290. ->method('create')
  291. ->willReturn($this->regionData);
  292. }
  293. /**
  294. * Prepare form
  295. */
  296. protected function prepareForm(): void
  297. {
  298. $this->form = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form::class)
  299. ->disableOriginalConstructor()
  300. ->getMock();
  301. $this->formFactory = $this->getMockBuilder(\Magento\Customer\Model\Metadata\FormFactory::class)
  302. ->disableOriginalConstructor()
  303. ->getMock();
  304. }
  305. /**
  306. * Test form without formKey
  307. */
  308. public function testExecuteNoFormKey(): void
  309. {
  310. $this->formKeyValidator->expects($this->once())
  311. ->method('validate')
  312. ->with($this->request)
  313. ->willReturn(false);
  314. $this->resultRedirect->expects($this->once())
  315. ->method('setPath')
  316. ->with('*/*/')
  317. ->willReturnSelf();
  318. $this->assertEquals($this->resultRedirect, $this->model->execute());
  319. }
  320. /**
  321. * Test executing without post data
  322. */
  323. public function testExecuteNoPostData(): void
  324. {
  325. $postValue = 'post_value';
  326. $url = 'url';
  327. $this->formKeyValidator->expects($this->once())
  328. ->method('validate')
  329. ->with($this->request)
  330. ->willReturn(true);
  331. $this->request->expects($this->once())
  332. ->method('isPost')
  333. ->willReturn(false);
  334. $this->request->expects($this->once())
  335. ->method('getPostValue')
  336. ->willReturn($postValue);
  337. $this->session->expects($this->once())
  338. ->method('setAddressFormData')
  339. ->with($postValue)
  340. ->willReturnSelf();
  341. $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  342. ->getMockForAbstractClass();
  343. $urlBuilder->expects($this->once())
  344. ->method('getUrl')
  345. ->with('*/*/edit', [])
  346. ->willReturn($url);
  347. $this->objectManager->expects($this->once())
  348. ->method('create')
  349. ->with(\Magento\Framework\UrlInterface::class)
  350. ->willReturn($urlBuilder);
  351. $this->redirect->expects($this->once())
  352. ->method('error')
  353. ->with($url)
  354. ->willReturn($url);
  355. $this->resultRedirect->expects($this->once())
  356. ->method('setUrl')
  357. ->with($url)
  358. ->willReturnSelf();
  359. $this->assertEquals($this->resultRedirect, $this->model->execute());
  360. }
  361. /**
  362. * Tests executing
  363. *
  364. * @param int $addressId
  365. * @param int $countryId
  366. * @param int $customerId
  367. * @param int $regionId
  368. * @param string $region
  369. * @param string $regionCode
  370. * @param int $newRegionId
  371. * @param string $newRegion
  372. * @param string $newRegionCode
  373. * @dataProvider dataProviderTestExecute
  374. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  375. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  376. */
  377. public function testExecute(
  378. $addressId,
  379. $countryId,
  380. $customerId,
  381. $regionId,
  382. $region,
  383. $regionCode,
  384. $newRegionId,
  385. $newRegion,
  386. $newRegionCode,
  387. $existingDefaultBilling = false,
  388. $existingDefaultShipping = false,
  389. $setDefaultBilling = false,
  390. $setDefaultShipping = false
  391. ): void {
  392. $existingAddressData = [
  393. 'country_id' => $countryId,
  394. 'region_id' => $regionId,
  395. 'region' => $region,
  396. 'region_code' => $regionCode,
  397. 'customer_id' => $customerId,
  398. 'default_billing' => $existingDefaultBilling,
  399. 'default_shipping' => $existingDefaultShipping,
  400. ];
  401. $newAddressData = [
  402. 'country_id' => $countryId,
  403. 'region_id' => $newRegionId,
  404. 'region' => $newRegion,
  405. 'region_code' => $newRegionCode,
  406. 'customer_id' => $customerId
  407. ];
  408. $url = 'success_url';
  409. $this->formKeyValidator->expects($this->once())
  410. ->method('validate')
  411. ->with($this->request)
  412. ->willReturn(true);
  413. $this->request->expects($this->once())
  414. ->method('isPost')
  415. ->willReturn(true);
  416. $this->request->expects($this->exactly(3))
  417. ->method('getParam')
  418. ->willReturnMap([
  419. ['id', null, $addressId],
  420. ['default_billing', $existingDefaultBilling, $setDefaultBilling],
  421. ['default_shipping', $existingDefaultShipping, $setDefaultShipping],
  422. ]);
  423. $this->addressRepository->expects($this->once())
  424. ->method('getById')
  425. ->with($addressId)
  426. ->willReturn($this->addressData);
  427. $this->addressRepository->expects($this->once())
  428. ->method('save')
  429. ->with($this->addressData)
  430. ->willReturnSelf();
  431. $this->customerAddressMapper->expects($this->once())
  432. ->method('toFlatArray')
  433. ->with($this->addressData)
  434. ->willReturn($existingAddressData);
  435. $this->formFactory->expects($this->once())
  436. ->method('create')
  437. ->with('customer_address', 'customer_address_edit', $existingAddressData)
  438. ->willReturn($this->form);
  439. $this->form->expects($this->once())
  440. ->method('extractData')
  441. ->with($this->request)
  442. ->willReturn($newAddressData);
  443. $this->form->expects($this->once())
  444. ->method('compactData')
  445. ->with($newAddressData)
  446. ->willReturn($newAddressData);
  447. $this->region->expects($this->any())
  448. ->method('load')
  449. ->with($newRegionId)
  450. ->willReturn($this->region);
  451. $this->region->expects($this->any())
  452. ->method('getCode')
  453. ->willReturn($newRegionCode);
  454. $this->region->expects($this->any())
  455. ->method('getDefaultName')
  456. ->willReturn($newRegion);
  457. $regionData = [
  458. RegionInterface::REGION_ID => !empty($newRegionId) ? $newRegionId : null,
  459. RegionInterface::REGION => !empty($newRegion) ? $newRegion : null,
  460. RegionInterface::REGION_CODE => !empty($newRegionCode) ? $newRegionCode : null,
  461. ];
  462. $this->dataObjectHelper->expects($this->exactly(2))
  463. ->method('populateWithArray')
  464. ->willReturnMap([
  465. [
  466. $this->regionData,
  467. $regionData,
  468. \Magento\Customer\Api\Data\RegionInterface::class,
  469. $this->dataObjectHelper,
  470. ],
  471. [
  472. $this->addressData,
  473. array_merge($existingAddressData, $newAddressData),
  474. \Magento\Customer\Api\Data\AddressInterface::class,
  475. $this->dataObjectHelper,
  476. ],
  477. ]);
  478. $this->session->expects($this->atLeastOnce())
  479. ->method('getCustomerId')
  480. ->willReturn($customerId);
  481. $this->addressData->expects($this->any())
  482. ->method('getCustomerId')
  483. ->willReturn($customerId);
  484. $this->addressData->expects($this->once())
  485. ->method('setCustomerId')
  486. ->with($customerId)
  487. ->willReturnSelf();
  488. $this->addressData->expects($this->once())
  489. ->method('setIsDefaultBilling')
  490. ->with($setDefaultBilling)
  491. ->willReturnSelf();
  492. $this->addressData->expects($this->once())
  493. ->method('setIsDefaultShipping')
  494. ->with($setDefaultShipping)
  495. ->willReturnSelf();
  496. $this->messageManager->expects($this->once())
  497. ->method('addSuccessMessage')
  498. ->with(__('You saved the address.'))
  499. ->willReturnSelf();
  500. $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  501. ->getMockForAbstractClass();
  502. $urlBuilder->expects($this->once())
  503. ->method('getUrl')
  504. ->with('*/*/index', ['_secure' => true])
  505. ->willReturn($url);
  506. $this->objectManager->expects($this->once())
  507. ->method('create')
  508. ->with(\Magento\Framework\UrlInterface::class)
  509. ->willReturn($urlBuilder);
  510. $this->redirect->expects($this->once())
  511. ->method('success')
  512. ->with($url)
  513. ->willReturn($url);
  514. $this->resultRedirect->expects($this->once())
  515. ->method('setUrl')
  516. ->with($url)
  517. ->willReturnSelf();
  518. $this->assertEquals($this->resultRedirect, $this->model->execute());
  519. }
  520. /**
  521. * @return array
  522. */
  523. public function dataProviderTestExecute(): array
  524. {
  525. return [
  526. [1, 1, 1, null, '', null, '', null, ''],
  527. [1, 1, 1, '', null, '', null, '', null],
  528. [1, 1, 1, null, null, null, 12, null, null],
  529. [1, 1, 1, null, null, null, 1, 'California', null],
  530. [1, 1, 1, null, null, null, 1, 'California', 'CA'],
  531. [1, 1, 1, null, null, null, 1, null, 'CA'],
  532. [1, 1, 1, null, null, null, null, null, 'CA'],
  533. [1, 1, 1, 2, null, null, null, null, null],
  534. [1, 1, 1, 2, 'Alaska', null, null, null, null],
  535. [1, 1, 1, 2, 'Alaska', 'AK', null, null, null],
  536. [1, 1, 1, 2, null, null, null, null, null],
  537. [1, 1, 1, 2, 'Alaska', null, null, null, null],
  538. [1, 1, 1, 2, 'Alaska', 'AK', null, null, null],
  539. [1, 1, 1, 2, null, null, 12, null, null],
  540. [1, 1, 1, 2, 'Alaska', null, 12, null, 'CA'],
  541. [1, 1, 1, 2, 'Alaska', 'AK', 12, 'California', null, true, true, true, false],
  542. [1, 1, 1, 2, null, null, 12, null, null, false, false, true, false],
  543. [1, 1, 1, 2, 'Alaska', null, 12, null, 'CA', true, false, true, false],
  544. [1, 1, 1, 2, 'Alaska', 'AK', 12, 'California', null, true, true, true, true],
  545. ];
  546. }
  547. /**
  548. * Tests input exception
  549. */
  550. public function testExecuteInputException(): void
  551. {
  552. $addressId = 1;
  553. $postValue = 'post_value';
  554. $url = 'result_url';
  555. $this->formKeyValidator->expects($this->once())
  556. ->method('validate')
  557. ->with($this->request)
  558. ->willReturn(true);
  559. $this->request->expects($this->once())
  560. ->method('isPost')
  561. ->willReturn(true);
  562. $this->request->expects($this->exactly(2))
  563. ->method('getParam')
  564. ->with('id')
  565. ->willReturn($addressId);
  566. $this->request->expects($this->once())
  567. ->method('getPostValue')
  568. ->willReturn($postValue);
  569. $this->addressRepository->expects($this->once())
  570. ->method('getById')
  571. ->with($addressId)
  572. ->willThrowException(new InputException(__('InputException')));
  573. $this->messageManager->expects($this->once())
  574. ->method('addErrorMessage')
  575. ->with('InputException')
  576. ->willReturnSelf();
  577. $this->session->expects($this->once())
  578. ->method('setAddressFormData')
  579. ->with($postValue)
  580. ->willReturnSelf();
  581. $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  582. ->getMockForAbstractClass();
  583. $urlBuilder->expects($this->once())
  584. ->method('getUrl')
  585. ->with('*/*/edit', ['id' => $addressId])
  586. ->willReturn($url);
  587. $this->objectManager->expects($this->once())
  588. ->method('create')
  589. ->with(\Magento\Framework\UrlInterface::class)
  590. ->willReturn($urlBuilder);
  591. $this->redirect->expects($this->once())
  592. ->method('error')
  593. ->with($url)
  594. ->willReturn($url);
  595. $this->resultRedirect->expects($this->once())
  596. ->method('setUrl')
  597. ->with($url)
  598. ->willReturnSelf();
  599. $this->assertEquals($this->resultRedirect, $this->model->execute());
  600. }
  601. /**
  602. * Tests exception
  603. */
  604. public function testExecuteException(): void
  605. {
  606. $addressId = 1;
  607. $postValue = 'post_value';
  608. $url = 'result_url';
  609. $this->formKeyValidator->expects($this->once())
  610. ->method('validate')
  611. ->with($this->request)
  612. ->willReturn(true);
  613. $this->request->expects($this->once())
  614. ->method('isPost')
  615. ->willReturn(true);
  616. $this->request->expects($this->once())
  617. ->method('getParam')
  618. ->with('id')
  619. ->willReturn($addressId);
  620. $this->request->expects($this->never())
  621. ->method('getPostValue')
  622. ->willReturn($postValue);
  623. $exception = new \Exception(__('Exception'));
  624. $this->addressRepository->expects($this->once())
  625. ->method('getById')
  626. ->with($addressId)
  627. ->willThrowException($exception);
  628. $this->messageManager->expects($this->once())
  629. ->method('addExceptionMessage')
  630. ->with($exception, __('We can\'t save the address.'))
  631. ->willReturnSelf();
  632. $this->session->expects($this->never())
  633. ->method('setAddressFormData')
  634. ->with($postValue)
  635. ->willReturnSelf();
  636. $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  637. ->getMockForAbstractClass();
  638. $urlBuilder->expects($this->once())
  639. ->method('getUrl')
  640. ->with('*/*/index')
  641. ->willReturn($url);
  642. $this->objectManager->expects($this->once())
  643. ->method('create')
  644. ->with(\Magento\Framework\UrlInterface::class)
  645. ->willReturn($urlBuilder);
  646. $this->redirect->expects($this->once())
  647. ->method('error')
  648. ->with($url)
  649. ->willReturn($url);
  650. $this->resultRedirect->expects($this->once())
  651. ->method('setUrl')
  652. ->with($url)
  653. ->willReturnSelf();
  654. $this->assertEquals($this->resultRedirect, $this->model->execute());
  655. }
  656. }