Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SendmailTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class SendmailTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $model;
17 
20 
22  protected $requestMock;
23 
25  protected $registryMock;
26 
28  protected $validatorMock;
29 
31  protected $sendFriendMock;
32 
35 
38 
41 
44 
46  protected $resultFactoryMock;
47 
49  protected $eventManagerMock;
50 
52  protected $redirectMock;
53 
55  protected $urlBuilderMock;
56 
57  protected function setUp()
58  {
59  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
60  ->setMethods(['getPost', 'getPostValue', 'getParam'])
61  ->getMockForAbstractClass();
62  $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->sendFriendMock = $this->getMockBuilder(\Magento\SendFriend\Model\SendFriend::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->productRepositoryMock = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
72  ->getMockForAbstractClass();
73  $this->categoryRepositoryMock = $this->getMockBuilder(\Magento\Catalog\Api\CategoryRepositoryInterface::class)
74  ->getMockForAbstractClass();
75  $this->catalogSessionMock = $this->getMockBuilder(\Magento\Catalog\Model\Session::class)
76  ->setMethods(['getSendfriendFormData', 'setSendfriendFormData'])
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
80  ->getMock();
81  $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
85  ->getMock();
86  $this->redirectMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
87  ->getMock();
88  $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
89  ->getMock();
90 
91  $this->objectManagerHelper = new ObjectManagerHelper($this);
92  $this->model = $this->objectManagerHelper->getObject(
93  \Magento\SendFriend\Controller\Product\Sendmail::class,
94  [
95  'request' => $this->requestMock,
96  'coreRegistry' => $this->registryMock,
97  'formKeyValidator' => $this->validatorMock,
98  'sendFriend' => $this->sendFriendMock,
99  'productRepository' => $this->productRepositoryMock,
100  'categoryRepository' => $this->categoryRepositoryMock,
101  'catalogSession' => $this->catalogSessionMock,
102  'messageManager' => $this->messageManagerMock,
103  'resultFactory' => $this->resultFactoryMock,
104  'eventManager' => $this->eventManagerMock,
105  'redirect' => $this->redirectMock,
106  'url' => $this->urlBuilderMock,
107  ]
108  );
109  }
110 
114  public function testExecute()
115  {
116  $productId = 11;
117  $categoryId = 5;
118  $sender = 'sender';
119  $recipients = 'recipients';
120  $formData = [
121  'sender' => $sender,
122  'recipients' => $recipients,
123  ];
124  $productUrl = 'product_url';
125 
127  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130 
131  $this->resultFactoryMock->expects($this->once())
132  ->method('create')
133  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
134  ->willReturn($redirectMock);
135 
136  $this->validatorMock->expects($this->once())
137  ->method('validate')
138  ->with($this->requestMock)
139  ->willReturn(true);
140 
141  $this->requestMock->expects($this->exactly(2))
142  ->method('getParam')
143  ->willReturnMap(
144  [
145  ['id', null, $productId],
146  ['cat_id', null, $categoryId],
147  ]
148  );
149 
151  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
152  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
153  ->getMockForAbstractClass();
154 
155  $this->productRepositoryMock->expects($this->once())
156  ->method('getById')
157  ->with($productId, false, null, false)
158  ->willReturn($productMock);
159 
160  $productMock->expects($this->once())
161  ->method('isVisibleInCatalog')
162  ->willReturn(true);
163 
165  $categoryMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\CategoryInterface::class)
166  ->getMockForAbstractClass();
167 
168  $this->categoryRepositoryMock->expects($this->once())
169  ->method('get')
170  ->with($categoryId, null)
171  ->willReturn($categoryMock);
172 
173  $productMock->expects($this->once())
174  ->method('setCategory')
175  ->with($categoryMock);
176 
177  $this->registryMock->expects($this->exactly(2))
178  ->method('register')
179  ->willReturnMap(
180  [
181  ['product', $productMock, false, null],
182  ['current_category', $categoryMock, false, null],
183  ]
184  );
185 
186  $this->requestMock->expects($this->once())
187  ->method('getPostValue')
188  ->willReturn($formData);
189 
190  $this->requestMock->expects($this->exactly(2))
191  ->method('getPost')
192  ->willReturnMap(
193  [
194  ['sender', $sender],
195  ['recipients', $recipients],
196  ]
197  );
198 
199  $this->sendFriendMock->expects($this->once())
200  ->method('setSender')
201  ->with($sender)
202  ->willReturnSelf();
203  $this->sendFriendMock->expects($this->once())
204  ->method('setRecipients')
205  ->with($recipients)
206  ->willReturnSelf();
207  $this->sendFriendMock->expects($this->once())
208  ->method('setProduct')
209  ->with($productMock)
210  ->willReturnSelf();
211  $this->sendFriendMock->expects($this->once())
212  ->method('validate')
213  ->willReturn(true);
214  $this->sendFriendMock->expects($this->once())
215  ->method('send')
216  ->willReturnSelf();
217 
218  $this->messageManagerMock->expects($this->once())
219  ->method('addSuccess')
220  ->with(__('The link to a friend was sent.'))
221  ->willReturnSelf();
222 
223  $productMock->expects($this->once())
224  ->method('getProductUrl')
225  ->willReturn($productUrl);
226 
227  $this->redirectMock->expects($this->once())
228  ->method('success')
229  ->with($productUrl)
230  ->willReturnArgument(0);
231 
232  $redirectMock->expects($this->once())
233  ->method('setUrl')
234  ->with($productUrl)
235  ->willReturnSelf();
236 
237  $this->assertEquals($redirectMock, $this->model->execute());
238  }
239 
243  public function testExecuteWithoutValidationAndCategory()
244  {
245  $productId = 11;
246  $categoryId = 5;
247  $sender = 'sender';
248  $recipients = 'recipients';
249  $formData = [
250  'sender' => $sender,
251  'recipients' => $recipients,
252  ];
253  $redirectUrl = 'redirect_url';
254 
256  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
257  ->disableOriginalConstructor()
258  ->getMock();
259 
260  $this->resultFactoryMock->expects($this->once())
261  ->method('create')
262  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
263  ->willReturn($redirectMock);
264 
265  $this->validatorMock->expects($this->once())
266  ->method('validate')
267  ->with($this->requestMock)
268  ->willReturn(true);
269 
270  $this->requestMock->expects($this->exactly(2))
271  ->method('getParam')
272  ->willReturnMap(
273  [
274  ['id', null, $productId],
275  ['cat_id', null, $categoryId],
276  ]
277  );
278 
280  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
281  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
282  ->getMockForAbstractClass();
283 
284  $this->productRepositoryMock->expects($this->once())
285  ->method('getById')
286  ->with($productId, false, null, false)
287  ->willReturn($productMock);
288 
289  $productMock->expects($this->once())
290  ->method('isVisibleInCatalog')
291  ->willReturn(true);
292 
293  $this->categoryRepositoryMock->expects($this->once())
294  ->method('get')
295  ->with($categoryId, null)
296  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('No Category Exception.')));
297 
298  $productMock->expects($this->never())
299  ->method('setCategory');
300 
301  $this->registryMock->expects($this->once())
302  ->method('register')
303  ->willReturnMap(
304  [
305  ['product', $productMock, false, null],
306  ]
307  );
308 
309  $this->requestMock->expects($this->once())
310  ->method('getPostValue')
311  ->willReturn($formData);
312 
313  $this->requestMock->expects($this->exactly(2))
314  ->method('getPost')
315  ->willReturnMap(
316  [
317  ['sender', $sender],
318  ['recipients', $recipients],
319  ]
320  );
321 
322  $this->sendFriendMock->expects($this->once())
323  ->method('setSender')
324  ->with($sender)
325  ->willReturnSelf();
326  $this->sendFriendMock->expects($this->once())
327  ->method('setRecipients')
328  ->with($recipients)
329  ->willReturnSelf();
330  $this->sendFriendMock->expects($this->once())
331  ->method('setProduct')
332  ->with($productMock)
333  ->willReturnSelf();
334  $this->sendFriendMock->expects($this->once())
335  ->method('validate')
336  ->willReturn(['Some error']);
337  $this->sendFriendMock->expects($this->never())
338  ->method('send');
339 
340  $this->messageManagerMock->expects($this->once())
341  ->method('addError')
342  ->with(__('Some error'))
343  ->willReturnSelf();
344 
345  $this->catalogSessionMock->expects($this->once())
346  ->method('setSendfriendFormData')
347  ->with($formData);
348 
349  $this->urlBuilderMock->expects($this->once())
350  ->method('getUrl')
351  ->with('sendfriend/product/send', ['_current' => true])
352  ->willReturn($redirectUrl);
353 
354  $this->redirectMock->expects($this->once())
355  ->method('error')
356  ->with($redirectUrl)
357  ->willReturnArgument(0);
358 
359  $redirectMock->expects($this->once())
360  ->method('setUrl')
361  ->with($redirectUrl)
362  ->willReturnSelf();
363 
364  $this->assertEquals($redirectMock, $this->model->execute());
365  }
366 
370  public function testExecuteWithoutValidationAndCategoryWithProblems()
371  {
372  $productId = 11;
373  $categoryId = 5;
374  $sender = 'sender';
375  $recipients = 'recipients';
376  $formData = [
377  'sender' => $sender,
378  'recipients' => $recipients,
379  ];
380  $redirectUrl = 'redirect_url';
381 
383  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
384  ->disableOriginalConstructor()
385  ->getMock();
386 
387  $this->resultFactoryMock->expects($this->once())
388  ->method('create')
389  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
390  ->willReturn($redirectMock);
391 
392  $this->validatorMock->expects($this->once())
393  ->method('validate')
394  ->with($this->requestMock)
395  ->willReturn(true);
396 
397  $this->requestMock->expects($this->exactly(2))
398  ->method('getParam')
399  ->willReturnMap(
400  [
401  ['id', null, $productId],
402  ['cat_id', null, $categoryId],
403  ]
404  );
405 
407  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
408  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
409  ->getMockForAbstractClass();
410 
411  $this->productRepositoryMock->expects($this->once())
412  ->method('getById')
413  ->with($productId, false, null, false)
414  ->willReturn($productMock);
415 
416  $productMock->expects($this->once())
417  ->method('isVisibleInCatalog')
418  ->willReturn(true);
419 
420  $this->categoryRepositoryMock->expects($this->once())
421  ->method('get')
422  ->with($categoryId, null)
423  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('No Category Exception.')));
424 
425  $productMock->expects($this->never())
426  ->method('setCategory');
427 
428  $this->registryMock->expects($this->once())
429  ->method('register')
430  ->willReturnMap(
431  [
432  ['product', $productMock, false, null],
433  ]
434  );
435 
436  $this->requestMock->expects($this->once())
437  ->method('getPostValue')
438  ->willReturn($formData);
439 
440  $this->requestMock->expects($this->exactly(2))
441  ->method('getPost')
442  ->willReturnMap(
443  [
444  ['sender', $sender],
445  ['recipients', $recipients],
446  ]
447  );
448 
449  $this->sendFriendMock->expects($this->once())
450  ->method('setSender')
451  ->with($sender)
452  ->willReturnSelf();
453  $this->sendFriendMock->expects($this->once())
454  ->method('setRecipients')
455  ->with($recipients)
456  ->willReturnSelf();
457  $this->sendFriendMock->expects($this->once())
458  ->method('setProduct')
459  ->with($productMock)
460  ->willReturnSelf();
461  $this->sendFriendMock->expects($this->once())
462  ->method('validate')
463  ->willReturn('Some error');
464  $this->sendFriendMock->expects($this->never())
465  ->method('send');
466 
467  $this->messageManagerMock->expects($this->once())
468  ->method('addError')
469  ->with(__('We found some problems with the data.'))
470  ->willReturnSelf();
471 
472  $this->catalogSessionMock->expects($this->once())
473  ->method('setSendfriendFormData')
474  ->with($formData);
475 
476  $this->urlBuilderMock->expects($this->once())
477  ->method('getUrl')
478  ->with('sendfriend/product/send', ['_current' => true])
479  ->willReturn($redirectUrl);
480 
481  $this->redirectMock->expects($this->once())
482  ->method('error')
483  ->with($redirectUrl)
484  ->willReturnArgument(0);
485 
486  $redirectMock->expects($this->once())
487  ->method('setUrl')
488  ->with($redirectUrl)
489  ->willReturnSelf();
490 
491  $this->assertEquals($redirectMock, $this->model->execute());
492  }
493 
497  public function testExecuteWithLocalizedException()
498  {
499  $productId = 11;
500  $categoryId = 5;
501  $sender = 'sender';
502  $recipients = 'recipients';
503  $formData = [
504  'sender' => $sender,
505  'recipients' => $recipients,
506  ];
507  $redirectUrl = 'redirect_url';
508 
510  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
511  ->disableOriginalConstructor()
512  ->getMock();
513 
514  $this->resultFactoryMock->expects($this->once())
515  ->method('create')
516  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
517  ->willReturn($redirectMock);
518 
519  $this->validatorMock->expects($this->once())
520  ->method('validate')
521  ->with($this->requestMock)
522  ->willReturn(true);
523 
524  $this->requestMock->expects($this->exactly(2))
525  ->method('getParam')
526  ->willReturnMap(
527  [
528  ['id', null, $productId],
529  ['cat_id', null, $categoryId],
530  ]
531  );
532 
534  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
535  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
536  ->getMockForAbstractClass();
537 
538  $this->productRepositoryMock->expects($this->once())
539  ->method('getById')
540  ->with($productId, false, null, false)
541  ->willReturn($productMock);
542 
543  $productMock->expects($this->once())
544  ->method('isVisibleInCatalog')
545  ->willReturn(true);
546 
547  $this->categoryRepositoryMock->expects($this->once())
548  ->method('get')
549  ->with($categoryId, null)
550  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('No Category Exception.')));
551 
552  $productMock->expects($this->never())
553  ->method('setCategory');
554 
555  $this->registryMock->expects($this->once())
556  ->method('register')
557  ->willReturnMap(
558  [
559  ['product', $productMock, false, null],
560  ]
561  );
562 
563  $this->requestMock->expects($this->once())
564  ->method('getPostValue')
565  ->willReturn($formData);
566 
567  $this->requestMock->expects($this->exactly(2))
568  ->method('getPost')
569  ->willReturnMap(
570  [
571  ['sender', $sender],
572  ['recipients', $recipients],
573  ]
574  );
575 
576  $this->sendFriendMock->expects($this->once())
577  ->method('setSender')
578  ->with($sender)
579  ->willReturnSelf();
580  $this->sendFriendMock->expects($this->once())
581  ->method('setRecipients')
582  ->with($recipients)
583  ->willReturnSelf();
584  $this->sendFriendMock->expects($this->once())
585  ->method('setProduct')
586  ->with($productMock)
587  ->willReturnSelf();
588  $this->sendFriendMock->expects($this->once())
589  ->method('validate')
590  ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Localized Exception.')));
591  $this->sendFriendMock->expects($this->never())
592  ->method('send');
593 
594  $this->messageManagerMock->expects($this->once())
595  ->method('addError')
596  ->with(__('Localized Exception.'))
597  ->willReturnSelf();
598 
599  $this->catalogSessionMock->expects($this->once())
600  ->method('setSendfriendFormData')
601  ->with($formData);
602 
603  $this->urlBuilderMock->expects($this->once())
604  ->method('getUrl')
605  ->with('sendfriend/product/send', ['_current' => true])
606  ->willReturn($redirectUrl);
607 
608  $this->redirectMock->expects($this->once())
609  ->method('error')
610  ->with($redirectUrl)
611  ->willReturnArgument(0);
612 
613  $redirectMock->expects($this->once())
614  ->method('setUrl')
615  ->with($redirectUrl)
616  ->willReturnSelf();
617 
618  $this->assertEquals($redirectMock, $this->model->execute());
619  }
620 
624  public function testExecuteWithException()
625  {
626  $productId = 11;
627  $categoryId = 5;
628  $sender = 'sender';
629  $recipients = 'recipients';
630  $formData = [
631  'sender' => $sender,
632  'recipients' => $recipients,
633  ];
634  $redirectUrl = 'redirect_url';
635 
637  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
638  ->disableOriginalConstructor()
639  ->getMock();
640 
641  $this->resultFactoryMock->expects($this->once())
642  ->method('create')
643  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
644  ->willReturn($redirectMock);
645 
646  $this->validatorMock->expects($this->once())
647  ->method('validate')
648  ->with($this->requestMock)
649  ->willReturn(true);
650 
651  $this->requestMock->expects($this->exactly(2))
652  ->method('getParam')
653  ->willReturnMap(
654  [
655  ['id', null, $productId],
656  ['cat_id', null, $categoryId],
657  ]
658  );
659 
661  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
662  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
663  ->getMockForAbstractClass();
664 
665  $this->productRepositoryMock->expects($this->once())
666  ->method('getById')
667  ->with($productId, false, null, false)
668  ->willReturn($productMock);
669 
670  $productMock->expects($this->once())
671  ->method('isVisibleInCatalog')
672  ->willReturn(true);
673 
674  $this->categoryRepositoryMock->expects($this->once())
675  ->method('get')
676  ->with($categoryId, null)
677  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('No Category Exception.')));
678 
679  $productMock->expects($this->never())
680  ->method('setCategory');
681 
682  $this->registryMock->expects($this->once())
683  ->method('register')
684  ->willReturnMap(
685  [
686  ['product', $productMock, false, null],
687  ]
688  );
689 
690  $this->requestMock->expects($this->once())
691  ->method('getPostValue')
692  ->willReturn($formData);
693 
694  $this->requestMock->expects($this->exactly(2))
695  ->method('getPost')
696  ->willReturnMap(
697  [
698  ['sender', $sender],
699  ['recipients', $recipients],
700  ]
701  );
702 
703  $this->sendFriendMock->expects($this->once())
704  ->method('setSender')
705  ->with($sender)
706  ->willReturnSelf();
707  $this->sendFriendMock->expects($this->once())
708  ->method('setRecipients')
709  ->with($recipients)
710  ->willReturnSelf();
711  $this->sendFriendMock->expects($this->once())
712  ->method('setProduct')
713  ->with($productMock)
714  ->willReturnSelf();
715  $exception = new \Exception(__('Exception.'));
716  $this->sendFriendMock->expects($this->once())
717  ->method('validate')
718  ->willThrowException($exception);
719  $this->sendFriendMock->expects($this->never())
720  ->method('send');
721 
722  $this->messageManagerMock->expects($this->once())
723  ->method('addException')
724  ->with($exception, __('Some emails were not sent.'))
725  ->willReturnSelf();
726 
727  $this->catalogSessionMock->expects($this->once())
728  ->method('setSendfriendFormData')
729  ->with($formData);
730 
731  $this->urlBuilderMock->expects($this->once())
732  ->method('getUrl')
733  ->with('sendfriend/product/send', ['_current' => true])
734  ->willReturn($redirectUrl);
735 
736  $this->redirectMock->expects($this->once())
737  ->method('error')
738  ->with($redirectUrl)
739  ->willReturnArgument(0);
740 
741  $redirectMock->expects($this->once())
742  ->method('setUrl')
743  ->with($redirectUrl)
744  ->willReturnSelf();
745 
746  $this->assertEquals($redirectMock, $this->model->execute());
747  }
748 
752  public function testExecuteWithoutProduct()
753  {
754  $sender = 'sender';
755  $recipients = 'recipients';
756  $formData = [
757  'sender' => $sender,
758  'recipients' => $recipients,
759  ];
760 
762  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
763  ->disableOriginalConstructor()
764  ->getMock();
765 
767  $forwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class)
768  ->disableOriginalConstructor()
769  ->getMock();
770 
771  $this->resultFactoryMock->expects($this->exactly(2))
772  ->method('create')
773  ->willReturnMap(
774  [
775  [\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [], $redirectMock],
776  [\Magento\Framework\Controller\ResultFactory::TYPE_FORWARD, [], $forwardMock],
777  ]
778  );
779 
780  $this->validatorMock->expects($this->once())
781  ->method('validate')
782  ->with($this->requestMock)
783  ->willReturn(true);
784 
785  $this->requestMock->expects($this->once())
786  ->method('getParam')
787  ->willReturnMap(
788  [
789  ['id', null, null],
790  ]
791  );
792 
793  $this->requestMock->expects($this->once())
794  ->method('getPostValue')
795  ->willReturn($formData);
796 
797  $forwardMock->expects($this->once())
798  ->method('forward')
799  ->with('noroute')
800  ->willReturnSelf();
801 
802  $this->assertEquals($forwardMock, $this->model->execute());
803  }
804 
808  public function testExecuteWithoutData()
809  {
810  $productId = 11;
811  $formData = '';
812 
814  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
815  ->disableOriginalConstructor()
816  ->getMock();
817 
819  $forwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class)
820  ->disableOriginalConstructor()
821  ->getMock();
822 
823  $this->resultFactoryMock->expects($this->exactly(2))
824  ->method('create')
825  ->willReturnMap(
826  [
827  [\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [], $redirectMock],
828  [\Magento\Framework\Controller\ResultFactory::TYPE_FORWARD, [], $forwardMock],
829  ]
830  );
831 
832  $this->validatorMock->expects($this->once())
833  ->method('validate')
834  ->with($this->requestMock)
835  ->willReturn(true);
836 
837  $this->requestMock->expects($this->once())
838  ->method('getParam')
839  ->willReturnMap(
840  [
841  ['id', null, $productId],
842  ]
843  );
844 
846  $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
847  ->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])
848  ->getMockForAbstractClass();
849 
850  $this->productRepositoryMock->expects($this->once())
851  ->method('getById')
852  ->with($productId, false, null, false)
853  ->willReturn($productMock);
854 
855  $productMock->expects($this->once())
856  ->method('isVisibleInCatalog')
857  ->willReturn(true);
858 
859  $this->registryMock->expects($this->once())
860  ->method('register')
861  ->willReturnMap(
862  [
863  ['product', $productMock, false, null],
864  ]
865  );
866 
867  $this->requestMock->expects($this->once())
868  ->method('getPostValue')
869  ->willReturn($formData);
870 
871  $forwardMock->expects($this->once())
872  ->method('forward')
873  ->with('noroute')
874  ->willReturnSelf();
875 
876  $this->assertEquals($forwardMock, $this->model->execute());
877  }
878 
879  public function testExecuteWithoutFormKey()
880  {
882  $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
883  ->disableOriginalConstructor()
884  ->getMock();
885 
886  $this->resultFactoryMock->expects($this->once())
887  ->method('create')
888  ->willReturnMap(
889  [
890  [\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [], $redirectMock],
891  ]
892  );
893 
894  $this->validatorMock->expects($this->once())
895  ->method('validate')
896  ->with($this->requestMock)
897  ->willReturn(false);
898 
899  $redirectMock->expects($this->once())
900  ->method('setPath')
901  ->with('sendfriend/product/send', ['_current' => true])
902  ->willReturnSelf();
903 
904  $this->assertEquals($redirectMock, $this->model->execute());
905  }
906 }
__()
Definition: __.php:13