From add2bbb8f6b82f450dbf7e466f12f93254c0eed0 Mon Sep 17 00:00:00 2001 From: Nazar Klovanych Date: Thu, 2 Jan 2020 11:35:27 +0200 Subject: [PATCH] cover changes with unit test --- .../Test/Unit/Controller/Index/SendTest.php | 57 ++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php index 47148f7878134..c70c2a1a6a9b6 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php @@ -5,7 +5,10 @@ */ namespace Magento\Wishlist\Test\Unit\Controller\Index; +use Magento\Captcha\Helper\Data as CaptchaHelper; +use Magento\Captcha\Model\DefaultModel as CaptchaModel; use Magento\Customer\Model\Data\Customer as CustomerData; +use Magento\Customer\Model\Session; use Magento\Framework\App\Action\Context as ActionContext; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\Redirect as ResultRedirect; @@ -14,15 +17,13 @@ use Magento\Framework\Event\ManagerInterface as EventManagerInterface; use Magento\Framework\Mail\TransportInterface; use Magento\Framework\Message\ManagerInterface; +use Magento\Framework\Phrase; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\UrlInterface; use Magento\Framework\View\Result\Layout as ResultLayout; use Magento\Store\Model\Store; use Magento\Wishlist\Controller\Index\Send; use Magento\Wishlist\Controller\WishlistProviderInterface; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\Captcha\Helper\Data as CaptchaHelper; -use Magento\Captcha\Model\DefaultModel as CaptchaModel; -use Magento\Customer\Model\Session; /** * @SuppressWarnings(PHPMD.TooManyFields) @@ -212,7 +213,12 @@ protected function setUp() ); } - public function testExecuteNoFormKeyValidated() + /** + * Verify execute method without Form Key validated + * + * @return void + */ + public function testExecuteNoFormKeyValidated(): void { $this->formKeyValidator->expects($this->once()) ->method('validate') @@ -228,8 +234,43 @@ public function testExecuteNoFormKeyValidated() } /** - * @expectedException \Magento\Framework\Exception\NotFoundException - * @expectedExceptionMessage Page not found. + * Verify execute with no emails left + * + * @return void + */ + public function testExecuteWithNoEmailLeft(): void + { + $expectedMessage = new Phrase('Maximum of %1 emails can be sent.', [0]); + + $this->formKeyValidator->expects($this->once()) + ->method('validate') + ->with($this->request) + ->willReturn(true); + + $this->request->expects($this->at(0)) + ->method('getPost') + ->with('emails') + ->willReturn('some.Email@gmail.com', 'some.email2@gmail.com'); + $this->request->expects($this->at(1)) + ->method('getPost') + ->with('message'); + $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class); + $this->wishlistProvider->expects($this->once()) + ->method('getWishlist') + ->willReturn($wishlist); + $this->resultRedirect->expects($this->once()) + ->method('setPath') + ->with('*/*/share') + ->willReturnSelf(); + $this->messageManager->expects($this->once()) + ->method('addErrorMessage') + ->with($expectedMessage); + + $this->assertEquals($this->resultRedirect, $this->model->execute()); + } + + /** + * Execute method with no wishlist available */ public function testExecuteNoWishlistAvailable() { @@ -241,6 +282,8 @@ public function testExecuteNoWishlistAvailable() $this->wishlistProvider->expects($this->once()) ->method('getWishlist') ->willReturn(null); + $this->expectException(\Magento\Framework\Exception\NotFoundException::class); + $this->expectExceptionMessage('Page not found'); $this->model->execute(); }