AddToCartTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\Observer;
  7. use \Magento\Wishlist\Observer\AddToCart as Observer;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class AddToCartTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Observer
  15. */
  16. protected $observer;
  17. /**
  18. * @var \Magento\Wishlist\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $helper;
  21. /**
  22. * @var \Magento\Checkout\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $checkoutSession;
  25. /**
  26. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $customerSession;
  29. /**
  30. * @var \Magento\Wishlist\Model\WishlistFactory|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $wishlistFactory;
  33. /**
  34. * @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $wishlist;
  37. /**
  38. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $messageManager;
  41. protected function setUp()
  42. {
  43. $this->checkoutSession = $this->getMockBuilder(
  44. \Magento\Checkout\Model\Session::class
  45. )->setMethods(
  46. [
  47. 'getSharedWishlist',
  48. 'getWishlistPendingMessages',
  49. 'getWishlistPendingUrls',
  50. 'getWishlistIds',
  51. 'getSingleWishlistId',
  52. 'setSingleWishlistId',
  53. 'setWishlistIds',
  54. 'setWishlistPendingUrls',
  55. 'setWishlistPendingMessages',
  56. 'setNoCartRedirect',
  57. ]
  58. )->disableOriginalConstructor()->getMock();
  59. $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['setWishlistItemCount', 'isLoggedIn', 'getCustomerId'])
  62. ->getMock();
  63. $this->wishlistFactory = $this->getMockBuilder(\Magento\Wishlist\Model\WishlistFactory::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods(['create'])
  66. ->getMock();
  67. $this->wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  71. ->getMock();
  72. $this->wishlistFactory->expects($this->any())
  73. ->method('create')
  74. ->willReturn($this->wishlist);
  75. $this->observer = new Observer(
  76. $this->checkoutSession,
  77. $this->customerSession,
  78. $this->wishlistFactory,
  79. $this->messageManager
  80. );
  81. }
  82. public function testExecute()
  83. {
  84. $wishlistId = 1;
  85. $customerId = 2;
  86. $url = 'http://some.pending/url';
  87. $message = 'some error msg';
  88. $eventObserver = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $event = $this->getMockBuilder(\Magento\Framework\Event::class)
  92. ->setMethods(['getRequest', 'getResponse'])
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)->getMock();
  96. $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  97. ->setMethods(['setRedirect'])
  98. ->getMockForAbstractClass();
  99. $wishlists = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Wishlist\Collection::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $loadedWishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist\Item::class)
  103. ->setMethods(['getId', 'delete'])
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $eventObserver->expects($this->any())->method('getEvent')->willReturn($event);
  107. $request->expects($this->any())->method('getParam')->with('wishlist_next')->willReturn(true);
  108. $event->expects($this->once())->method('getRequest')->willReturn($request);
  109. $this->checkoutSession->expects($this->once())->method('getSharedWishlist');
  110. $this->checkoutSession->expects($this->once())->method('getWishlistPendingMessages')->willReturn([$message]);
  111. $this->checkoutSession->expects($this->once())->method('getWishlistPendingUrls')->willReturn([$url]);
  112. $this->checkoutSession->expects($this->once())->method('getWishlistIds');
  113. $this->checkoutSession->expects($this->once())->method('getSingleWishlistId')->willReturn($wishlistId);
  114. $this->customerSession->expects($this->once())
  115. ->method('isLoggedIn')
  116. ->willReturn(true);
  117. $this->customerSession->expects($this->once())
  118. ->method('getCustomerId')
  119. ->willReturn($customerId);
  120. $this->wishlist->expects($this->once())
  121. ->method('loadByCustomerId')
  122. ->with($this->logicalOr($customerId, true))
  123. ->willReturnSelf();
  124. $this->wishlist->expects($this->once())
  125. ->method('getItemCollection')
  126. ->willReturn($wishlists);
  127. $loadedWishlist->expects($this->once())
  128. ->method('getId')
  129. ->willReturn($wishlistId);
  130. $loadedWishlist->expects($this->once())
  131. ->method('delete');
  132. $wishlists->expects($this->once())
  133. ->method('load')
  134. ->willReturn([$loadedWishlist]);
  135. $this->checkoutSession->expects($this->once())
  136. ->method('setWishlistIds')
  137. ->with([])
  138. ->willReturnSelf();
  139. $this->checkoutSession->expects($this->once())
  140. ->method('setSingleWishlistId')
  141. ->with(null)
  142. ->willReturnSelf();
  143. $this->checkoutSession->expects($this->once())
  144. ->method('setWishlistPendingUrls')
  145. ->with([])
  146. ->willReturnSelf();
  147. $this->checkoutSession->expects($this->once())
  148. ->method('setWishlistPendingMessages')
  149. ->with([])
  150. ->willReturnSelf();
  151. $this->messageManager->expects($this->once())
  152. ->method('addError')
  153. ->with($message)
  154. ->willReturnSelf();
  155. $event->expects($this->once())
  156. ->method('getResponse')
  157. ->willReturn($response);
  158. $response->expects($this->once())
  159. ->method('setRedirect')
  160. ->with($url);
  161. $this->checkoutSession->expects($this->once())
  162. ->method('setNoCartRedirect')
  163. ->with(true);
  164. /** @var $eventObserver \Magento\Framework\Event\Observer */
  165. $this->observer->execute($eventObserver);
  166. }
  167. }