Skip to content
Snippets Groups Projects
Commit 70292946 authored by Sviatoslav Mankivskyi's avatar Sviatoslav Mankivskyi
Browse files

MAGETWO-35331: Increase unit test coverage for Magento\Wishlist

parent f340c06f
Branches
No related merge requests found
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Test\Unit\Controller\Shared;
class AllcartTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Wishlist\Controller\Shared\WishlistProvider|\PHPUnit_Framework_MockObject_MockObject
*/
protected $wishlistProvider;
/**
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
*/
protected $request;
/**
* @var \Magento\Wishlist\Model\ItemCarrier|\PHPUnit_Framework_MockObject_MockObject
*/
protected $itemCarrier;
/**
* @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
*/
protected $response;
/**
* @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
*/
protected $context;
protected function setUp()
{
$this->wishlistProvider = $this->getMock(
'\Magento\Wishlist\Controller\Shared\WishlistProvider',
[],
[],
'',
false
);
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->itemCarrier = $this->getMock('Magento\Wishlist\Model\ItemCarrier', [], [], '', false);
$this->context = $this->getMock('Magento\Framework\App\Action\Context', [], [], '', false);
$this->response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
}
protected function prepareContext()
{
$om = $this->getMock('Magento\Framework\App\ObjectManager', [], [], '', false);
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
$url = $this->getMock('Magento\Framework\Url', [], [], '', false);
$actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', [], [], '', false);
$redirect = $this->getMock('\Magento\Store\App\Response\Redirect', [], [], '', false);
$view = $this->getMock('Magento\Framework\App\View', [], [], '', false);
$messageManager = $this->getMock('Magento\Framework\Message\Manager', [], [], '', false);
$this->context
->expects($this->any())
->method('getObjectManager')
->will($this->returnValue($om));
$this->context
->expects($this->any())
->method('getRequest')
->will($this->returnValue($this->request));
$this->context
->expects($this->any())
->method('getResponse')
->will($this->returnValue($this->response));
$this->context
->expects($this->any())
->method('getEventManager')
->will($this->returnValue($eventManager));
$this->context
->expects($this->any())
->method('getUrl')
->will($this->returnValue($url));
$this->context
->expects($this->any())
->method('getActionFlag')
->will($this->returnValue($actionFlag));
$this->context
->expects($this->any())
->method('getRedirect')
->will($this->returnValue($redirect));
$this->context
->expects($this->any())
->method('getView')
->will($this->returnValue($view));
$this->context
->expects($this->any())
->method('getMessageManager')
->will($this->returnValue($messageManager));
}
public function getController()
{
$this->prepareContext();
return new \Magento\Wishlist\Controller\Shared\Allcart(
$this->context,
$this->wishlistProvider,
$this->itemCarrier
);
}
public function testExecuteWithNoWishlist()
{
$this->wishlistProvider->expects($this->once())
->method('getWishlist')
->willReturn(false);
$this->request
->expects($this->once())
->method('initForward')
->will($this->returnValue(true));
$this->request
->expects($this->once())
->method('setActionName')
->with('noroute')
->will($this->returnValue(true));
$this->request
->expects($this->once())
->method('setDispatched')
->with(false)
->will($this->returnValue(true));
$controller = $this->getController();
$controller->execute();
}
public function testExecuteWithWishlist()
{
$wishlist = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist')
->disableOriginalConstructor()
->getMock();
$this->wishlistProvider->expects($this->once())
->method('getWishlist')
->willReturn($wishlist);
$this->request
->expects($this->once())
->method('getParam')
->with('qty')
->will($this->returnValue(2));
$this->itemCarrier
->expects($this->once())
->method('moveAllToCart')
->with($wishlist, 2)
->will($this->returnValue('http://redirect-url.com'));
$this->response
->expects($this->once())
->method('setRedirect')
->will($this->returnValue('http://redirect-url.com'));
$controller = $this->getController();
$controller->execute();
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Test\Unit\Helper;
class RssTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Wishlist\Helper\Rss
*/
protected $model;
/**
* @var \Magento\Wishlist\Model\WishlistFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $wishlistFactoryMock;
/**
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $requestMock;
/**
* @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $urlDecoderMock;
/**
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerFactoryMock;
/**
* @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerSessionMock;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerRepositoryMock;
/**
* @var \Magento\Framework\Module\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleManagerMock;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $scopeConfigMock;
public function setUp()
{
$this->wishlistFactoryMock = $this->getMockBuilder('Magento\Wishlist\Model\WishlistFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
->getMock();
$this->urlDecoderMock = $this->getMockBuilder('Magento\Framework\Url\DecoderInterface')
->getMock();
$this->customerFactoryMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterfaceFactory')
->disableOriginalConstructor()
->getMock();
$this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface')
->getMock();
$this->moduleManagerMock = $this->getMockBuilder('Magento\Framework\Module\Manager')
->disableOriginalConstructor()
->getMock();
$this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
->getMock();
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->model = $objectManager->getObject(
'Magento\Wishlist\Helper\Rss',
[
'wishlistFactory' => $this->wishlistFactoryMock,
'httpRequest' => $this->requestMock,
'urlDecoder' => $this->urlDecoderMock,
'customerFactory' => $this->customerFactoryMock,
'customerSession' => $this->customerSessionMock,
'customerRepository' => $this->customerRepositoryMock,
'moduleManager' => $this->moduleManagerMock,
'scopeConfig' => $this->scopeConfigMock,
]
);
}
public function testGetWishlistWithWishlistId()
{
$wishlistId = 1;
$wishlist = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist')
->disableOriginalConstructor()
->getMock();
$this->wishlistFactoryMock->expects($this->once())
->method('create')
->with([])
->willReturn($wishlist);
$this->requestMock->expects($this->once())
->method('getParam')
->with('wishlist_id', null)
->willReturn($wishlistId);
$wishlist->expects($this->once())
->method('load')
->with($wishlistId, null)
->willReturnSelf();
$this->assertEquals($wishlist, $this->model->getWishlist());
// Check that wishlist is cached
$this->assertSame($wishlist, $this->model->getWishlist());
}
public function testGetWishlistWithCustomerId()
{
$customerId = 1;
$data = $customerId . ',2';
$wishlist = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist')
->disableOriginalConstructor()
->getMock();
$this->wishlistFactoryMock->expects($this->once())
->method('create')
->with([])
->willReturn($wishlist);
$this->requestMock->expects($this->at(0))
->method('getParam')
->with('wishlist_id', null)
->willReturn('');
$this->urlDecoderMock->expects($this->any())
->method('decode')
->willReturnArgument(0);
$this->requestMock->expects($this->at(1))
->method('getParam')
->with('data', null)
->willReturn($data);
$this->customerSessionMock->expects($this->once())
->method('getCustomerId')
->willReturn(0);
$customer = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerFactoryMock->expects($this->once())
->method('create')
->with([])
->willReturn($customer);
$this->customerRepositoryMock->expects($this->never())
->method('getById');
$customer->expects($this->exactly(2))
->method('getId')
->willReturn($customerId);
$wishlist->expects($this->once())
->method('loadByCustomerId')
->with($customerId, false)
->willReturnSelf();
$this->assertEquals($wishlist, $this->model->getWishlist());
}
public function testGetCustomerWithSession()
{
$customerId = 1;
$data = $customerId . ',2';
$this->urlDecoderMock->expects($this->any())
->method('decode')
->willReturnArgument(0);
$this->requestMock->expects($this->once())
->method('getParam')
->with('data', null)
->willReturn($data);
$this->customerSessionMock->expects($this->once())
->method('getCustomerId')
->willReturn($customerId);
$customer = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId)
->willReturn($customer);
$this->customerFactoryMock->expects($this->never())
->method('create');
$this->assertEquals($customer, $this->model->getCustomer());
// Check that customer is cached
$this->assertSame($customer, $this->model->getCustomer());
}
/**
* @param bool $isModuleEnabled
* @param bool $isWishlistActive
* @param bool $result
* @dataProvider dataProviderIsRssAllow
*/
public function testIsRssAllow($isModuleEnabled, $isWishlistActive, $result)
{
$this->moduleManagerMock->expects($this->once())
->method('isEnabled')
->with('Magento_Rss')
->willReturn($isModuleEnabled);
$this->scopeConfigMock->expects($this->any())
->method('isSetFlag')
->with('rss/wishlist/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
->willReturn($isWishlistActive);
$this->assertEquals($result, $this->model->isRssAllow());
}
/**
* @return array
*/
public function dataProviderIsRssAllow()
{
return [
[false, false, false],
[true, false, false],
[false, true, false],
[true, true, true],
];
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\Wishlist\Helper;
class RssTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* Core data
*
* @var \Magento\Framework\Url\EncoderInterface
*/
protected $urlEncoder;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var \Magento\Framework\App\Helper\Context
*/
protected $_contextHelper;
/**
* @var \Magento\Wishlist\Helper\Rss
*/
protected $_wishlistHelper;
/**
* @var int
*/
protected $_fixtureCustomerId;
protected function setUp()
{
$this->_fixtureCustomerId = 1;
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_customerSession = $this->_objectManager->create('Magento\Customer\Model\Session');
$this->urlEncoder = $this->_objectManager->create('Magento\Framework\Url\EncoderInterface');
$this->_contextHelper = $this->_objectManager->create('Magento\Framework\App\Helper\Context');
$request = $this->_contextHelper->getRequest();
$request->setParam('data', $this->urlEncoder->encode($this->_fixtureCustomerId));
$this->_wishlistHelper = $this->_objectManager->create('Magento\Wishlist\Helper\Rss',
[
'context' => $this->_contextHelper,
'customerSession' => $this->_customerSession
]
);
$this->_customerSession->loginById($this->_fixtureCustomerId);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoAppArea frontend
*/
public function testGetCustomer()
{
$expectedCustomer = $this->_customerSession->getCustomerDataObject();
$actualCustomer = $this->_wishlistHelper->getCustomer();
$this->assertInstanceOf('Magento\Customer\Api\Data\CustomerInterface', $actualCustomer);
$this->assertEquals((int)$expectedCustomer->getId(), (int)$actualCustomer->getId());
$this->assertEquals((int)$expectedCustomer->getWebsiteId(), (int)$actualCustomer->getWebsiteId());
$this->assertEquals((int)$expectedCustomer->getStoreId(), (int)$actualCustomer->getStoreId());
$this->assertEquals((int)$expectedCustomer->getGroupId(), (int)$actualCustomer->getGroupId());
$this->assertEquals($expectedCustomer->getCustomAttributes(), $actualCustomer->getCustomAttributes());
$this->assertEquals($expectedCustomer->getFirstname(), $actualCustomer->getFirstname());
$this->assertEquals($expectedCustomer->getLastname(), $actualCustomer->getLastname());
$this->assertEquals($expectedCustomer->getEmail(), $actualCustomer->getEmail());
$this->assertEquals($expectedCustomer->getEmail(), $actualCustomer->getEmail());
$this->assertEquals((int)$expectedCustomer->getDefaultBilling(), (int)$actualCustomer->getDefaultBilling());
$this->assertEquals((int)$expectedCustomer->getDefaultShipping(), (int)$actualCustomer->getDefaultShipping());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_product_qty_increments.php
* @magentoAppArea frontend
*/
public function testGetWishlistByParam()
{
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
$wishlist = $this->_objectManager->create('Magento\Wishlist\Model\Wishlist')
->loadByCustomerId($this->_fixtureCustomerId);
$wishlist->load($wishlist->getId());
/** @var \Magento\Framework\App\Request\Http $request */
$request = $this->_contextHelper->getRequest();
$request->setParam('wishlist_id', $wishlist->getId());
$this->assertEquals($wishlist, $this->_wishlistHelper->getWishlist());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_product_qty_increments.php
* @magentoAppArea frontend
*/
public function testGetWishlistByCustomerId()
{
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
$wishlist = $this->_objectManager->create('Magento\Wishlist\Model\Wishlist')
->loadByCustomerId($this->_fixtureCustomerId);
/** @var \Magento\Framework\App\Request\Http $request */
$request = $this->_contextHelper->getRequest();
$request->setParam('wishlist_id', '');
$this->assertEquals($wishlist, $this->_wishlistHelper->getWishlist());
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment