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()
65 $this->validatorMock = $this->getMockBuilder(\
Magento\Framework\Data\Form\FormKey\Validator::class)
66 ->disableOriginalConstructor()
68 $this->sendFriendMock = $this->getMockBuilder(\
Magento\SendFriend\Model\SendFriend::class)
69 ->disableOriginalConstructor()
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()
79 $this->messageManagerMock = $this->getMockBuilder(\
Magento\Framework\Message\ManagerInterface::class)
81 $this->resultFactoryMock = $this->getMockBuilder(\
Magento\Framework\Controller\ResultFactory::class)
82 ->disableOriginalConstructor()
84 $this->eventManagerMock = $this->getMockBuilder(\
Magento\Framework\Event\ManagerInterface::class)
86 $this->redirectMock = $this->getMockBuilder(\
Magento\Framework\
App\
Response\RedirectInterface::class)
88 $this->urlBuilderMock = $this->getMockBuilder(\
Magento\Framework\UrlInterface::class)
91 $this->objectManagerHelper =
new ObjectManagerHelper($this);
92 $this->model = $this->objectManagerHelper->getObject(
93 \
Magento\SendFriend\Controller\Product\Sendmail::class,
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,
114 public function testExecute()
119 $recipients =
'recipients';
122 'recipients' => $recipients,
124 $productUrl =
'product_url';
128 ->disableOriginalConstructor()
131 $this->resultFactoryMock->expects($this->once())
133 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
136 $this->validatorMock->expects($this->once())
138 ->with($this->requestMock)
141 $this->requestMock->expects($this->exactly(2))
146 [
'cat_id',
null, $categoryId],
151 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
152 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
153 ->getMockForAbstractClass();
155 $this->productRepositoryMock->expects($this->once())
158 ->willReturn($productMock);
160 $productMock->expects($this->once())
161 ->method(
'isVisibleInCatalog')
165 $categoryMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\CategoryInterface::class)
166 ->getMockForAbstractClass();
168 $this->categoryRepositoryMock->expects($this->once())
170 ->with($categoryId,
null)
171 ->willReturn($categoryMock);
173 $productMock->expects($this->once())
174 ->method(
'setCategory')
175 ->with($categoryMock);
177 $this->registryMock->expects($this->exactly(2))
181 [
'product', $productMock,
false,
null],
182 [
'current_category', $categoryMock,
false,
null],
186 $this->requestMock->expects($this->once())
187 ->method(
'getPostValue')
188 ->willReturn($formData);
190 $this->requestMock->expects($this->exactly(2))
195 [
'recipients', $recipients],
199 $this->sendFriendMock->expects($this->once())
200 ->method(
'setSender')
203 $this->sendFriendMock->expects($this->once())
204 ->method(
'setRecipients')
207 $this->sendFriendMock->expects($this->once())
208 ->method(
'setProduct')
211 $this->sendFriendMock->expects($this->once())
214 $this->sendFriendMock->expects($this->once())
218 $this->messageManagerMock->expects($this->once())
219 ->method(
'addSuccess')
220 ->with(
__(
'The link to a friend was sent.'))
223 $productMock->expects($this->once())
224 ->method(
'getProductUrl')
225 ->willReturn($productUrl);
227 $this->redirectMock->expects($this->once())
230 ->willReturnArgument(0);
243 public function testExecuteWithoutValidationAndCategory()
248 $recipients =
'recipients';
251 'recipients' => $recipients,
253 $redirectUrl =
'redirect_url';
257 ->disableOriginalConstructor()
260 $this->resultFactoryMock->expects($this->once())
262 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
265 $this->validatorMock->expects($this->once())
267 ->with($this->requestMock)
270 $this->requestMock->expects($this->exactly(2))
275 [
'cat_id',
null, $categoryId],
280 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
281 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
282 ->getMockForAbstractClass();
284 $this->productRepositoryMock->expects($this->once())
287 ->willReturn($productMock);
289 $productMock->expects($this->once())
290 ->method(
'isVisibleInCatalog')
293 $this->categoryRepositoryMock->expects($this->once())
295 ->with($categoryId,
null)
296 ->willThrowException(
new \
Magento\Framework\Exception\NoSuchEntityException(
__(
'No Category Exception.')));
298 $productMock->expects($this->never())
299 ->method(
'setCategory');
301 $this->registryMock->expects($this->once())
305 [
'product', $productMock,
false,
null],
309 $this->requestMock->expects($this->once())
310 ->method(
'getPostValue')
311 ->willReturn($formData);
313 $this->requestMock->expects($this->exactly(2))
318 [
'recipients', $recipients],
322 $this->sendFriendMock->expects($this->once())
323 ->method(
'setSender')
326 $this->sendFriendMock->expects($this->once())
327 ->method(
'setRecipients')
330 $this->sendFriendMock->expects($this->once())
331 ->method(
'setProduct')
334 $this->sendFriendMock->expects($this->once())
336 ->willReturn([
'Some error']);
337 $this->sendFriendMock->expects($this->never())
340 $this->messageManagerMock->expects($this->once())
342 ->with(
__(
'Some error'))
345 $this->catalogSessionMock->expects($this->once())
346 ->method(
'setSendfriendFormData')
349 $this->urlBuilderMock->expects($this->once())
351 ->with(
'sendfriend/product/send', [
'_current' =>
true])
352 ->willReturn($redirectUrl);
354 $this->redirectMock->expects($this->once())
357 ->willReturnArgument(0);
370 public function testExecuteWithoutValidationAndCategoryWithProblems()
375 $recipients =
'recipients';
378 'recipients' => $recipients,
380 $redirectUrl =
'redirect_url';
384 ->disableOriginalConstructor()
387 $this->resultFactoryMock->expects($this->once())
389 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
392 $this->validatorMock->expects($this->once())
394 ->with($this->requestMock)
397 $this->requestMock->expects($this->exactly(2))
402 [
'cat_id',
null, $categoryId],
407 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
408 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
409 ->getMockForAbstractClass();
411 $this->productRepositoryMock->expects($this->once())
414 ->willReturn($productMock);
416 $productMock->expects($this->once())
417 ->method(
'isVisibleInCatalog')
420 $this->categoryRepositoryMock->expects($this->once())
422 ->with($categoryId,
null)
423 ->willThrowException(
new \
Magento\Framework\Exception\NoSuchEntityException(
__(
'No Category Exception.')));
425 $productMock->expects($this->never())
426 ->method(
'setCategory');
428 $this->registryMock->expects($this->once())
432 [
'product', $productMock,
false,
null],
436 $this->requestMock->expects($this->once())
437 ->method(
'getPostValue')
438 ->willReturn($formData);
440 $this->requestMock->expects($this->exactly(2))
445 [
'recipients', $recipients],
449 $this->sendFriendMock->expects($this->once())
450 ->method(
'setSender')
453 $this->sendFriendMock->expects($this->once())
454 ->method(
'setRecipients')
457 $this->sendFriendMock->expects($this->once())
458 ->method(
'setProduct')
461 $this->sendFriendMock->expects($this->once())
463 ->willReturn(
'Some error');
464 $this->sendFriendMock->expects($this->never())
467 $this->messageManagerMock->expects($this->once())
469 ->with(
__(
'We found some problems with the data.'))
472 $this->catalogSessionMock->expects($this->once())
473 ->method(
'setSendfriendFormData')
476 $this->urlBuilderMock->expects($this->once())
478 ->with(
'sendfriend/product/send', [
'_current' =>
true])
479 ->willReturn($redirectUrl);
481 $this->redirectMock->expects($this->once())
484 ->willReturnArgument(0);
497 public function testExecuteWithLocalizedException()
502 $recipients =
'recipients';
505 'recipients' => $recipients,
507 $redirectUrl =
'redirect_url';
511 ->disableOriginalConstructor()
514 $this->resultFactoryMock->expects($this->once())
516 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
519 $this->validatorMock->expects($this->once())
521 ->with($this->requestMock)
524 $this->requestMock->expects($this->exactly(2))
529 [
'cat_id',
null, $categoryId],
534 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
535 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
536 ->getMockForAbstractClass();
538 $this->productRepositoryMock->expects($this->once())
541 ->willReturn($productMock);
543 $productMock->expects($this->once())
544 ->method(
'isVisibleInCatalog')
547 $this->categoryRepositoryMock->expects($this->once())
549 ->with($categoryId,
null)
550 ->willThrowException(
new \
Magento\Framework\Exception\NoSuchEntityException(
__(
'No Category Exception.')));
552 $productMock->expects($this->never())
553 ->method(
'setCategory');
555 $this->registryMock->expects($this->once())
559 [
'product', $productMock,
false,
null],
563 $this->requestMock->expects($this->once())
564 ->method(
'getPostValue')
565 ->willReturn($formData);
567 $this->requestMock->expects($this->exactly(2))
572 [
'recipients', $recipients],
576 $this->sendFriendMock->expects($this->once())
577 ->method(
'setSender')
580 $this->sendFriendMock->expects($this->once())
581 ->method(
'setRecipients')
584 $this->sendFriendMock->expects($this->once())
585 ->method(
'setProduct')
588 $this->sendFriendMock->expects($this->once())
590 ->willThrowException(
new \
Magento\Framework\Exception\LocalizedException(
__(
'Localized Exception.')));
591 $this->sendFriendMock->expects($this->never())
594 $this->messageManagerMock->expects($this->once())
596 ->with(
__(
'Localized Exception.'))
599 $this->catalogSessionMock->expects($this->once())
600 ->method(
'setSendfriendFormData')
603 $this->urlBuilderMock->expects($this->once())
605 ->with(
'sendfriend/product/send', [
'_current' =>
true])
606 ->willReturn($redirectUrl);
608 $this->redirectMock->expects($this->once())
611 ->willReturnArgument(0);
624 public function testExecuteWithException()
629 $recipients =
'recipients';
632 'recipients' => $recipients,
634 $redirectUrl =
'redirect_url';
638 ->disableOriginalConstructor()
641 $this->resultFactoryMock->expects($this->once())
643 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])
646 $this->validatorMock->expects($this->once())
648 ->with($this->requestMock)
651 $this->requestMock->expects($this->exactly(2))
656 [
'cat_id',
null, $categoryId],
661 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
662 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
663 ->getMockForAbstractClass();
665 $this->productRepositoryMock->expects($this->once())
668 ->willReturn($productMock);
670 $productMock->expects($this->once())
671 ->method(
'isVisibleInCatalog')
674 $this->categoryRepositoryMock->expects($this->once())
676 ->with($categoryId,
null)
677 ->willThrowException(
new \
Magento\Framework\Exception\NoSuchEntityException(
__(
'No Category Exception.')));
679 $productMock->expects($this->never())
680 ->method(
'setCategory');
682 $this->registryMock->expects($this->once())
686 [
'product', $productMock,
false,
null],
690 $this->requestMock->expects($this->once())
691 ->method(
'getPostValue')
692 ->willReturn($formData);
694 $this->requestMock->expects($this->exactly(2))
699 [
'recipients', $recipients],
703 $this->sendFriendMock->expects($this->once())
704 ->method(
'setSender')
707 $this->sendFriendMock->expects($this->once())
708 ->method(
'setRecipients')
711 $this->sendFriendMock->expects($this->once())
712 ->method(
'setProduct')
715 $exception = new \Exception(
__(
'Exception.'));
716 $this->sendFriendMock->expects($this->once())
718 ->willThrowException($exception);
719 $this->sendFriendMock->expects($this->never())
722 $this->messageManagerMock->expects($this->once())
723 ->method(
'addException')
724 ->with($exception,
__(
'Some emails were not sent.'))
727 $this->catalogSessionMock->expects($this->once())
728 ->method(
'setSendfriendFormData')
731 $this->urlBuilderMock->expects($this->once())
733 ->with(
'sendfriend/product/send', [
'_current' =>
true])
734 ->willReturn($redirectUrl);
736 $this->redirectMock->expects($this->once())
739 ->willReturnArgument(0);
752 public function testExecuteWithoutProduct()
755 $recipients =
'recipients';
758 'recipients' => $recipients,
763 ->disableOriginalConstructor()
767 $forwardMock = $this->getMockBuilder(\
Magento\Framework\Controller\Result\Forward::class)
768 ->disableOriginalConstructor()
771 $this->resultFactoryMock->expects($this->exactly(2))
776 [\
Magento\Framework\Controller\ResultFactory::TYPE_FORWARD, [], $forwardMock],
780 $this->validatorMock->expects($this->once())
782 ->with($this->requestMock)
785 $this->requestMock->expects($this->once())
793 $this->requestMock->expects($this->once())
794 ->method(
'getPostValue')
795 ->willReturn($formData);
797 $forwardMock->expects($this->once())
802 $this->assertEquals($forwardMock, $this->model->execute());
808 public function testExecuteWithoutData()
815 ->disableOriginalConstructor()
819 $forwardMock = $this->getMockBuilder(\
Magento\Framework\Controller\Result\Forward::class)
820 ->disableOriginalConstructor()
823 $this->resultFactoryMock->expects($this->exactly(2))
828 [\
Magento\Framework\Controller\ResultFactory::TYPE_FORWARD, [], $forwardMock],
832 $this->validatorMock->expects($this->once())
834 ->with($this->requestMock)
837 $this->requestMock->expects($this->once())
846 $productMock = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)
847 ->setMethods([
'isVisibleInCatalog',
'setCategory',
'getProductUrl'])
848 ->getMockForAbstractClass();
850 $this->productRepositoryMock->expects($this->once())
853 ->willReturn($productMock);
855 $productMock->expects($this->once())
856 ->method(
'isVisibleInCatalog')
859 $this->registryMock->expects($this->once())
863 [
'product', $productMock,
false,
null],
867 $this->requestMock->expects($this->once())
868 ->method(
'getPostValue')
869 ->willReturn($formData);
871 $forwardMock->expects($this->once())
876 $this->assertEquals($forwardMock, $this->model->execute());
879 public function testExecuteWithoutFormKey()
883 ->disableOriginalConstructor()
886 $this->resultFactoryMock->expects($this->once())
894 $this->validatorMock->expects($this->once())
896 ->with($this->requestMock)
901 ->with(
'sendfriend/product/send', [
'_current' =>
true])