From a4c74d8fd1cb4a67faee123607dab819bea54f04 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 30 Mar 2020 10:32:44 +0200 Subject: [PATCH 01/15] Implement ActionInterface for Wishlist/Shared --- .../Wishlist/Controller/Shared/Allcart.php | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 6300b14dcf515..8d160d270a6f0 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -3,55 +3,68 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + namespace Magento\Wishlist\Controller\Shared; -use Magento\Framework\App\Action\Context; -use Magento\Wishlist\Model\ItemCarrier; +use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Controller\Result\Forward; +use Magento\Framework\Controller\Result\Redirect; +use Magento\Wishlist\Model\ItemCarrier; -class Allcart extends \Magento\Framework\App\Action\Action +class Allcart implements HttpGetActionInterface { + /** + * @var ItemCarrier + */ + private $itemCarrier; + /** * @var WishlistProvider */ - protected $wishlistProvider; + private $wishlistProvider; /** - * @var \Magento\Wishlist\Model\ItemCarrier + * @var RequestInterface */ - protected $itemCarrier; + private $request; /** - * @param Context $context - * @param WishlistProvider $wishlistProvider - * @param ItemCarrier $itemCarrier + * @var ResultFactory */ + private $resultFactory; + public function __construct( - Context $context, - WishlistProvider $wishlistProvider, - ItemCarrier $itemCarrier + ItemCarrier $itemCarrier, + RequestInterface $request, + ResultFactory $resultFactory, + WishlistProvider $wishlistProvider ) { - $this->wishlistProvider = $wishlistProvider; $this->itemCarrier = $itemCarrier; - parent::__construct($context); + $this->request = $request; + $this->resultFactory = $resultFactory; + $this->wishlistProvider = $wishlistProvider; } /** * Add all items from wishlist to shopping cart * - * @return \Magento\Framework\Controller\ResultInterface + * @inheritDoc */ public function execute() { $wishlist = $this->wishlistProvider->getWishlist(); if (!$wishlist) { - /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ + /** @var Forward $resultForward */ $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD); $resultForward->forward('noroute'); return $resultForward; } - $redirectUrl = $this->itemCarrier->moveAllToCart($wishlist, $this->getRequest()->getParam('qty')); - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ + $redirectUrl = $this->itemCarrier->moveAllToCart($wishlist, $this->request->getParam('qty')); + /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($redirectUrl); return $resultRedirect; From 18e7519bb69cd05bb73a9c0afb8d4948850b9f9a Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 30 Mar 2020 10:56:02 +0200 Subject: [PATCH 02/15] Unit test for Wishlist/Shared Controller --- .../Unit/Controller/Shared/AllcartTest.php | 72 +++++++++---------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index d5ac5e9485424..4422c13a72e60 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -5,82 +5,86 @@ */ namespace Magento\Wishlist\Test\Unit\Controller\Shared; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\App\Action\Context; +use Magento\Framework\App\Request\Http; +use Magento\Framework\Controller\Result\Forward; +use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; - -class AllcartTest extends \PHPUnit\Framework\TestCase +use Magento\Wishlist\Controller\Shared\Allcart; +use Magento\Wishlist\Controller\Shared\WishlistProvider; +use Magento\Wishlist\Model\ItemCarrier; +use Magento\Wishlist\Model\Wishlist; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class AllcartTest extends TestCase { /** - * @var \Magento\Wishlist\Controller\Shared\Allcart + * @var Allcart */ protected $allcartController; /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - protected $objectManagerHelper; - - /** - * @var \Magento\Framework\App\Action\Context + * @var Context */ protected $context; /** - * @var \Magento\Wishlist\Controller\Shared\WishlistProvider|\PHPUnit\Framework\MockObject\MockObject + * @var WishlistProvider|MockObject */ protected $wishlistProviderMock; /** - * @var \Magento\Wishlist\Model\ItemCarrier|\PHPUnit\Framework\MockObject\MockObject + * @var ItemCarrier|MockObject */ protected $itemCarrierMock; /** - * @var \Magento\Wishlist\Model\Wishlist|\PHPUnit\Framework\MockObject\MockObject + * @var Wishlist|MockObject */ protected $wishlistMock; /** - * @var \Magento\Framework\App\Request\Http|\PHPUnit\Framework\MockObject\MockObject + * @var Http|MockObject */ protected $requestMock; /** - * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit\Framework\MockObject\MockObject + * @var ResultFactory|MockObject */ protected $resultFactoryMock; /** - * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit\Framework\MockObject\MockObject + * @var Redirect|MockObject */ protected $resultRedirectMock; /** - * @var \Magento\Framework\Controller\Result\Forward|\PHPUnit\Framework\MockObject\MockObject + * @var Forward|MockObject */ protected $resultForwardMock; protected function setUp() { - $this->wishlistProviderMock = $this->getMockBuilder(\Magento\Wishlist\Controller\Shared\WishlistProvider::class) + $this->wishlistProviderMock = $this->getMockBuilder(WishlistProvider::class) ->disableOriginalConstructor() ->getMock(); - $this->itemCarrierMock = $this->getMockBuilder(\Magento\Wishlist\Model\ItemCarrier::class) + $this->itemCarrierMock = $this->getMockBuilder(ItemCarrier::class) ->disableOriginalConstructor() ->getMock(); - $this->wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class) + $this->wishlistMock = $this->getMockBuilder(Wishlist::class) ->disableOriginalConstructor() ->getMock(); - $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) + $this->requestMock = $this->getMockBuilder(Http::class) ->disableOriginalConstructor() ->getMock(); - $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class) + $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) ->disableOriginalConstructor() ->getMock(); - $this->resultRedirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class) + $this->resultRedirectMock = $this->getMockBuilder(Redirect::class) ->disableOriginalConstructor() ->getMock(); - $this->resultForwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class) + $this->resultForwardMock = $this->getMockBuilder(Forward::class) ->disableOriginalConstructor() ->getMock(); @@ -93,21 +97,11 @@ protected function setUp() ] ); - $this->objectManagerHelper = new ObjectManagerHelper($this); - $this->context = $this->objectManagerHelper->getObject( - \Magento\Framework\App\Action\Context::class, - [ - 'request' => $this->requestMock, - 'resultFactory' => $this->resultFactoryMock - ] - ); - $this->allcartController = $this->objectManagerHelper->getObject( - \Magento\Wishlist\Controller\Shared\Allcart::class, - [ - 'context' => $this->context, - 'wishlistProvider' => $this->wishlistProviderMock, - 'itemCarrier' => $this->itemCarrierMock - ] + $this->allcartController = new Allcart( + $this->itemCarrierMock, + $this->requestMock, + $this->resultFactoryMock, + $this->wishlistProviderMock ); } From a002bf27f2a680bde1d13f82fb867a8bbb65a372 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 30 Mar 2020 11:05:56 +0200 Subject: [PATCH 03/15] PSR2 linted --- app/code/Magento/Wishlist/Controller/Shared/Allcart.php | 2 +- .../Wishlist/Test/Unit/Controller/Shared/AllcartTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 8d160d270a6f0..89c1b88d9568e 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -15,7 +15,7 @@ use Magento\Framework\Controller\Result\Redirect; use Magento\Wishlist\Model\ItemCarrier; -class Allcart implements HttpGetActionInterface +class Allcart implements HttpGetActionInterface { /** * @var ItemCarrier diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index 4422c13a72e60..9b931290befbd 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Wishlist\Test\Unit\Controller\Shared; use Magento\Framework\App\Action\Context; From df2c759db53ef19d4f1d37bfdca6a02a76fde27c Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 30 Mar 2020 12:32:26 +0200 Subject: [PATCH 04/15] Implement ActionInterface for Wishlist/Shared/Cart + unit test --- .../Wishlist/Controller/Shared/Cart.php | 64 ++++++++++----- .../Test/Unit/Controller/Shared/CartTest.php | 81 ++++++++++--------- 2 files changed, 88 insertions(+), 57 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Cart.php b/app/code/Magento/Wishlist/Controller/Shared/Cart.php index 38f100602972a..77dc52e05f077 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Cart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Cart.php @@ -3,16 +3,22 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + namespace Magento\Wishlist\Controller\Shared; use Magento\Catalog\Model\Product\Exception as ProductException; use Magento\Checkout\Helper\Cart as CartHelper; use Magento\Checkout\Model\Cart as CustomerCart; -use Magento\Framework\App\Action\Context as ActionContext; use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Controller\Result\Redirect as ResultRedirect; use Magento\Framework\Escaper; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; +use Magento\Framework\App\Response\RedirectInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Item\OptionFactory; use Magento\Wishlist\Model\ItemFactory; @@ -23,55 +29,73 @@ * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class Cart extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface +class Cart implements HttpGetActionInterface { /** * @var CustomerCart */ - protected $cart; + private $cart; /** * @var OptionFactory */ - protected $optionFactory; + private $optionFactory; /** * @var ItemFactory */ - protected $itemFactory; + private $itemFactory; /** * @var CartHelper */ - protected $cartHelper; + private $cartHelper; /** * @var Escaper */ - protected $escaper; + private $escaper; + + /** + * @var RequestInterface + */ + private $request; + + /** + * @var RedirectInterface + */ + private $redirect; + + /** + * @var MessageManagerInterface + */ + private $messageManager; /** - * @param ActionContext $context - * @param CustomerCart $cart - * @param OptionFactory $optionFactory - * @param ItemFactory $itemFactory - * @param CartHelper $cartHelper - * @param Escaper $escaper + * @var ResultFactory */ + private $resultFactory; + public function __construct( - ActionContext $context, CustomerCart $cart, OptionFactory $optionFactory, ItemFactory $itemFactory, CartHelper $cartHelper, - Escaper $escaper + Escaper $escaper, + RequestInterface $request, + RedirectInterface $redirect, + MessageManagerInterface $messageManager, + ResultFactory $resultFactory ) { $this->cart = $cart; $this->optionFactory = $optionFactory; $this->itemFactory = $itemFactory; $this->cartHelper = $cartHelper; $this->escaper = $escaper; - parent::__construct($context); + $this->request = $request; + $this->redirect = $redirect; + $this->messageManager = $messageManager; + $this->resultFactory = $resultFactory; } /** @@ -80,17 +104,17 @@ public function __construct( * If Product has required options - redirect * to product view page with message about needed defined required options * - * @return \Magento\Framework\Controller\Result\Redirect + * @inheritDoc */ public function execute() { - $itemId = (int)$this->getRequest()->getParam('item'); + $itemId = (int)$this->request->getParam('item'); /* @var $item Item */ $item = $this->itemFactory->create() ->load($itemId); - $redirectUrl = $this->_redirect->getRefererUrl(); + $redirectUrl = $this->redirect->getRefererUrl(); try { /** @var OptionCollection $options */ @@ -120,7 +144,7 @@ public function execute() } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, __('We can\'t add the item to the cart right now.')); } - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ + /** @var ResultRedirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($redirectUrl); return $resultRedirect; diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index eba5666114139..c495ea6651342 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -3,9 +3,11 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Wishlist\Test\Unit\Controller\Shared; use Magento\Catalog\Model\Product; +use Magento\Catalog\Model\Product\Exception; use Magento\Checkout\Helper\Cart as CartHelper; use Magento\Checkout\Model\Cart; use Magento\Framework\App\Action\Context as ActionContext; @@ -23,80 +25,82 @@ use Magento\Wishlist\Model\Item\OptionFactory; use Magento\Wishlist\Model\ItemFactory; use Magento\Wishlist\Model\ResourceModel\Item\Option\Collection as OptionCollection; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; /** * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class CartTest extends \PHPUnit\Framework\TestCase +class CartTest extends TestCase { - /** @var SharedCart|\PHPUnit\Framework\MockObject\MockObject */ + /** @var SharedCart|MockObject */ protected $model; - /** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var RequestInterface|MockObject */ protected $request; - /** @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ManagerInterface|MockObject */ protected $messageManager; - /** @var ActionContext|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ActionContext|MockObject */ protected $context; - /** @var Cart|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Cart|MockObject */ protected $cart; - /** @var CartHelper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var CartHelper|MockObject */ protected $cartHelper; - /** @var Quote|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Quote|MockObject */ protected $quote; - /** @var OptionCollection|\PHPUnit\Framework\MockObject\MockObject */ + /** @var OptionCollection|MockObject */ protected $optionCollection; - /** @var OptionFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var OptionFactory|MockObject */ protected $optionFactory; - /** @var Option|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Option|MockObject */ protected $option; - /** @var ItemFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ItemFactory|MockObject */ protected $itemFactory; - /** @var Item|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Item|MockObject */ protected $item; - /** @var Escaper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Escaper|MockObject */ protected $escaper; - /** @var RedirectInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var RedirectInterface|MockObject */ protected $redirect; - /** @var ResultFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ResultFactory|MockObject */ protected $resultFactory; - /** @var Redirect|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Redirect|MockObject */ protected $resultRedirect; - /** @var Product|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Product|MockObject */ protected $product; protected function setUp() { - $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) + $this->request = $this->getMockBuilder(RequestInterface::class) ->getMockForAbstractClass(); - $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class) + $this->redirect = $this->getMockBuilder(RedirectInterface::class) ->getMockForAbstractClass(); - $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class) + $this->messageManager = $this->getMockBuilder(ManagerInterface::class) ->getMockForAbstractClass(); - $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class) + $this->resultRedirect = $this->getMockBuilder(Redirect::class) ->disableOriginalConstructor() ->getMock(); - $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class) + $this->resultFactory = $this->getMockBuilder(ResultFactory::class) ->disableOriginalConstructor() ->getMock(); $this->resultFactory->expects($this->once()) @@ -104,7 +108,7 @@ protected function setUp() ->with(ResultFactory::TYPE_REDIRECT) ->willReturn($this->resultRedirect); - $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class) + $this->context = $this->getMockBuilder(ActionContext::class) ->disableOriginalConstructor() ->getMock(); $this->context->expects($this->any()) @@ -120,28 +124,28 @@ protected function setUp() ->method('getResultFactory') ->willReturn($this->resultFactory); - $this->cart = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class) + $this->cart = $this->getMockBuilder(Cart::class) ->disableOriginalConstructor() ->getMock(); - $this->cartHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Cart::class) + $this->cartHelper = $this->getMockBuilder(CartHelper::class) ->disableOriginalConstructor() ->getMock(); - $this->quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class) + $this->quote = $this->getMockBuilder(Quote::class) ->disableOriginalConstructor() ->setMethods(['getHasError']) ->getMock(); $this->optionCollection = $this->getMockBuilder( - \Magento\Wishlist\Model\ResourceModel\Item\Option\Collection::class + OptionCollection::class )->disableOriginalConstructor()->getMock(); - $this->option = $this->getMockBuilder(\Magento\Wishlist\Model\Item\Option::class) + $this->option = $this->getMockBuilder(Option::class) ->disableOriginalConstructor() ->getMock(); - $this->optionFactory = $this->getMockBuilder(\Magento\Wishlist\Model\Item\OptionFactory::class) + $this->optionFactory = $this->getMockBuilder(OptionFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); @@ -149,11 +153,11 @@ protected function setUp() ->method('create') ->willReturn($this->option); - $this->item = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class) + $this->item = $this->getMockBuilder(Item::class) ->disableOriginalConstructor() ->getMock(); - $this->itemFactory = $this->getMockBuilder(\Magento\Wishlist\Model\ItemFactory::class) + $this->itemFactory = $this->getMockBuilder(ItemFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); @@ -161,21 +165,24 @@ protected function setUp() ->method('create') ->willReturn($this->item); - $this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class) + $this->escaper = $this->getMockBuilder(Escaper::class) ->disableOriginalConstructor() ->getMock(); - $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) + $this->product = $this->getMockBuilder(Product::class) ->disableOriginalConstructor() ->getMock(); $this->model = new SharedCart( - $this->context, $this->cart, $this->optionFactory, $this->itemFactory, $this->cartHelper, - $this->escaper + $this->escaper, + $this->request, + $this->redirect, + $this->messageManager, + $this->resultFactory ); } @@ -353,7 +360,7 @@ public function testExecuteProductException() $this->option->expects($this->once()) ->method('getCollection') - ->willThrowException(new \Magento\Catalog\Model\Product\Exception(__('LocalizedException'))); + ->willThrowException(new Exception(__('LocalizedException'))); $this->resultRedirect->expects($this->once()) ->method('setUrl') From 3ac458b2e4442d4b0fc3e7de672d7562c1c85607 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Wed, 13 May 2020 15:06:54 +0200 Subject: [PATCH 05/15] Static test fixes --- .../Magento/Wishlist/Controller/Shared/Allcart.php | 8 +++++++- app/code/Magento/Wishlist/Controller/Shared/Cart.php | 11 +++++++++++ .../Wishlist/Test/Unit/Controller/Shared/CartTest.php | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 89c1b88d9568e..b6e93d7047c76 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -37,6 +37,12 @@ class Allcart implements HttpGetActionInterface */ private $resultFactory; + /** + * @param ItemCarrier $itemCarrier + * @param RequestInterface $request + * @param ResultFactory $resultFactory + * @param WishlistProvider $wishlistProvider + */ public function __construct( ItemCarrier $itemCarrier, RequestInterface $request, @@ -52,7 +58,7 @@ public function __construct( /** * Add all items from wishlist to shopping cart * - * @inheritDoc + * {@inheritDoc} */ public function execute() { diff --git a/app/code/Magento/Wishlist/Controller/Shared/Cart.php b/app/code/Magento/Wishlist/Controller/Shared/Cart.php index 77dc52e05f077..df023ffc01ddf 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Cart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Cart.php @@ -76,6 +76,17 @@ class Cart implements HttpGetActionInterface */ private $resultFactory; + /** + * @param CustomerCart $cart + * @param OptionFactory $optionFactory + * @param ItemFactory $itemFactory + * @param CartHelper $cartHelper + * @param Escaper $escaper + * @param RequestInterface $request + * @param RedirectInterface $redirect + * @param MessageManagerInterface $messageManager + * @param ResultFactory $resultFactory + */ public function __construct( CustomerCart $cart, OptionFactory $optionFactory, diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index c495ea6651342..43f82c1722bde 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -85,6 +85,7 @@ class CartTest extends TestCase /** @var Product|MockObject */ protected $product; + // phpcs:ignore Generic.Files.LineLength.TooLong protected function setUp() { $this->request = $this->getMockBuilder(RequestInterface::class) From f4744395fa245230386c65d00405dfba665dc868 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Wed, 13 May 2020 16:14:53 +0200 Subject: [PATCH 06/15] suppress ExcessiveMethodLength notice --- .../Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index 76ac73d837f77..0c284d1c917b9 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -86,7 +86,9 @@ class CartTest extends TestCase /** @var Product|MockObject */ protected $product; - // phpcs:ignore Generic.Files.LineLength.TooLong + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ protected function setUp(): void { $this->request = $this->getMockBuilder(RequestInterface::class) From 4a8536439280fe98692020ce570e64b32555764d Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Fri, 26 Jun 2020 11:57:18 +0200 Subject: [PATCH 07/15] added extend Action back, updated tests --- .../Magento/Wishlist/Controller/Shared/Allcart.php | 9 +++++++-- app/code/Magento/Wishlist/Controller/Shared/Cart.php | 11 ++++++++--- .../Test/Unit/Controller/Shared/AllcartTest.php | 12 ++++++++---- .../Test/Unit/Controller/Shared/CartTest.php | 1 + 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index b6e93d7047c76..17ee92e8c1307 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -8,14 +8,16 @@ namespace Magento\Wishlist\Controller\Shared; +use Magento\Framework\App\Action\Action; +use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\RequestInterface; -use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\Redirect; +use Magento\Framework\Controller\ResultFactory; use Magento\Wishlist\Model\ItemCarrier; -class Allcart implements HttpGetActionInterface +class Allcart extends Action implements HttpGetActionInterface { /** * @var ItemCarrier @@ -38,12 +40,14 @@ class Allcart implements HttpGetActionInterface private $resultFactory; /** + * @param Context $context * @param ItemCarrier $itemCarrier * @param RequestInterface $request * @param ResultFactory $resultFactory * @param WishlistProvider $wishlistProvider */ public function __construct( + Context $context, ItemCarrier $itemCarrier, RequestInterface $request, ResultFactory $resultFactory, @@ -53,6 +57,7 @@ public function __construct( $this->request = $request; $this->resultFactory = $resultFactory; $this->wishlistProvider = $wishlistProvider; + parent::__construct($context); } /** diff --git a/app/code/Magento/Wishlist/Controller/Shared/Cart.php b/app/code/Magento/Wishlist/Controller/Shared/Cart.php index df023ffc01ddf..b6e7904ae412c 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Cart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Cart.php @@ -11,14 +11,16 @@ use Magento\Catalog\Model\Product\Exception as ProductException; use Magento\Checkout\Helper\Cart as CartHelper; use Magento\Checkout\Model\Cart as CustomerCart; +use Magento\Framework\App\Action\Action; +use Magento\Framework\App\Action\Context as ActionContext; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\RequestInterface; -use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\Controller\Result\Redirect as ResultRedirect; +use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Escaper; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; -use Magento\Framework\App\Response\RedirectInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Item\OptionFactory; use Magento\Wishlist\Model\ItemFactory; @@ -29,7 +31,7 @@ * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class Cart implements HttpGetActionInterface +class Cart extends Action implements HttpGetActionInterface { /** * @var CustomerCart @@ -77,6 +79,7 @@ class Cart implements HttpGetActionInterface private $resultFactory; /** + * @param ActionContext $context * @param CustomerCart $cart * @param OptionFactory $optionFactory * @param ItemFactory $itemFactory @@ -88,6 +91,7 @@ class Cart implements HttpGetActionInterface * @param ResultFactory $resultFactory */ public function __construct( + ActionContext $context, CustomerCart $cart, OptionFactory $optionFactory, ItemFactory $itemFactory, @@ -107,6 +111,7 @@ public function __construct( $this->redirect = $redirect; $this->messageManager = $messageManager; $this->resultFactory = $resultFactory; + parent::__construct($context); } /** diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index 239f65f50c424..e80404b55ffdb 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -27,14 +27,14 @@ class AllcartTest extends TestCase protected $allcartController; /** - * @var Context + * @var WishlistProvider|MockObject */ - protected $context; + protected $wishlistProviderMock; /** - * @var WishlistProvider|MockObject + * @var Context|MockObject */ - protected $wishlistProviderMock; + protected $contextMock; /** * @var ItemCarrier|MockObject @@ -74,6 +74,9 @@ protected function setUp(): void $this->itemCarrierMock = $this->getMockBuilder(ItemCarrier::class) ->disableOriginalConstructor() ->getMock(); + $this->contextMock = $this->getMockBuilder(Context::class) + ->disableOriginalConstructor() + ->getMock(); $this->wishlistMock = $this->getMockBuilder(Wishlist::class) ->disableOriginalConstructor() ->getMock(); @@ -100,6 +103,7 @@ protected function setUp(): void ); $this->allcartController = new Allcart( + $this->contextMock, $this->itemCarrierMock, $this->requestMock, $this->resultFactoryMock, diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index 0c284d1c917b9..c0181ab061529 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -178,6 +178,7 @@ protected function setUp(): void ->getMock(); $this->model = new SharedCart( + $this->context, $this->cart, $this->optionFactory, $this->itemFactory, From 979ab1f1c37e4e6534a0a9905e166218d792722f Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 29 Jun 2020 10:30:29 +0200 Subject: [PATCH 08/15] removed unnecessary properties --- .../Wishlist/Controller/Shared/Allcart.php | 19 +-------- .../Wishlist/Controller/Shared/Cart.php | 41 ++----------------- 2 files changed, 4 insertions(+), 56 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 17ee92e8c1307..96b3102a6f443 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -11,7 +11,6 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; -use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; @@ -29,33 +28,17 @@ class Allcart extends Action implements HttpGetActionInterface */ private $wishlistProvider; - /** - * @var RequestInterface - */ - private $request; - - /** - * @var ResultFactory - */ - private $resultFactory; - /** * @param Context $context * @param ItemCarrier $itemCarrier - * @param RequestInterface $request - * @param ResultFactory $resultFactory * @param WishlistProvider $wishlistProvider */ public function __construct( Context $context, ItemCarrier $itemCarrier, - RequestInterface $request, - ResultFactory $resultFactory, WishlistProvider $wishlistProvider ) { $this->itemCarrier = $itemCarrier; - $this->request = $request; - $this->resultFactory = $resultFactory; $this->wishlistProvider = $wishlistProvider; parent::__construct($context); } @@ -74,7 +57,7 @@ public function execute() $resultForward->forward('noroute'); return $resultForward; } - $redirectUrl = $this->itemCarrier->moveAllToCart($wishlist, $this->request->getParam('qty')); + $redirectUrl = $this->itemCarrier->moveAllToCart($wishlist, $this->getRequest()->getParam('qty')); /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($redirectUrl); diff --git a/app/code/Magento/Wishlist/Controller/Shared/Cart.php b/app/code/Magento/Wishlist/Controller/Shared/Cart.php index b6e7904ae412c..9fdadb917a920 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Cart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Cart.php @@ -14,13 +14,10 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context as ActionContext; use Magento\Framework\App\Action\HttpGetActionInterface; -use Magento\Framework\App\RequestInterface; -use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\Controller\Result\Redirect as ResultRedirect; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Escaper; use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Item\OptionFactory; use Magento\Wishlist\Model\ItemFactory; @@ -58,26 +55,6 @@ class Cart extends Action implements HttpGetActionInterface */ private $escaper; - /** - * @var RequestInterface - */ - private $request; - - /** - * @var RedirectInterface - */ - private $redirect; - - /** - * @var MessageManagerInterface - */ - private $messageManager; - - /** - * @var ResultFactory - */ - private $resultFactory; - /** * @param ActionContext $context * @param CustomerCart $cart @@ -85,10 +62,6 @@ class Cart extends Action implements HttpGetActionInterface * @param ItemFactory $itemFactory * @param CartHelper $cartHelper * @param Escaper $escaper - * @param RequestInterface $request - * @param RedirectInterface $redirect - * @param MessageManagerInterface $messageManager - * @param ResultFactory $resultFactory */ public function __construct( ActionContext $context, @@ -96,21 +69,13 @@ public function __construct( OptionFactory $optionFactory, ItemFactory $itemFactory, CartHelper $cartHelper, - Escaper $escaper, - RequestInterface $request, - RedirectInterface $redirect, - MessageManagerInterface $messageManager, - ResultFactory $resultFactory + Escaper $escaper ) { $this->cart = $cart; $this->optionFactory = $optionFactory; $this->itemFactory = $itemFactory; $this->cartHelper = $cartHelper; $this->escaper = $escaper; - $this->request = $request; - $this->redirect = $redirect; - $this->messageManager = $messageManager; - $this->resultFactory = $resultFactory; parent::__construct($context); } @@ -124,13 +89,13 @@ public function __construct( */ public function execute() { - $itemId = (int)$this->request->getParam('item'); + $itemId = (int)$this->getRequest()->getParam('item'); /* @var $item Item */ $item = $this->itemFactory->create() ->load($itemId); - $redirectUrl = $this->redirect->getRefererUrl(); + $redirectUrl = $this->_redirect->getRefererUrl(); try { /** @var OptionCollection $options */ From 6d8f77c7ffe52075d0b5ff117b39100bd06e1474 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 29 Jun 2020 12:10:08 +0200 Subject: [PATCH 09/15] fixed tests --- .../Wishlist/Test/Unit/Controller/Shared/AllcartTest.php | 2 -- .../Wishlist/Test/Unit/Controller/Shared/CartTest.php | 6 +----- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index e80404b55ffdb..f44fdf1b6568b 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -105,8 +105,6 @@ protected function setUp(): void $this->allcartController = new Allcart( $this->contextMock, $this->itemCarrierMock, - $this->requestMock, - $this->resultFactoryMock, $this->wishlistProviderMock ); } diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index c0181ab061529..cee4b39ecfc2f 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -183,11 +183,7 @@ protected function setUp(): void $this->optionFactory, $this->itemFactory, $this->cartHelper, - $this->escaper, - $this->request, - $this->redirect, - $this->messageManager, - $this->resultFactory + $this->escaper ); } From 2616ba14c89b3cb85a88399bca245b7ea764f4e2 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Mon, 29 Jun 2020 12:29:13 +0200 Subject: [PATCH 10/15] fixed tests --- .../Unit/Controller/Shared/AllcartTest.php | 24 ------------------- .../Test/Unit/Controller/Shared/CartTest.php | 3 --- 2 files changed, 27 deletions(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index f44fdf1b6568b..6c96cefe8f92f 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -77,30 +77,6 @@ protected function setUp(): void $this->contextMock = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->getMock(); - $this->wishlistMock = $this->getMockBuilder(Wishlist::class) - ->disableOriginalConstructor() - ->getMock(); - $this->requestMock = $this->getMockBuilder(Http::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultRedirectMock = $this->getMockBuilder(Redirect::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultForwardMock = $this->getMockBuilder(Forward::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->resultFactoryMock->expects($this->any()) - ->method('create') - ->willReturnMap( - [ - [ResultFactory::TYPE_REDIRECT, [], $this->resultRedirectMock], - [ResultFactory::TYPE_FORWARD, [], $this->resultForwardMock] - ] - ); $this->allcartController = new Allcart( $this->contextMock, diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index cee4b39ecfc2f..da9c2d536830f 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -173,9 +173,6 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); - $this->product = $this->getMockBuilder(Product::class) - ->disableOriginalConstructor() - ->getMock(); $this->model = new SharedCart( $this->context, From e2db162bb51e794809dbc402a7e52c21e7c43c04 Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Tue, 30 Jun 2020 15:23:49 +0200 Subject: [PATCH 11/15] reverted fixed tests --- .../Test/Unit/Controller/Shared/AllcartTest.php | 15 +++++++++++++++ .../Test/Unit/Controller/Shared/CartTest.php | 3 +++ 2 files changed, 18 insertions(+) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index 6c96cefe8f92f..4c2175a55ce9d 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -77,6 +77,21 @@ protected function setUp(): void $this->contextMock = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->getMock(); + $this->wishlistMock = $this->getMockBuilder(Wishlist::class) + ->disableOriginalConstructor() + ->getMock(); + $this->requestMock = $this->getMockBuilder(Http::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultRedirectMock = $this->getMockBuilder(Redirect::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultForwardMock = $this->getMockBuilder(Forward::class) + ->disableOriginalConstructor() + ->getMock(); $this->allcartController = new Allcart( $this->contextMock, diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index da9c2d536830f..cee4b39ecfc2f 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -173,6 +173,9 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); + $this->product = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->getMock(); $this->model = new SharedCart( $this->context, From 2f080644f1f1f7cfed677a9eb3008932fcef9a2c Mon Sep 17 00:00:00 2001 From: Rudolf Vince Date: Tue, 30 Jun 2020 17:07:06 +0200 Subject: [PATCH 12/15] objectmanager added to test --- .../Unit/Controller/Shared/AllcartTest.php | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index 4c2175a55ce9d..5fa5eb748ac65 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -12,6 +12,7 @@ use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; use Magento\Wishlist\Controller\Shared\Allcart; use Magento\Wishlist\Controller\Shared\WishlistProvider; use Magento\Wishlist\Model\ItemCarrier; @@ -26,6 +27,11 @@ class AllcartTest extends TestCase */ protected $allcartController; + /** + * @var ObjectManagerHelper + */ + protected $objectManagerHelper; + /** * @var WishlistProvider|MockObject */ @@ -34,7 +40,7 @@ class AllcartTest extends TestCase /** * @var Context|MockObject */ - protected $contextMock; + protected $context; /** * @var ItemCarrier|MockObject @@ -74,9 +80,6 @@ protected function setUp(): void $this->itemCarrierMock = $this->getMockBuilder(ItemCarrier::class) ->disableOriginalConstructor() ->getMock(); - $this->contextMock = $this->getMockBuilder(Context::class) - ->disableOriginalConstructor() - ->getMock(); $this->wishlistMock = $this->getMockBuilder(Wishlist::class) ->disableOriginalConstructor() ->getMock(); @@ -93,8 +96,26 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); + $this->resultFactoryMock->expects($this->any()) + ->method('create') + ->willReturnMap( + [ + [ResultFactory::TYPE_REDIRECT, [], $this->resultRedirectMock], + [ResultFactory::TYPE_FORWARD, [], $this->resultForwardMock] + ] + ); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->context = $this->objectManagerHelper->getObject( + Context::class, + [ + 'request' => $this->requestMock, + 'resultFactory' => $this->resultFactoryMock + ] + ); + $this->allcartController = new Allcart( - $this->contextMock, + $this->context, $this->itemCarrierMock, $this->wishlistProviderMock ); From 21f9fa4e30295d3e2e5d3c548922aa7e57421dce Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Fri, 3 Jul 2020 12:02:18 +0300 Subject: [PATCH 13/15] impr --- .../Wishlist/Controller/Shared/Allcart.php | 20 +- .../Wishlist/Controller/Shared/Cart.php | 11 +- .../Unit/Controller/Shared/AllcartTest.php | 86 ++++----- .../Test/Unit/Controller/Shared/CartTest.php | 178 ++++++++---------- 4 files changed, 134 insertions(+), 161 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 96b3102a6f443..c1e908cc0f49e 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -16,30 +16,33 @@ use Magento\Framework\Controller\ResultFactory; use Magento\Wishlist\Model\ItemCarrier; +/** + * Wishlist Allcart Controller + */ class Allcart extends Action implements HttpGetActionInterface { /** - * @var ItemCarrier + * @var WishlistProvider */ - private $itemCarrier; + protected $wishlistProvider; /** - * @var WishlistProvider + * @var ItemCarrier */ - private $wishlistProvider; + protected $itemCarrier; /** * @param Context $context - * @param ItemCarrier $itemCarrier * @param WishlistProvider $wishlistProvider + * @param ItemCarrier $itemCarrier */ public function __construct( Context $context, - ItemCarrier $itemCarrier, - WishlistProvider $wishlistProvider + WishlistProvider $wishlistProvider, + ItemCarrier $itemCarrier ) { - $this->itemCarrier = $itemCarrier; $this->wishlistProvider = $wishlistProvider; + $this->itemCarrier = $itemCarrier; parent::__construct($context); } @@ -61,6 +64,7 @@ public function execute() /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($redirectUrl); + return $resultRedirect; } } diff --git a/app/code/Magento/Wishlist/Controller/Shared/Cart.php b/app/code/Magento/Wishlist/Controller/Shared/Cart.php index 9fdadb917a920..bee1e187be648 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Cart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Cart.php @@ -33,27 +33,27 @@ class Cart extends Action implements HttpGetActionInterface /** * @var CustomerCart */ - private $cart; + protected $cart; /** * @var OptionFactory */ - private $optionFactory; + protected $optionFactory; /** * @var ItemFactory */ - private $itemFactory; + protected $itemFactory; /** * @var CartHelper */ - private $cartHelper; + protected $cartHelper; /** * @var Escaper */ - private $escaper; + protected $escaper; /** * @param ActionContext $context @@ -128,6 +128,7 @@ public function execute() /** @var ResultRedirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($redirectUrl); + return $resultRedirect; } } diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php index 5fa5eb748ac65..d9339af8144f4 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/AllcartTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + declare(strict_types=1); namespace Magento\Wishlist\Test\Unit\Controller\Shared; @@ -20,83 +21,60 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +/** + * Test for \Magento\Wishlist\Controller\Shared\Allcart. + */ class AllcartTest extends TestCase { /** * @var Allcart */ - protected $allcartController; - - /** - * @var ObjectManagerHelper - */ - protected $objectManagerHelper; + private $allcartController; /** * @var WishlistProvider|MockObject */ - protected $wishlistProviderMock; - - /** - * @var Context|MockObject - */ - protected $context; + private $wishlistProviderMock; /** * @var ItemCarrier|MockObject */ - protected $itemCarrierMock; + private $itemCarrierMock; /** * @var Wishlist|MockObject */ - protected $wishlistMock; + private $wishlistMock; /** * @var Http|MockObject */ - protected $requestMock; - - /** - * @var ResultFactory|MockObject - */ - protected $resultFactoryMock; + private $requestMock; /** * @var Redirect|MockObject */ - protected $resultRedirectMock; + private $resultRedirectMock; /** * @var Forward|MockObject */ - protected $resultForwardMock; + private $resultForwardMock; + /** + * @inheritDoc + */ protected function setUp(): void { - $this->wishlistProviderMock = $this->getMockBuilder(WishlistProvider::class) - ->disableOriginalConstructor() - ->getMock(); - $this->itemCarrierMock = $this->getMockBuilder(ItemCarrier::class) - ->disableOriginalConstructor() - ->getMock(); - $this->wishlistMock = $this->getMockBuilder(Wishlist::class) - ->disableOriginalConstructor() - ->getMock(); - $this->requestMock = $this->getMockBuilder(Http::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultRedirectMock = $this->getMockBuilder(Redirect::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultForwardMock = $this->getMockBuilder(Forward::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->resultFactoryMock->expects($this->any()) + $this->wishlistProviderMock = $this->createMock(WishlistProvider::class); + $this->itemCarrierMock = $this->createMock(ItemCarrier::class); + $this->wishlistMock = $this->createMock(Wishlist::class); + $this->requestMock = $this->createMock(Http::class); + $resultFactoryMock = $this->createMock(ResultFactory::class); + $this->resultRedirectMock = $this->createMock(Redirect::class); + $this->resultForwardMock = $this->createMock(Forward::class); + + $resultFactoryMock->expects($this->any()) ->method('create') ->willReturnMap( [ @@ -105,19 +83,21 @@ protected function setUp(): void ] ); - $this->objectManagerHelper = new ObjectManagerHelper($this); - $this->context = $this->objectManagerHelper->getObject( + $objectManagerHelper = new ObjectManagerHelper($this); + $context = $objectManagerHelper->getObject( Context::class, [ 'request' => $this->requestMock, - 'resultFactory' => $this->resultFactoryMock + 'resultFactory' => $resultFactoryMock ] ); - - $this->allcartController = new Allcart( - $this->context, - $this->itemCarrierMock, - $this->wishlistProviderMock + $this->allcartController = $objectManagerHelper->getObject( + Allcart::class, + [ + 'context' => $context, + 'wishlistProvider' => $this->wishlistProviderMock, + 'itemCarrier' => $this->itemCarrierMock + ] ); } diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php index cee4b39ecfc2f..e6a127457a6c6 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php @@ -30,158 +30,146 @@ use PHPUnit\Framework\TestCase; /** + * Test for \Magento\Wishlist\Controller\Shared\Cart. + * * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CartTest extends TestCase { - /** @var SharedCart|MockObject */ - protected $model; - - /** @var RequestInterface|MockObject */ - protected $request; - - /** @var ManagerInterface|MockObject */ - protected $messageManager; - - /** @var ActionContext|MockObject */ - protected $context; - - /** @var Cart|MockObject */ - protected $cart; + /** + * @var SharedCart|MockObject + */ + private $model; - /** @var CartHelper|MockObject */ - protected $cartHelper; + /** + * @var RequestInterface|MockObject + */ + private $request; - /** @var Quote|MockObject */ - protected $quote; + /** + * @var ManagerInterface|MockObject + */ + private $messageManager; - /** @var OptionCollection|MockObject */ - protected $optionCollection; + /** + * @var Cart|MockObject + */ + private $cart; - /** @var OptionFactory|MockObject */ - protected $optionFactory; + /** + * @var CartHelper|MockObject + */ + private $cartHelper; - /** @var Option|MockObject */ - protected $option; + /** + * @var Quote|MockObject + */ + private $quote; - /** @var ItemFactory|MockObject */ - protected $itemFactory; + /** + * @var OptionCollection|MockObject + */ + private $optionCollection; - /** @var Item|MockObject */ - protected $item; + /** + * @var Option|MockObject + */ + private $option; - /** @var Escaper|MockObject */ - protected $escaper; + /** + * @var Item|MockObject + */ + private $item; - /** @var RedirectInterface|MockObject */ - protected $redirect; + /** + * @var Escaper|MockObject + */ + private $escaper; - /** @var ResultFactory|MockObject */ - protected $resultFactory; + /** + * @var RedirectInterface|MockObject + */ + private $redirect; - /** @var Redirect|MockObject */ - protected $resultRedirect; + /** + * @var Redirect|MockObject + */ + private $resultRedirect; - /** @var Product|MockObject */ - protected $product; + /** + * @var Product|MockObject + */ + private $product; /** - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @inheritDoc */ protected function setUp(): void { - $this->request = $this->getMockBuilder(RequestInterface::class) - ->getMockForAbstractClass(); - - $this->redirect = $this->getMockBuilder(RedirectInterface::class) - ->getMockForAbstractClass(); + $this->request = $this->getMockForAbstractClass(RequestInterface::class); + $this->redirect = $this->getMockForAbstractClass(RedirectInterface::class); + $this->messageManager = $this->getMockForAbstractClass(ManagerInterface::class); + $this->resultRedirect = $this->createMock(Redirect::class); - $this->messageManager = $this->getMockBuilder(ManagerInterface::class) - ->getMockForAbstractClass(); - - $this->resultRedirect = $this->getMockBuilder(Redirect::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->resultFactory = $this->getMockBuilder(ResultFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $this->resultFactory->expects($this->once()) + $resultFactory = $this->createMock(ResultFactory::class); + $resultFactory->expects($this->once()) ->method('create') ->with(ResultFactory::TYPE_REDIRECT) ->willReturn($this->resultRedirect); - $this->context = $this->getMockBuilder(ActionContext::class) + /** @var ActionContext|MockObject $context */ + $context = $this->getMockBuilder(ActionContext::class) ->disableOriginalConstructor() ->getMock(); - $this->context->expects($this->any()) + $context->expects($this->any()) ->method('getRequest') ->willReturn($this->request); - $this->context->expects($this->any()) + $context->expects($this->any()) ->method('getRedirect') ->willReturn($this->redirect); - $this->context->expects($this->any()) + $context->expects($this->any()) ->method('getMessageManager') ->willReturn($this->messageManager); - $this->context->expects($this->any()) + $context->expects($this->any()) ->method('getResultFactory') - ->willReturn($this->resultFactory); - - $this->cart = $this->getMockBuilder(Cart::class) - ->disableOriginalConstructor() - ->getMock(); + ->willReturn($resultFactory); - $this->cartHelper = $this->getMockBuilder(CartHelper::class) - ->disableOriginalConstructor() - ->getMock(); + $this->cart = $this->createMock(Cart::class); + $this->cartHelper = $this->createMock(CartHelper::class); $this->quote = $this->getMockBuilder(Quote::class) ->disableOriginalConstructor() - ->setMethods(['getHasError']) + ->addMethods(['getHasError']) ->getMock(); - $this->optionCollection = $this->getMockBuilder( - OptionCollection::class - )->disableOriginalConstructor()->getMock(); + $this->optionCollection = $this->createMock(OptionCollection::class); $this->option = $this->getMockBuilder(Option::class) ->disableOriginalConstructor() ->getMock(); - $this->optionFactory = $this->getMockBuilder(OptionFactory::class) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $this->optionFactory->expects($this->once()) + /** @var OptionFactory|MockObject $optionFactory */ + $optionFactory = $this->createMock(OptionFactory::class); + $optionFactory->expects($this->once()) ->method('create') ->willReturn($this->option); - $this->item = $this->getMockBuilder(Item::class) - ->disableOriginalConstructor() - ->getMock(); + $this->item = $this->createMock(Item::class); - $this->itemFactory = $this->getMockBuilder(ItemFactory::class) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $this->itemFactory->expects($this->once()) + $itemFactory = $this->createMock(ItemFactory::class); + $itemFactory->expects($this->once()) ->method('create') ->willReturn($this->item); - $this->escaper = $this->getMockBuilder(Escaper::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->product = $this->getMockBuilder(Product::class) - ->disableOriginalConstructor() - ->getMock(); + $this->escaper = $this->createMock(Escaper::class); + $this->product = $this->createMock(Product::class); $this->model = new SharedCart( - $this->context, + $context, $this->cart, - $this->optionFactory, - $this->itemFactory, + $optionFactory, + $itemFactory, $this->cartHelper, $this->escaper ); From c5f6a1f4a70e979f2fc31a8c4d714c713bd9e650 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Wed, 12 Aug 2020 12:41:19 +0300 Subject: [PATCH 14/15] fix http action --- app/code/Magento/Wishlist/Controller/Shared/Allcart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index c1e908cc0f49e..1c504a7a8a80d 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -10,7 +10,7 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; -use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; @@ -19,7 +19,7 @@ /** * Wishlist Allcart Controller */ -class Allcart extends Action implements HttpGetActionInterface +class Allcart extends Action implements HttpPostActionInterface { /** * @var WishlistProvider From a7da7fd6ff08388b1a2364c23710f303e82b33bf Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Wed, 12 Aug 2020 16:28:32 +0300 Subject: [PATCH 15/15] add get --- app/code/Magento/Wishlist/Controller/Shared/Allcart.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php index 1c504a7a8a80d..89413eff8323f 100644 --- a/app/code/Magento/Wishlist/Controller/Shared/Allcart.php +++ b/app/code/Magento/Wishlist/Controller/Shared/Allcart.php @@ -10,6 +10,7 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; +use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\Controller\Result\Forward; use Magento\Framework\Controller\Result\Redirect; @@ -19,7 +20,7 @@ /** * Wishlist Allcart Controller */ -class Allcart extends Action implements HttpPostActionInterface +class Allcart extends Action implements HttpGetActionInterface, HttpPostActionInterface { /** * @var WishlistProvider