Esempio n. 1
0
 public function testGetConfigureUrl()
 {
     $json = '{json;}';
     /**
      * @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock
      */
     $itemMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->disableOriginalConstructor()->getMock();
     $this->cartHelperMock->expects($this->once())->method('getDeletePostJson')->with($itemMock)->willReturn($json);
     $this->model->setItem($itemMock);
     $this->assertEquals($json, $this->model->getDeletePostJson());
 }
 public function testGetAddToCartPostParams()
 {
     $url = 'http://localhost.com/dev/';
     $id = 1;
     $uenc = strtr(base64_encode($url), '+/=', '-_,');
     $expectedPostData = ['action' => $url, 'data' => ['product' => $id, 'uenc' => $uenc]];
     $this->typeInstanceMock->expects($this->once())->method('hasRequiredOptions')->with($this->equalTo($this->productMock))->will($this->returnValue(false));
     $this->cartHelperMock->expects($this->any())->method('getAddUrl')->with($this->equalTo($this->productMock), $this->equalTo([]))->will($this->returnValue($url));
     $this->productMock->expects($this->once())->method('getEntityId')->will($this->returnValue($id));
     $this->productMock->expects($this->once())->method('getTypeInstance')->will($this->returnValue($this->typeInstanceMock));
     $this->urlHelperMock->expects($this->once())->method('getEncodedUrl')->with($this->equalTo($url))->will($this->returnValue($uenc));
     $result = $this->block->getAddToCartPostParams($this->productMock);
     $this->assertEquals($expectedPostData, $result);
 }
Esempio n. 3
0
 /**
  * @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;
 }
Esempio n. 4
0
 public function testGetAddToCartPostParams()
 {
     $url = 'http://localhost.com/dev/';
     $id = 1;
     $uenc = strtr(base64_encode($url), '+/=', '-_,');
     $data = array('product' => $id, \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $uenc);
     $expectedPostData = json_encode(array('action' => $url, 'data' => array('product' => $id, 'uenc' => $uenc)));
     $this->typeInstanceMock->expects($this->once())->method('hasRequiredOptions')->with($this->equalTo($this->productMock))->will($this->returnValue(false));
     $this->cartHelperMock->expects($this->any())->method('getAddUrl')->with($this->equalTo($this->productMock), $this->equalTo(array()))->will($this->returnValue($url));
     $this->productMock->expects($this->once())->method('getEntityId')->will($this->returnValue($id));
     $this->productMock->expects($this->once())->method('getTypeInstance')->will($this->returnValue($this->typeInstanceMock));
     $this->postDataHelperMock->expects($this->once())->method('getEncodedUrl')->with($this->equalTo($url))->will($this->returnValue($uenc));
     $this->postDataHelperMock->expects($this->once())->method('getPostData')->with($this->equalTo($url), $this->equalTo($data))->will($this->returnValue($expectedPostData));
     $result = $this->block->getAddToCartPostParams($this->productMock);
     $this->assertEquals($expectedPostData, $result);
 }
 /**
  * @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));
 }