| Current Path : /home/rtorresani/www/vendor/magento/module-wishlist/Test/Unit/Controller/Index/ |
| Current File : //home/rtorresani/www/vendor/magento/module-wishlist/Test/Unit/Controller/Index/AllcartTest.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Test\Unit\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\View;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Event\Manager;
use Magento\Framework\Url;
use Magento\Wishlist\Controller\Index\Allcart;
use Magento\Wishlist\Controller\WishlistProvider;
use Magento\Wishlist\Controller\WishlistProviderInterface;
use Magento\Wishlist\Model\ItemCarrier;
use Magento\Wishlist\Model\Wishlist;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AllcartTest extends TestCase
{
/**
* @var WishlistProviderInterface|MockObject
*/
protected $wishlistProvider;
/**
* @var ItemCarrier|MockObject
*/
protected $itemCarrier;
/**
* @var Validator|MockObject
*/
protected $formKeyValidator;
/**
* @var Context|MockObject
*/
protected $context;
/**
* @var Http|MockObject
*/
protected $request;
/**
* @var \Magento\Framework\App\Response\Http|MockObject
*/
protected $response;
/**
* @var ResultFactory|MockObject
*/
protected $resultFactoryMock;
/**
* @var Redirect|MockObject
*/
protected $resultRedirectMock;
/**
* @var Forward|MockObject
*/
protected $resultForwardMock;
protected function setUp(): void
{
$this->context = $this->createMock(Context::class);
$this->wishlistProvider = $this->createMock(WishlistProvider::class);
$this->itemCarrier = $this->createMock(ItemCarrier::class);
$this->formKeyValidator = $this->createMock(Validator::class);
$this->request = $this->createMock(Http::class);
$this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
$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]
]
);
}
protected function prepareContext()
{
$om = $this->createMock(ObjectManager::class);
$eventManager = $this->createMock(Manager::class);
$url = $this->createMock(Url::class);
$actionFlag = $this->createMock(ActionFlag::class);
$redirect = $this->createMock(\Magento\Store\App\Response\Redirect::class);
$view = $this->createMock(View::class);
$messageManager = $this->createMock(\Magento\Framework\Message\Manager::class);
$this->context
->expects($this->any())
->method('getObjectManager')
->willReturn($om);
$this->context
->expects($this->any())
->method('getRequest')
->willReturn($this->request);
$this->context
->expects($this->any())
->method('getResponse')
->willReturn($this->response);
$this->context
->expects($this->any())
->method('getEventManager')
->willReturn($eventManager);
$this->context
->expects($this->any())
->method('getUrl')
->willReturn($url);
$this->context
->expects($this->any())
->method('getActionFlag')
->willReturn($actionFlag);
$this->context
->expects($this->any())
->method('getRedirect')
->willReturn($redirect);
$this->context
->expects($this->any())
->method('getView')
->willReturn($view);
$this->context
->expects($this->any())
->method('getMessageManager')
->willReturn($messageManager);
$this->context->expects($this->any())
->method('getResultFactory')
->willReturn($this->resultFactoryMock);
}
/**
* @return Allcart
*/
public function getController()
{
$this->prepareContext();
return new Allcart(
$this->context,
$this->wishlistProvider,
$this->formKeyValidator,
$this->itemCarrier
);
}
public function testExecuteInvalidFormKey()
{
$this->formKeyValidator
->expects($this->once())
->method('validate')
->with($this->request)
->willReturn(false);
$this->resultForwardMock->expects($this->once())
->method('forward')
->with('noroute')
->willReturnSelf();
$controller = $this->getController();
$this->assertSame($this->resultForwardMock, $controller->execute());
}
public function testExecuteWithoutWishlist()
{
$this->formKeyValidator
->expects($this->once())
->method('validate')
->with($this->request)
->willReturn(true);
$this->wishlistProvider
->expects($this->once())
->method('getWishlist')
->willReturn(null);
$this->resultForwardMock->expects($this->once())
->method('forward')
->with('noroute')
->willReturnSelf();
$this->assertSame($this->resultForwardMock, $this->getController()->execute());
}
public function testExecutePassed()
{
$url = 'http://redirect-url.com';
$wishlist = $this->createMock(Wishlist::class);
$this->formKeyValidator->expects($this->once())
->method('validate')
->with($this->request)
->willReturn(true);
$this->request->expects($this->once())
->method('getParam')
->with('qty')
->willReturn(2);
$this->wishlistProvider->expects($this->once())
->method('getWishlist')
->willReturn($wishlist);
$this->itemCarrier->expects($this->once())
->method('moveAllToCart')
->with($wishlist, 2)
->willReturn($url);
$this->resultRedirectMock->expects($this->once())
->method('setUrl')
->with($url)
->willReturnSelf();
$this->assertSame($this->resultRedirectMock, $this->getController()->execute());
}
}