Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddToCartTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Wishlist\Observer\AddToCart as Observer;
10 
14 class AddToCartTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $observer;
20 
24  protected $helper;
25 
29  protected $checkoutSession;
30 
34  protected $customerSession;
35 
39  protected $wishlistFactory;
40 
44  protected $wishlist;
45 
49  protected $messageManager;
50 
51  protected function setUp()
52  {
53  $this->checkoutSession = $this->getMockBuilder(
54  \Magento\Checkout\Model\Session::class
55  )->setMethods(
56  [
57  'getSharedWishlist',
58  'getWishlistPendingMessages',
59  'getWishlistPendingUrls',
60  'getWishlistIds',
61  'getSingleWishlistId',
62  'setSingleWishlistId',
63  'setWishlistIds',
64  'setWishlistPendingUrls',
65  'setWishlistPendingMessages',
66  'setNoCartRedirect',
67  ]
68  )->disableOriginalConstructor()->getMock();
69  $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['setWishlistItemCount', 'isLoggedIn', 'getCustomerId'])
72  ->getMock();
73  $this->wishlistFactory = $this->getMockBuilder(\Magento\Wishlist\Model\WishlistFactory::class)
74  ->disableOriginalConstructor()
75  ->setMethods(['create'])
76  ->getMock();
77  $this->wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
81  ->getMock();
82 
83  $this->wishlistFactory->expects($this->any())
84  ->method('create')
85  ->willReturn($this->wishlist);
86 
87  $this->observer = new Observer(
88  $this->checkoutSession,
89  $this->customerSession,
90  $this->wishlistFactory,
91  $this->messageManager
92  );
93  }
94 
95  public function testExecute()
96  {
97  $wishlistId = 1;
98  $customerId = 2;
99  $url = 'http://some.pending/url';
100  $message = 'some error msg';
101 
102  $eventObserver = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $event = $this->getMockBuilder(\Magento\Framework\Event::class)
106  ->setMethods(['getRequest', 'getResponse'])
107  ->disableOriginalConstructor()
108  ->getMock();
109  $request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)->getMock();
110  $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
111  ->setMethods(['setRedirect'])
112  ->getMockForAbstractClass();
113  $wishlists = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Wishlist\Collection::class)
114  ->disableOriginalConstructor()
115  ->getMock();
116  $loadedWishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist\Item::class)
117  ->setMethods(['getId', 'delete'])
118  ->disableOriginalConstructor()
119  ->getMock();
120 
121  $eventObserver->expects($this->any())->method('getEvent')->willReturn($event);
122 
123  $request->expects($this->any())->method('getParam')->with('wishlist_next')->willReturn(true);
124  $event->expects($this->once())->method('getRequest')->willReturn($request);
125 
126  $this->checkoutSession->expects($this->once())->method('getSharedWishlist');
127  $this->checkoutSession->expects($this->once())->method('getWishlistPendingMessages')->willReturn([$message]);
128  $this->checkoutSession->expects($this->once())->method('getWishlistPendingUrls')->willReturn([$url]);
129  $this->checkoutSession->expects($this->once())->method('getWishlistIds');
130  $this->checkoutSession->expects($this->once())->method('getSingleWishlistId')->willReturn($wishlistId);
131 
132  $this->customerSession->expects($this->once())
133  ->method('isLoggedIn')
134  ->willReturn(true);
135  $this->customerSession->expects($this->once())
136  ->method('getCustomerId')
137  ->willReturn($customerId);
138  $this->wishlist->expects($this->once())
139  ->method('loadByCustomerId')
140  ->with($this->logicalOr($customerId, true))
141  ->willReturnSelf();
142  $this->wishlist->expects($this->once())
143  ->method('getItemCollection')
144  ->willReturn($wishlists);
145  $loadedWishlist->expects($this->once())
146  ->method('getId')
147  ->willReturn($wishlistId);
148  $loadedWishlist->expects($this->once())
149  ->method('delete');
150  $wishlists->expects($this->once())
151  ->method('load')
152  ->willReturn([$loadedWishlist]);
153  $this->checkoutSession->expects($this->once())
154  ->method('setWishlistIds')
155  ->with([])
156  ->willReturnSelf();
157  $this->checkoutSession->expects($this->once())
158  ->method('setSingleWishlistId')
159  ->with(null)
160  ->willReturnSelf();
161  $this->checkoutSession->expects($this->once())
162  ->method('setWishlistPendingUrls')
163  ->with([])
164  ->willReturnSelf();
165  $this->checkoutSession->expects($this->once())
166  ->method('setWishlistPendingMessages')
167  ->with([])
168  ->willReturnSelf();
169  $this->messageManager->expects($this->once())
170  ->method('addError')
171  ->with($message)
172  ->willReturnSelf();
173  $event->expects($this->once())
174  ->method('getResponse')
175  ->willReturn($response);
176  $response->expects($this->once())
177  ->method('setRedirect')
178  ->with($url);
179  $this->checkoutSession->expects($this->once())
180  ->method('setNoCartRedirect')
181  ->with(true);
182 
184  $this->observer->execute($eventObserver);
185  }
186 }
$response
Definition: 404.php:11
$message