Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FromcartTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Checkout\Helper\Cart as CartHelper;
18 use Magento\Framework\Message\Manager as MessageManager;
21 use Magento\Wishlist\Helper\Data as WishlistHelper;
22 
26 class FromcartTest extends \PHPUnit\Framework\TestCase
27 {
31  protected $controller;
32 
36  protected $context;
37 
41  protected $wishlistProvider;
42 
46  protected $wishlistHelper;
47 
51  protected $cart;
52 
56  protected $cartHelper;
57 
61  protected $escaper;
62 
66  protected $request;
67 
71  protected $messageManager;
72 
76  protected $resultFactory;
77 
81  protected $resultRedirect;
82 
86  protected $formKeyValidator;
87 
88  protected function setUp()
89  {
90  $this->prepareContext();
91 
92  $this->wishlistProvider = $this->getMockBuilder(\Magento\Wishlist\Controller\WishlistProviderInterface::class)
93  ->getMockForAbstractClass();
94 
95  $this->wishlistHelper = $this->getMockBuilder(\Magento\Wishlist\Helper\Data::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98 
99  $this->cart = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102 
103  $this->cartHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Cart::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106 
107  $this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110 
111  $this->formKeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $this->controller = new Fromcart(
116  $this->context,
117  $this->wishlistProvider,
118  $this->wishlistHelper,
119  $this->cart,
120  $this->cartHelper,
121  $this->escaper,
122  $this->formKeyValidator
123  );
124  }
125 
127  {
128  $this->formKeyValidator->expects($this->once())
129  ->method('validate')
130  ->with($this->request)
131  ->willReturn(false);
132 
133  $this->resultRedirect->expects($this->once())
134  ->method('setPath')
135  ->with('*/*/')
136  ->willReturnSelf();
137 
138  $this->assertSame($this->resultRedirect, $this->controller->execute());
139  }
140 
145  public function testExecutePageNotFound()
146  {
147  $this->formKeyValidator->expects($this->once())
148  ->method('validate')
149  ->with($this->request)
150  ->willReturn(true);
151 
152  $this->wishlistProvider->expects($this->once())
153  ->method('getWishlist')
154  ->willReturn(null);
155 
156  $this->controller->execute();
157  }
158 
159  public function testExecuteNoCartItem()
160  {
161  $itemId = 1;
162  $cartUrl = 'cart_url';
163 
164  $this->formKeyValidator->expects($this->once())
165  ->method('validate')
166  ->with($this->request)
167  ->willReturn(true);
168 
169  $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
170  ->disableOriginalConstructor()
171  ->getMock();
172 
173  $this->wishlistProvider->expects($this->once())
174  ->method('getWishlist')
175  ->willReturn($wishlistMock);
176 
177  $this->request->expects($this->once())
178  ->method('getParam')
179  ->with('item')
180  ->willReturn($itemId);
181 
182  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
183  ->disableOriginalConstructor()
184  ->getMock();
185 
186  $quoteMock->expects($this->once())
187  ->method('getItemById')
188  ->with($itemId)
189  ->willReturn(null);
190 
191  $this->cart->expects($this->once())
192  ->method('getQuote')
193  ->willReturn($quoteMock);
194 
195  $this->cartHelper->expects($this->once())
196  ->method('getCartUrl')
197  ->willReturn($cartUrl);
198 
199  $this->messageManager->expects($this->once())
200  ->method('addErrorMessage')
201  ->with(__("The cart item doesn't exist."))
202  ->willReturnSelf();
203 
204  $this->resultRedirect->expects($this->once())
205  ->method('setUrl')
206  ->with($cartUrl)
207  ->willReturnSelf();
208 
209  $this->assertSame($this->resultRedirect, $this->controller->execute());
210  }
211 
212  public function testExecute()
213  {
214  $itemId = 1;
215  $cartUrl = 'cart_url';
216  $productId = 1;
217  $productName = 'product_name';
218 
219  $this->formKeyValidator->expects($this->once())
220  ->method('validate')
221  ->with($this->request)
222  ->willReturn(true);
223 
224  $dataObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
225  ->disableOriginalConstructor()
226  ->getMock();
227 
228  $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
229  ->disableOriginalConstructor()
230  ->getMock();
231  $wishlistMock->expects($this->once())
232  ->method('addNewItem')
233  ->with($productId, $dataObjectMock)
234  ->willReturnSelf();
235  $wishlistMock->expects($this->once())
236  ->method('save')
237  ->willReturnSelf();
238 
239  $this->wishlistProvider->expects($this->once())
240  ->method('getWishlist')
241  ->willReturn($wishlistMock);
242 
243  $this->wishlistHelper->expects($this->once())
244  ->method('calculate')
245  ->willReturnSelf();
246 
247  $this->request->expects($this->once())
248  ->method('getParam')
249  ->with('item')
250  ->willReturn($itemId);
251 
252  $quoteMock = $this->createQuoteMock($productId, $productName, $dataObjectMock, $itemId);
253 
254  $this->cart->expects($this->exactly(2))
255  ->method('getQuote')
256  ->willReturn($quoteMock);
257  $this->cart->expects($this->once())
258  ->method('save')
259  ->willReturnSelf();
260 
261  $this->cartHelper->expects($this->once())
262  ->method('getCartUrl')
263  ->willReturn($cartUrl);
264 
265  $this->escaper->expects($this->once())
266  ->method('escapeHtml')
267  ->with($productName)
268  ->willReturn($productName);
269 
270  $this->messageManager->expects($this->once())
271  ->method('addSuccessMessage')
272  ->with(__("%1 has been moved to your wish list.", $productName))
273  ->willReturnSelf();
274 
275  $this->resultRedirect->expects($this->once())
276  ->method('setUrl')
277  ->with($cartUrl)
278  ->willReturnSelf();
279 
280  $this->assertSame($this->resultRedirect, $this->controller->execute());
281  }
282 
283  public function testExecuteWithException()
284  {
285  $cartUrl = 'cart_url';
286  $exceptionMessage = 'exception_message';
287  $exception = new \Exception($exceptionMessage);
288 
289  $this->formKeyValidator->expects($this->once())
290  ->method('validate')
291  ->with($this->request)
292  ->willReturn(true);
293 
294  $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
295  ->disableOriginalConstructor()
296  ->getMock();
297 
298  $this->wishlistProvider->expects($this->once())
299  ->method('getWishlist')
300  ->willReturn($wishlistMock);
301 
302  $this->request->expects($this->once())
303  ->method('getParam')
304  ->with('item')
305  ->willThrowException($exception);
306 
307  $this->messageManager->expects($this->once())
308  ->method('addExceptionMessage')
309  ->with($exception, __('We can\'t move the item to the wish list.'))
310  ->willReturnSelf();
311 
312  $this->cartHelper->expects($this->once())
313  ->method('getCartUrl')
314  ->willReturn($cartUrl);
315 
316  $this->resultRedirect->expects($this->once())
317  ->method('setUrl')
318  ->with($cartUrl)
319  ->willReturnSelf();
320 
321  $this->assertSame($this->resultRedirect, $this->controller->execute());
322  }
323 
324  protected function prepareContext()
325  {
326  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
327  ->disableOriginalConstructor()
328  ->getMock();
329 
330  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
331  ->disableOriginalConstructor()
332  ->getMock();
333 
334  $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
335  ->disableOriginalConstructor()
336  ->getMock();
337 
338  $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
339  ->disableOriginalConstructor()
340  ->getMock();
341  $this->resultFactory->expects($this->any())
342  ->method('create')
344  ->willReturn($this->resultRedirect);
345 
346  $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
347  ->disableOriginalConstructor()
348  ->getMock();
349 
350  $this->context->expects($this->any())
351  ->method('getRequest')
352  ->willReturn($this->request);
353  $this->context->expects($this->any())
354  ->method('getMessageManager')
355  ->willReturn($this->messageManager);
356  $this->context->expects($this->any())
357  ->method('getResultFactory')
358  ->willReturn($this->resultFactory);
359  }
360 
368  protected function createQuoteMock($productId, $productName, $dataObjectMock, $itemId)
369  {
370  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
371  ->disableOriginalConstructor()
372  ->getMock();
373  $productMock->expects($this->once())
374  ->method('getName')
375  ->willReturn($productName);
376 
377  $quoteItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
378  ->disableOriginalConstructor()
379  ->setMethods([
380  'getProductId',
381  'getBuyRequest',
382  'getProduct',
383  ])
384  ->getMock();
385  $quoteItemMock->expects($this->once())
386  ->method('getProductId')
387  ->willReturn($productId);
388  $quoteItemMock->expects($this->once())
389  ->method('getBuyRequest')
390  ->willReturn($dataObjectMock);
391  $quoteItemMock->expects($this->once())
392  ->method('getProduct')
393  ->willReturn($productMock);
394 
395  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
396  ->disableOriginalConstructor()
397  ->getMock();
398  $quoteMock->expects($this->once())
399  ->method('getItemById')
400  ->with($itemId)
401  ->willReturn($quoteItemMock);
402  $quoteMock->expects($this->once())
403  ->method('removeItem')
404  ->with($itemId)
405  ->willReturnSelf();
406 
407  return $quoteMock;
408  }
409 }
__()
Definition: __.php:13
createQuoteMock($productId, $productName, $dataObjectMock, $itemId)