public function testUpdateQuoteItemWithZeroQty() { $itemId = 1; $itemQty = 0; $this->resolverMock->expects($this->never())->method('getLocale'); $this->cartMock->expects($this->once())->method('updateItems')->with([$itemId => ['qty' => $itemQty]])->willReturnSelf(); $this->cartMock->expects($this->once())->method('save')->willReturnSelf(); $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty)); }
/** * @param bool $isAjax * @return array * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function prepareExecuteWithQuantityArray($isAjax = false) { $itemId = 2; $wishlistId = 1; $qty = [$itemId => 3]; $productId = 4; $productName = 'product_name'; $indexUrl = 'index_url'; $configureUrl = 'configure_url'; $options = [5 => 'option']; $params = ['item' => $itemId, 'qty' => $qty]; $refererUrl = 'referer_url'; $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($qty[$itemId])->willReturnArgument(0); $itemMock->expects($this->once())->method('setQty')->with($qty[$itemId])->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\\Resource\\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); $this->requestMock->expects($this->once())->method('isAjax')->willReturn($isAjax); $buyRequestMock = $this->getMockBuilder('Magento\\Framework\\Object')->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)->willReturn(true); $this->checkoutCartMock->expects($this->once())->method('save')->willReturnSelf(); $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['getHasError', 'collectTotals'])->getMock(); $this->checkoutCartMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock); $quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf(); $wishlistMock->expects($this->once())->method('save')->willReturnSelf(); $quoteMock->expects($this->once())->method('getHasError')->willReturn(false); $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $itemMock->expects($this->once())->method('getProduct')->willReturn($productMock); $productMock->expects($this->once())->method('getName')->willReturn($productName); $this->escaperMock->expects($this->once())->method('escapeHtml')->with($productName, null)->willReturn($productName); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with('You added ' . $productName . ' to your shopping cart.', null)->willReturnSelf(); $this->cartHelperMock->expects($this->once())->method('getShouldRedirectToCart')->willReturn(false); $this->redirectMock->expects($this->once())->method('getRefererUrl')->willReturn($refererUrl); $this->helperMock->expects($this->once())->method('calculate')->willReturnSelf(); return $refererUrl; }
/** * @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)); }