Skip to content

Commit

Permalink
Implement ActionInterface for Wishlist/Shared/Cart + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudolf Vince committed Mar 30, 2020
1 parent a002bf2 commit df2c759
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
64 changes: 44 additions & 20 deletions app/code/Magento/Wishlist/Controller/Shared/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
81 changes: 44 additions & 37 deletions app/code/Magento/Wishlist/Test/Unit/Controller/Shared/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,88 +25,90 @@
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())
->method('create')
->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())
Expand All @@ -120,62 +124,65 @@ 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();
$this->optionFactory->expects($this->once())
->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();
$this->itemFactory->expects($this->once())
->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
);
}

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit df2c759

Please sign in to comment.