Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormPostTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Customer\Api\Data\AddressInterfaceFactory;
13 use Magento\Customer\Api\Data\RegionInterfaceFactory;
18 use Magento\Directory\Helper\Data as HelperData;
25 use Magento\Framework\Controller\Result\ForwardFactory;
28 use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
34 
39 class FormPostTest extends \PHPUnit\Framework\TestCase
40 {
44  protected $model;
45 
49  protected $context;
50 
54  protected $session;
55 
59  protected $formKeyValidator;
60 
64  protected $formFactory;
65 
69  protected $addressRepository;
70 
75 
79  protected $regionDataFactory;
80 
84  protected $dataProcessor;
85 
89  protected $dataObjectHelper;
90 
95 
99  protected $resultPageFactory;
100 
104  protected $regionFactory;
105 
109  protected $request;
110 
114  protected $resultRedirect;
115 
120 
124  protected $redirect;
125 
129  protected $objectManager;
130 
134  protected $addressData;
135 
139  protected $regionData;
140 
144  protected $form;
145 
149  protected $helperData;
150 
154  protected $region;
155 
159  protected $messageManager;
160 
164  private $customerAddressMapper;
165 
169  protected function setUp()
170  {
171  $this->prepareContext();
172 
173  $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
174  ->disableOriginalConstructor()
175  ->setMethods([
176  'setAddressFormData',
177  'getCustomerId',
178  ])
179  ->getMock();
180 
181  $this->formKeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
182  ->disableOriginalConstructor()
183  ->getMock();
184 
185  $this->prepareForm();
186  $this->prepareAddress();
187  $this->prepareRegion();
188 
189  $this->dataProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192 
193  $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196 
197  $this->resultForwardFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class)
198  ->disableOriginalConstructor()
199  ->getMock();
200 
201  $this->resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
202  ->disableOriginalConstructor()
203  ->getMock();
204 
205  $this->helperData = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
206  ->disableOriginalConstructor()
207  ->getMock();
208 
209  $this->customerAddressMapper = $this->getMockBuilder(\Magento\Customer\Model\Address\Mapper::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212 
213  $this->model = new FormPost(
214  $this->context,
215  $this->session,
216  $this->formKeyValidator,
217  $this->formFactory,
218  $this->addressRepository,
219  $this->addressDataFactory,
220  $this->regionDataFactory,
221  $this->dataProcessor,
222  $this->dataObjectHelper,
223  $this->resultForwardFactory,
224  $this->resultPageFactory,
225  $this->regionFactory,
226  $this->helperData
227  );
228 
229  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
230  $objectManager->setBackwardCompatibleProperty(
231  $this->model,
232  'customerAddressMapper',
233  $this->customerAddressMapper
234  );
235  }
236 
240  protected function prepareContext(): void
241  {
242  $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
243  ->disableOriginalConstructor()
244  ->getMock();
245 
246  $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
247  ->setMethods([
248  'isPost',
249  'getPostValue',
250  'getParam',
251  ])
252  ->getMockForAbstractClass();
253 
254  $this->context->expects($this->any())
255  ->method('getRequest')
256  ->willReturn($this->request);
257 
258  $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
259  ->getMockForAbstractClass();
260 
261  $this->context->expects($this->any())
262  ->method('getRedirect')
263  ->willReturn($this->redirect);
264 
265  $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
266  ->disableOriginalConstructor()
267  ->getMock();
268  $this->resultRedirectFactory = $this->getMockBuilder(
269  \Magento\Framework\Controller\Result\RedirectFactory::class
270  )->disableOriginalConstructor()->getMock();
271  $this->resultRedirectFactory->expects($this->any())
272  ->method('create')
273  ->willReturn($this->resultRedirect);
274 
275  $this->context->expects($this->any())
276  ->method('getResultRedirectFactory')
277  ->willReturn($this->resultRedirectFactory);
278 
279  $this->objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
280  ->getMockForAbstractClass();
281 
282  $this->context->expects($this->any())
283  ->method('getObjectManager')
284  ->willReturn($this->objectManager);
285 
286  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
287  ->getMockForAbstractClass();
288 
289  $this->context->expects($this->any())
290  ->method('getMessageManager')
291  ->willReturn($this->messageManager);
292  }
293 
297  protected function prepareAddress(): void
298  {
299  $this->addressRepository = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
300  ->getMockForAbstractClass();
301 
302  $this->addressData = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
303  ->getMockForAbstractClass();
304 
305  $this->addressDataFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
306  ->disableOriginalConstructor()
307  ->setMethods([
308  'create',
309  ])
310  ->getMock();
311  $this->addressDataFactory->expects($this->any())
312  ->method('create')
313  ->willReturn($this->addressData);
314  }
315 
319  protected function prepareRegion(): void
320  {
321  $this->region = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
322  ->disableOriginalConstructor()
323  ->setMethods([
324  'load',
325  'getCode',
326  'getDefaultName',
327  ])
328  ->getMock();
329 
330  $this->regionFactory = $this->getMockBuilder(\Magento\Directory\Model\RegionFactory::class)
331  ->disableOriginalConstructor()
332  ->getMock();
333  $this->regionFactory->expects($this->any())
334  ->method('create')
335  ->willReturn($this->region);
336 
337  $this->regionData = $this->getMockBuilder(\Magento\Customer\Api\Data\RegionInterface::class)
338  ->getMockForAbstractClass();
339 
340  $this->regionDataFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\RegionInterfaceFactory::class)
341  ->disableOriginalConstructor()
342  ->setMethods([
343  'create',
344  ])
345  ->getMock();
346  $this->regionDataFactory->expects($this->any())
347  ->method('create')
348  ->willReturn($this->regionData);
349  }
350 
354  protected function prepareForm(): void
355  {
356  $this->form = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form::class)
357  ->disableOriginalConstructor()
358  ->getMock();
359 
360  $this->formFactory = $this->getMockBuilder(\Magento\Customer\Model\Metadata\FormFactory::class)
361  ->disableOriginalConstructor()
362  ->getMock();
363  }
364 
368  public function testExecuteNoFormKey(): void
369  {
370  $this->formKeyValidator->expects($this->once())
371  ->method('validate')
372  ->with($this->request)
373  ->willReturn(false);
374 
375  $this->resultRedirect->expects($this->once())
376  ->method('setPath')
377  ->with('*/*/')
378  ->willReturnSelf();
379 
380  $this->assertEquals($this->resultRedirect, $this->model->execute());
381  }
382 
386  public function testExecuteNoPostData(): void
387  {
388  $postValue = 'post_value';
389  $url = 'url';
390 
391  $this->formKeyValidator->expects($this->once())
392  ->method('validate')
393  ->with($this->request)
394  ->willReturn(true);
395 
396  $this->request->expects($this->once())
397  ->method('isPost')
398  ->willReturn(false);
399  $this->request->expects($this->once())
400  ->method('getPostValue')
401  ->willReturn($postValue);
402 
403  $this->session->expects($this->once())
404  ->method('setAddressFormData')
405  ->with($postValue)
406  ->willReturnSelf();
407 
408  $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
409  ->getMockForAbstractClass();
410  $urlBuilder->expects($this->once())
411  ->method('getUrl')
412  ->with('*/*/edit', [])
413  ->willReturn($url);
414 
415  $this->objectManager->expects($this->once())
416  ->method('create')
417  ->with(\Magento\Framework\UrlInterface::class)
418  ->willReturn($urlBuilder);
419 
420  $this->redirect->expects($this->once())
421  ->method('error')
422  ->with($url)
423  ->willReturn($url);
424 
425  $this->resultRedirect->expects($this->once())
426  ->method('setUrl')
427  ->with($url)
428  ->willReturnSelf();
429 
430  $this->assertEquals($this->resultRedirect, $this->model->execute());
431  }
432 
449  public function testExecute(
450  $addressId,
451  $countryId,
452  $customerId,
453  $regionId,
454  $region,
455  $regionCode,
456  $newRegionId,
457  $newRegion,
458  $newRegionCode
459  ): void {
460  $existingAddressData = [
461  'country_id' => $countryId,
462  'region_id' => $regionId,
463  'region' => $region,
464  'region_code' => $regionCode,
465  'customer_id' => $customerId
466  ];
467  $newAddressData = [
468  'country_id' => $countryId,
469  'region_id' => $newRegionId,
470  'region' => $newRegion,
471  'region_code' => $newRegionCode,
472  'customer_id' => $customerId
473  ];
474 
475  $url = 'success_url';
476 
477  $this->formKeyValidator->expects($this->once())
478  ->method('validate')
479  ->with($this->request)
480  ->willReturn(true);
481 
482  $this->request->expects($this->once())
483  ->method('isPost')
484  ->willReturn(true);
485  $this->request->expects($this->exactly(3))
486  ->method('getParam')
487  ->willReturnMap([
488  ['id', null, $addressId],
489  ['default_billing', false, $addressId],
490  ['default_shipping', false, $addressId],
491  ]);
492 
493  $this->addressRepository->expects($this->once())
494  ->method('getById')
495  ->with($addressId)
496  ->willReturn($this->addressData);
497  $this->addressRepository->expects($this->once())
498  ->method('save')
499  ->with($this->addressData)
500  ->willReturnSelf();
501 
502  $this->customerAddressMapper->expects($this->once())
503  ->method('toFlatArray')
504  ->with($this->addressData)
505  ->willReturn($existingAddressData);
506 
507  $this->formFactory->expects($this->once())
508  ->method('create')
509  ->with('customer_address', 'customer_address_edit', $existingAddressData)
510  ->willReturn($this->form);
511 
512  $this->form->expects($this->once())
513  ->method('extractData')
514  ->with($this->request)
515  ->willReturn($newAddressData);
516  $this->form->expects($this->once())
517  ->method('compactData')
518  ->with($newAddressData)
519  ->willReturn($newAddressData);
520 
521  $this->region->expects($this->any())
522  ->method('load')
523  ->with($newRegionId)
524  ->willReturn($this->region);
525  $this->region->expects($this->any())
526  ->method('getCode')
527  ->willReturn($newRegionCode);
528  $this->region->expects($this->any())
529  ->method('getDefaultName')
530  ->willReturn($newRegion);
531 
532  $regionData = [
533  RegionInterface::REGION_ID => !empty($newRegionId) ? $newRegionId : null,
534  RegionInterface::REGION => !empty($newRegion) ? $newRegion : null,
535  RegionInterface::REGION_CODE => !empty($newRegionCode) ? $newRegionCode : null,
536  ];
537 
538  $this->dataObjectHelper->expects($this->exactly(2))
539  ->method('populateWithArray')
540  ->willReturnMap([
541  [
542  $this->regionData,
543  $regionData,
544  \Magento\Customer\Api\Data\RegionInterface::class,
545  $this->dataObjectHelper,
546  ],
547  [
548  $this->addressData,
549  array_merge($existingAddressData, $newAddressData),
550  \Magento\Customer\Api\Data\AddressInterface::class,
551  $this->dataObjectHelper,
552  ],
553  ]);
554 
555  $this->session->expects($this->atLeastOnce())
556  ->method('getCustomerId')
557  ->willReturn($customerId);
558  $this->addressData->expects($this->any())
559  ->method('getCustomerId')
560  ->willReturn($customerId);
561 
562  $this->addressData->expects($this->once())
563  ->method('setCustomerId')
564  ->with($customerId)
565  ->willReturnSelf();
566  $this->addressData->expects($this->once())
567  ->method('setIsDefaultBilling')
568  ->with()
569  ->willReturnSelf();
570  $this->addressData->expects($this->once())
571  ->method('setIsDefaultShipping')
572  ->with()
573  ->willReturnSelf();
574 
575  $this->messageManager->expects($this->once())
576  ->method('addSuccessMessage')
577  ->with(__('You saved the address.'))
578  ->willReturnSelf();
579 
580  $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
581  ->getMockForAbstractClass();
582  $urlBuilder->expects($this->once())
583  ->method('getUrl')
584  ->with('*/*/index', ['_secure' => true])
585  ->willReturn($url);
586 
587  $this->objectManager->expects($this->once())
588  ->method('create')
589  ->with(\Magento\Framework\UrlInterface::class)
590  ->willReturn($urlBuilder);
591 
592  $this->redirect->expects($this->once())
593  ->method('success')
594  ->with($url)
595  ->willReturn($url);
596 
597  $this->resultRedirect->expects($this->once())
598  ->method('setUrl')
599  ->with($url)
600  ->willReturnSelf();
601 
602  $this->assertEquals($this->resultRedirect, $this->model->execute());
603  }
604 
608  public function dataProviderTestExecute(): array
609  {
610  return [
611  [1, 1, 1, null, '', null, '', null, ''],
612  [1, 1, 1, '', null, '', null, '', null],
613 
614  [1, 1, 1, null, null, null, 12, null, null],
615  [1, 1, 1, null, null, null, 1, 'California', null],
616  [1, 1, 1, null, null, null, 1, 'California', 'CA'],
617 
618  [1, 1, 1, null, null, null, 1, null, 'CA'],
619  [1, 1, 1, null, null, null, null, null, 'CA'],
620 
621  [1, 1, 1, 2, null, null, null, null, null],
622  [1, 1, 1, 2, 'Alaska', null, null, null, null],
623  [1, 1, 1, 2, 'Alaska', 'AK', null, null, null],
624 
625  [1, 1, 1, 2, null, null, null, null, null],
626  [1, 1, 1, 2, 'Alaska', null, null, null, null],
627  [1, 1, 1, 2, 'Alaska', 'AK', null, null, null],
628 
629  [1, 1, 1, 2, null, null, 12, null, null],
630  [1, 1, 1, 2, 'Alaska', null, 12, null, 'CA'],
631  [1, 1, 1, 2, 'Alaska', 'AK', 12, 'California', null],
632 
633  [1, 1, 1, 2, null, null, 12, null, null],
634  [1, 1, 1, 2, 'Alaska', null, 12, null, 'CA'],
635  [1, 1, 1, 2, 'Alaska', 'AK', 12, 'California', null],
636  ];
637  }
638 
642  public function testExecuteInputException(): void
643  {
644  $addressId = 1;
645  $postValue = 'post_value';
646  $url = 'result_url';
647 
648  $this->formKeyValidator->expects($this->once())
649  ->method('validate')
650  ->with($this->request)
651  ->willReturn(true);
652 
653  $this->request->expects($this->once())
654  ->method('isPost')
655  ->willReturn(true);
656  $this->request->expects($this->exactly(2))
657  ->method('getParam')
658  ->with('id')
659  ->willReturn($addressId);
660  $this->request->expects($this->once())
661  ->method('getPostValue')
662  ->willReturn($postValue);
663 
664  $this->addressRepository->expects($this->once())
665  ->method('getById')
666  ->with($addressId)
667  ->willThrowException(new InputException(__('InputException')));
668 
669  $this->messageManager->expects($this->once())
670  ->method('addErrorMessage')
671  ->with('InputException')
672  ->willReturnSelf();
673 
674  $this->session->expects($this->once())
675  ->method('setAddressFormData')
676  ->with($postValue)
677  ->willReturnSelf();
678 
679  $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
680  ->getMockForAbstractClass();
681  $urlBuilder->expects($this->once())
682  ->method('getUrl')
683  ->with('*/*/edit', ['id' => $addressId])
684  ->willReturn($url);
685 
686  $this->objectManager->expects($this->once())
687  ->method('create')
688  ->with(\Magento\Framework\UrlInterface::class)
689  ->willReturn($urlBuilder);
690 
691  $this->redirect->expects($this->once())
692  ->method('error')
693  ->with($url)
694  ->willReturn($url);
695 
696  $this->resultRedirect->expects($this->once())
697  ->method('setUrl')
698  ->with($url)
699  ->willReturnSelf();
700 
701  $this->assertEquals($this->resultRedirect, $this->model->execute());
702  }
703 
707  public function testExecuteException(): void
708  {
709  $addressId = 1;
710  $postValue = 'post_value';
711  $url = 'result_url';
712 
713  $this->formKeyValidator->expects($this->once())
714  ->method('validate')
715  ->with($this->request)
716  ->willReturn(true);
717 
718  $this->request->expects($this->once())
719  ->method('isPost')
720  ->willReturn(true);
721  $this->request->expects($this->once())
722  ->method('getParam')
723  ->with('id')
724  ->willReturn($addressId);
725  $this->request->expects($this->never())
726  ->method('getPostValue')
727  ->willReturn($postValue);
728 
729  $exception = new \Exception(__('Exception'));
730  $this->addressRepository->expects($this->once())
731  ->method('getById')
732  ->with($addressId)
733  ->willThrowException($exception);
734 
735  $this->messageManager->expects($this->once())
736  ->method('addExceptionMessage')
737  ->with($exception, __('We can\'t save the address.'))
738  ->willReturnSelf();
739 
740  $this->session->expects($this->never())
741  ->method('setAddressFormData')
742  ->with($postValue)
743  ->willReturnSelf();
744 
745  $urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
746  ->getMockForAbstractClass();
747  $urlBuilder->expects($this->once())
748  ->method('getUrl')
749  ->with('*/*/index')
750  ->willReturn($url);
751 
752  $this->objectManager->expects($this->once())
753  ->method('create')
754  ->with(\Magento\Framework\UrlInterface::class)
755  ->willReturn($urlBuilder);
756 
757  $this->redirect->expects($this->once())
758  ->method('error')
759  ->with($url)
760  ->willReturn($url);
761 
762  $this->resultRedirect->expects($this->once())
763  ->method('setUrl')
764  ->with($url)
765  ->willReturnSelf();
766 
767  $this->assertEquals($this->resultRedirect, $this->model->execute());
768  }
769 }
$resultForwardFactory
$session
$helperData
$regionFactory
$formFactory
$addressDataFactory
__()
Definition: __.php:13
$resultRedirectFactory
testExecuteNoPostData()
jquery extjs ext tree mage adminhtml form
Definition: tree.phtml:41
$addressRepository
testExecuteNoFormKey()
prepareAddress()
$request
dataProviderTestExecute()
$regionData
$dataObjectHelper
$messageManager
$regionDataFactory
$region
$addressData
prepareContext()
setUp()
$model
$redirect
$objectManager
$resultPageFactory
testExecuteInputException()
$dataProcessor
$formKeyValidator
prepareForm()
Definition: FormPost.php:31
testExecute( $addressId, $countryId, $customerId, $regionId, $region, $regionCode, $newRegionId, $newRegion, $newRegionCode)
$context
$resultRedirect
$form
testExecuteException()
prepareRegion()