/** * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute() { $customerId = 1; $itemId = 5; $itemQty = 123; $productId = 321; $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $event = $this->getMockBuilder('Magento\\Framework\\Event')->setMethods(['getCart', 'getInfo'])->disableOriginalConstructor()->getMock(); $eventObserver->expects($this->exactly(2))->method('getEvent')->willReturn($event); $quoteItem = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->setMethods(['getProductId', 'getBuyRequest', '__wakeup'])->disableOriginalConstructor()->getMock(); $buyRequest = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['setQty'])->disableOriginalConstructor()->getMock(); $infoData = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['toArray'])->disableOriginalConstructor()->getMock(); $infoData->expects($this->once())->method('toArray')->willReturn([$itemId => ['qty' => $itemQty, 'wishlist' => true]]); $cart = $this->getMockBuilder('Magento\\Checkout\\Model\\Cart')->disableOriginalConstructor()->getMock(); $quote = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->setMethods(['getCustomerId', 'getItemById', 'removeItem', '__wakeup'])->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getCart')->willReturn($cart); $event->expects($this->once())->method('getInfo')->willReturn($infoData); $cart->expects($this->any())->method('getQuote')->willReturn($quote); $quoteItem->expects($this->once())->method('getProductId')->willReturn($productId); $quoteItem->expects($this->once())->method('getBuyRequest')->willReturn($buyRequest); $buyRequest->expects($this->once())->method('setQty')->with($itemQty)->willReturnSelf(); $quote->expects($this->once())->method('getCustomerId')->willReturn($customerId); $quote->expects($this->once())->method('getItemById')->with($itemId)->willReturn($quoteItem); $quote->expects($this->once())->method('removeItem')->with($itemId); $this->wishlist->expects($this->once())->method('loadByCustomerId')->with($this->logicalOr($customerId, true))->willReturnSelf(); $this->wishlist->expects($this->once())->method('addNewItem')->with($this->logicalOr($productId, $buyRequest)); $this->wishlist->expects($this->once())->method('save'); $this->helper->expects($this->once())->method('calculate'); /** @var $eventObserver \Magento\Framework\Event\Observer */ $this->assertSame($this->observer, $this->observer->execute($eventObserver)); }
public function testExecute() { $event = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); /** @var $event \Magento\Framework\Event\Observer */ $this->helper->expects($this->once())->method('calculate'); $this->observer->execute($event); }
public function testGetMoveFromCartParams() { $itemId = 45; $json = '{json;}'; /** * @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock */ $itemMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->disableOriginalConstructor()->getMock(); $itemMock->expects($this->once())->method('getId')->willReturn($itemId); $this->wishlistHelperMock->expects($this->once())->method('getMoveFromCartParams')->with($itemId)->willReturn($json); $this->model->setItem($itemMock); $this->assertEquals($json, $this->model->getMoveFromCartParams()); }
protected function setUp() { $wishlist = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', 'getSharingCode'], [], '', false); $wishlist->expects($this->any())->method('getId')->will($this->returnValue(5)); $wishlist->expects($this->any())->method('getSharingCode')->will($this->returnValue('somesharingcode')); $customer = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $customer->expects($this->any())->method('getId')->will($this->returnValue(8)); $customer->expects($this->any())->method('getEmail')->will($this->returnValue('*****@*****.**')); $this->wishlistHelper = $this->getMock('Magento\\Wishlist\\Helper\\Data', ['getWishlist', 'getCustomer', 'urlEncode'], [], '', false); $this->urlEncoder = $this->getMock('Magento\\Framework\\Url\\EncoderInterface', ['encode'], [], '', false); $this->wishlistHelper->expects($this->any())->method('getWishlist')->will($this->returnValue($wishlist)); $this->wishlistHelper->expects($this->any())->method('getCustomer')->will($this->returnValue($customer)); $this->urlEncoder->expects($this->any())->method('encode')->willReturnCallback(function ($url) { return strtr(base64_encode($url), '+/=', '-_,'); }); $this->urlBuilder = $this->getMock('Magento\\Framework\\App\\Rss\\UrlBuilderInterface'); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->link = $this->objectManagerHelper->getObject('Magento\\Wishlist\\Block\\Rss\\EmailLink', ['wishlistHelper' => $this->wishlistHelper, 'rssUrlBuilder' => $this->urlBuilder, 'urlEncoder' => $this->urlEncoder]); }
/** * Test for method _toHtml for the case, when wishlist is absent */ public function testToHtmlWithoutWishlist() { $url = 'http://base.url/index'; $rssString = '<xml>Some empty xml</xml>'; $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $customerServiceMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', [], [], '', false); $wishlistModelMock = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', '__wakeup', 'getCustomerId'], [], '', false); $expectedHeaders = ['title' => __('We cannot retrieve the wish list.'), 'description' => __('We cannot retrieve the wish list.'), 'link' => $url, 'charset' => 'UTF-8']; $this->rssFactoryMock->expects($this->once())->method('create')->will($this->returnValue($rssObjMock)); $this->wishlistHelperMock->expects($this->once())->method('getWishlist')->will($this->returnValue($wishlistModelMock)); $wishlistModelMock->expects($this->once())->method('getId')->will($this->returnValue(false)); $this->urlBuilderMock->expects($this->once())->method('getUrl')->will($this->returnValue($url)); $this->wishlistHelperMock->expects($this->once())->method('getCustomer')->will($this->returnValue($customerServiceMock)); $rssObjMock->expects($this->once())->method('_addHeader')->with($expectedHeaders)->will($this->returnSelf()); $rssObjMock->expects($this->once())->method('createRssXml')->will($this->returnValue($rssString)); $this->assertEquals($rssString, $this->block->toHtml()); }
public function testGetSectionDataWithoutItems() { $items = []; $result = ['counter' => null, 'items' => []]; $this->wishlistHelperMock->expects($this->once())->method('getItemCount')->willReturn(count($items)); $this->viewMock->expects($this->never())->method('loadLayout'); $this->wishlistHelperMock->expects($this->never())->method('getWishlistItemCollection'); $this->catalogImageHelperMock->expects($this->never())->method('init'); $this->catalogImageHelperMock->expects($this->never())->method('getUrl'); $this->catalogImageHelperMock->expects($this->never())->method('getLabel'); $this->catalogImageHelperMock->expects($this->never())->method('getWidth'); $this->catalogImageHelperMock->expects($this->never())->method('getHeight'); $this->wishlistHelperMock->expects($this->never())->method('getProductUrl'); $this->sidebarMock->expects($this->never())->method('getProductPriceHtml'); $this->wishlistHelperMock->expects($this->never())->method('getAddToCartParams'); $this->wishlistHelperMock->expects($this->never())->method('getRemoveParams'); $this->assertEquals($result, $this->model->getSectionData()); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithoutQuantityArrayAndConfigurable() { $itemId = 2; $wishlistId = 1; $qty = []; $productId = 4; $indexUrl = 'index_url'; $configureUrl = 'configure_url'; $options = [5 => 'option']; $params = ['item' => $itemId, 'qty' => $qty]; $this->formKeyValidator->expects($this->once())->method('validate')->with($this->requestMock)->willReturn(true); $itemMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->setMethods(['load', 'getId', 'getWishlistId', 'setQty', 'setOptions', 'getBuyRequest', 'mergeBuyRequest', 'addToCart', 'getProduct', 'getProductId'])->getMock(); $this->requestMock->expects($this->at(0))->method('getParam')->with('item', null)->willReturn($itemId); $this->itemFactoryMock->expects($this->once())->method('create')->willReturn($itemMock); $itemMock->expects($this->once())->method('load')->with($itemId, null)->willReturnSelf(); $itemMock->expects($this->exactly(2))->method('getId')->willReturn($itemId); $itemMock->expects($this->once())->method('getWishlistId')->willReturn($wishlistId); $wishlistMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist')->disableOriginalConstructor()->getMock(); $this->wishlistProviderMock->expects($this->once())->method('getWishlist')->with($wishlistId)->willReturn($wishlistMock); $this->requestMock->expects($this->at(1))->method('getParam')->with('qty', null)->willReturn($qty); $this->quantityProcessorMock->expects($this->once())->method('process')->with(1)->willReturnArgument(0); $itemMock->expects($this->once())->method('setQty')->with(1)->willReturnSelf(); $this->urlMock->expects($this->at(0))->method('getUrl')->with('*/*', null)->willReturn($indexUrl); $itemMock->expects($this->once())->method('getProductId')->willReturn($productId); $this->urlMock->expects($this->at(1))->method('getUrl')->with('*/*/configure/', ['id' => $itemId, 'product_id' => $productId])->willReturn($configureUrl); $optionMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item\\Option')->disableOriginalConstructor()->getMock(); $this->optionFactoryMock->expects($this->once())->method('create')->willReturn($optionMock); $optionsMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Item\\Option\\Collection')->disableOriginalConstructor()->getMock(); $optionMock->expects($this->once())->method('getCollection')->willReturn($optionsMock); $optionsMock->expects($this->once())->method('addItemFilter')->with([$itemId])->willReturnSelf(); $optionsMock->expects($this->once())->method('getOptionsByItem')->with($itemId)->willReturn($options); $itemMock->expects($this->once())->method('setOptions')->with($options)->willReturnSelf(); $this->requestMock->expects($this->once())->method('getParams')->willReturn($params); $buyRequestMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $itemMock->expects($this->once())->method('getBuyRequest')->willReturn($buyRequestMock); $this->productHelperMock->expects($this->once())->method('addParamsToBuyRequest')->with($params, ['current_config' => $buyRequestMock])->willReturn($buyRequestMock); $itemMock->expects($this->once())->method('mergeBuyRequest')->with($buyRequestMock)->willReturnSelf(); $itemMock->expects($this->once())->method('addToCart')->with($this->checkoutCartMock, true)->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message'))); $this->messageManagerMock->expects($this->once())->method('addNotice')->with('message', null)->willReturnSelf(); $this->helperMock->expects($this->once())->method('calculate')->willReturnSelf(); $this->resultRedirectMock->expects($this->once())->method('setUrl')->with($configureUrl)->willReturnSelf(); $this->assertSame($this->resultRedirectMock, $this->model->execute()); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testMoveAllToCartWithException() { $wishlistId = 7; $sessionCustomerId = 23; $itemOneId = 14; $itemTwoId = 17; $productOneName = 'product one'; $productTwoName = 'product two'; $qtys = [14 => 21]; $isOwner = true; $indexUrl = 'index_url'; /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemOneMock */ $itemOneMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])->disableOriginalConstructor()->getMock(); /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemTwoMock */ $itemTwoMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])->disableOriginalConstructor()->getMock(); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productOneMock */ $productOneMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])->disableOriginalConstructor()->getMock(); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productTwoMock */ $productTwoMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])->disableOriginalConstructor()->getMock(); $itemOneMock->expects($this->any())->method('getProduct')->willReturn($productOneMock); $itemTwoMock->expects($this->any())->method('getProduct')->willReturn($productTwoMock); $collection = [$itemOneMock, $itemTwoMock]; /** @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject $wishlistMock */ $wishlistMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist')->disableOriginalConstructor()->getMock(); $this->sessionMock->expects($this->once())->method('getCustomerId')->willReturn($sessionCustomerId); $wishlistMock->expects($this->once())->method('isOwner')->with($sessionCustomerId)->willReturn($isOwner); $wishlistMock->expects($this->once())->method('getId')->willReturn($wishlistId); /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */ $collectionMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Item\\Collection')->disableOriginalConstructor()->getMock(); $wishlistMock->expects($this->once())->method('getItemCollection')->willReturn($collectionMock); $collectionMock->expects($this->once())->method('setVisibilityFilter')->with(true)->willReturn($collection); $productOneMock->expects($this->once())->method('getDisableAddToCart')->willReturn(true); $productOneMock->expects($this->once())->method('setDisableAddToCart')->with(true); $productTwoMock->expects($this->once())->method('getDisableAddToCart')->willReturn(false); $productTwoMock->expects($this->once())->method('setDisableAddToCart')->with(false); $itemOneMock->expects($this->once())->method('unsProduct'); $itemTwoMock->expects($this->once())->method('unsProduct'); $itemOneMock->expects($this->exactly(2))->method('getId')->willReturn($itemOneId); $itemTwoMock->expects($this->once())->method('getId')->willReturn($itemTwoId); $this->quantityProcessorMock->expects($this->once())->method('process')->with($qtys[$itemOneId])->willReturnArgument(0); $itemOneMock->expects($this->once())->method('setQty')->with($qtys[$itemOneId])->willReturnSelf(); $itemTwoMock->expects($this->never())->method('setQty'); $itemOneMock->expects($this->once())->method('addToCart')->with($this->cartMock, $isOwner)->willReturn(true); $exception = new \Exception('Exception.'); $itemTwoMock->expects($this->once())->method('addToCart')->with($this->cartMock, $isOwner)->willThrowException($exception); $this->loggerMock->expects($this->once())->method('critical')->with($exception, []); $this->managerMock->expects($this->at(0))->method('addError')->with(__('We can\'t add this item to your shopping cart right now.'), null)->willReturnSelf(); $this->wishlistHelperMock->expects($this->once())->method('getListUrl')->with($wishlistId)->willReturn($indexUrl); $this->cartHelperMock->expects($this->once())->method('getShouldRedirectToCart')->with(null)->willReturn(false); $this->redirectMock->expects($this->once())->method('getRefererUrl')->willReturn(''); $wishlistMock->expects($this->once())->method('save')->willThrowException(new \Exception()); $this->managerMock->expects($this->at(1))->method('addError')->with(__('We can\'t update the Wish List right now.'), null)->willReturnSelf(); $productOneMock->expects($this->any())->method('getName')->willReturn($productOneName); $productTwoMock->expects($this->any())->method('getName')->willReturn($productTwoName); $this->managerMock->expects($this->once())->method('addSuccess')->with(__('%1 product(s) have been added to shopping cart: %2.', 1, '"' . $productOneName . '"'), null)->willReturnSelf(); $this->cartMock->expects($this->once())->method('save')->willReturnSelf(); /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $collectionMock */ $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->getMock(); $this->cartMock->expects($this->once())->method('getQuote')->willReturn($quoteMock); $quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf(); $this->wishlistHelperMock->expects($this->once())->method('calculate')->willReturnSelf(); $this->assertEquals($indexUrl, $this->model->moveAllToCart($wishlistMock, $qtys)); }