/** * * @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() { $wishlistId = 1; $customerId = 2; $url = 'http://some.pending/url'; $message = 'some error msg'; $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $event = $this->getMockBuilder('Magento\\Framework\\Event')->setMethods(['getRequest', 'getResponse'])->disableOriginalConstructor()->getMock(); $request = $this->getMockBuilder('Magento\\Framework\\App\\RequestInterface')->getMock(); $response = $this->getMockBuilder('Magento\\Framework\\App\\ResponseInterface')->setMethods(['setRedirect'])->getMockForAbstractClass(); $wishlists = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Wishlist\\Collection')->disableOriginalConstructor()->getMock(); $loadedWishlist = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist\\Item')->setMethods(['getId', 'delete'])->disableOriginalConstructor()->getMock(); $eventObserver->expects($this->any())->method('getEvent')->willReturn($event); $request->expects($this->any())->method('getParam')->with('wishlist_next')->willReturn(true); $event->expects($this->once())->method('getRequest')->willReturn($request); $this->checkoutSession->expects($this->once())->method('getSharedWishlist'); $this->checkoutSession->expects($this->once())->method('getWishlistPendingMessages')->willReturn([$message]); $this->checkoutSession->expects($this->once())->method('getWishlistPendingUrls')->willReturn([$url]); $this->checkoutSession->expects($this->once())->method('getWishlistIds'); $this->checkoutSession->expects($this->once())->method('getSingleWishlistId')->willReturn($wishlistId); $this->customerSession->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->wishlist->expects($this->once())->method('loadByCustomerId')->with($this->logicalOr($customerId, true))->willReturnSelf(); $this->wishlist->expects($this->once())->method('getItemCollection')->willReturn($wishlists); $loadedWishlist->expects($this->once())->method('getId')->willReturn($wishlistId); $loadedWishlist->expects($this->once())->method('delete'); $wishlists->expects($this->once())->method('load')->willReturn([$loadedWishlist]); $this->checkoutSession->expects($this->once())->method('setWishlistIds')->with([])->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setSingleWishlistId')->with(null)->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setWishlistPendingUrls')->with([])->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setWishlistPendingMessages')->with([])->willReturnSelf(); $this->messageManager->expects($this->once())->method('addError')->with($message)->willReturnSelf(); $event->expects($this->once())->method('getResponse')->willReturn($response); $response->expects($this->once())->method('setRedirect')->with($url); $this->checkoutSession->expects($this->once())->method('setNoCartRedirect')->with(true); /** @var $eventObserver \Magento\Framework\Event\Observer */ $this->observer->execute($eventObserver); }
/** * Additional function to process forming description for wishlist item * * @param \Magento\Wishlist\Model\Wishlist $wishlistModelMock * @param array $staticArgs * @return string */ protected function processWishlistItemDescription($wishlistModelMock, $staticArgs) { $imgThumbSrc = 'http://source-for-thumbnail'; $priceHtmlForTest = '<div class="price">Price is 10 for example</div>'; $productDescription = 'Product description'; $productShortDescription = 'Product short description'; $wishlistItem = $this->getMock('Magento\\Wishlist\\Model\\Item', [], [], '', false); $wishlistItemsCollection = [$wishlistItem]; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getAllowedInRss', 'getAllowedPriceInRss', 'getDescription', 'getShortDescription', 'getName', 'getVisibleInSiteVisibilities', 'getUrlModel', '__wakeup'], [], '', false); $urlModelMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Url', [], [], '', false); $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\LayoutInterface', [], '', true, true, true, ['getBlock']); $wishlistModelMock->expects($this->once())->method('getItemCollection')->will($this->returnValue($wishlistItemsCollection)); $wishlistItem->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); $productMock->expects($this->once())->method('getUrlModel')->will($this->returnValue($urlModelMock)); $productMock->expects($this->once())->method('getAllowedPriceInRss')->will($this->returnValue($urlModelMock)); $urlModelMock->expects($this->once())->method('getUrl')->will($this->returnValue($staticArgs['productUrl'])); $productMock->expects($this->once())->method('getName')->will($this->returnValue($staticArgs['productName'])); $productMock->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getVisibleInSiteVisibilities')->will($this->returnValue(true)); $this->imageHelperMock->expects($this->once())->method('init')->will($this->returnSelf()); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $priceRendererMock = $this->getMock('Magento\\Framework\\Pricing\\Render', ['render'], [], '', false); $layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($priceRendererMock)); $priceRendererMock->expects($this->once())->method('render')->will($this->returnValue($priceHtmlForTest)); $productMock->expects($this->any())->method('getDescription')->will($this->returnValue($productDescription)); $productMock->expects($this->any())->method('getShortDescription')->will($this->returnValue($productShortDescription)); $this->catalogOutputMock->expects($this->any())->method('productAttribute')->will($this->returnArgument(1)); $this->block->setLayout($layoutMock); $description = '<table><tr><td><a href="' . $staticArgs['productUrl'] . '"><img src="' . $imgThumbSrc . '" border="0" align="left" height="75" width="75"></a></td><td style="text-decoration:none;">' . $productShortDescription . '<p>' . $priceHtmlForTest . '</p><p>Comment: ' . $productDescription . '<p>' . '</td></tr></table>'; return $description; }