Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateItemOptionsTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class UpdateItemOptionsTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $productRepository;
19 
23  protected $wishlistProvider;
24 
28  protected $context;
29 
33  protected $request;
34 
38  protected $om;
39 
43  protected $messageManager;
44 
48  protected $url;
49 
53  protected $customerSession;
54 
58  protected $eventManager;
59 
63  protected $resultFactoryMock;
64 
69 
73  protected $formKeyValidator;
74 
80  protected function setUp()
81  {
82  $this->productRepository = $this->createMock(\Magento\Catalog\Model\ProductRepository::class);
83  $this->context = $this->createMock(\Magento\Framework\App\Action\Context::class);
84  $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
85  $this->wishlistProvider = $this->createMock(\Magento\Wishlist\Controller\WishlistProvider::class);
86  $this->om = $this->createMock(\Magento\Framework\App\ObjectManager::class);
87  $this->messageManager = $this->createMock(\Magento\Framework\Message\Manager::class);
88  $this->url = $this->createMock(\Magento\Framework\Url::class);
89  $this->customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
90  $this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
91  $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94  $this->resultRedirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97 
98  $this->resultFactoryMock->expects($this->any())
99  ->method('create')
100  ->with(ResultFactory::TYPE_REDIRECT, [])
101  ->willReturn($this->resultRedirectMock);
102 
103  $this->formKeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106  }
107 
113  public function tearDown()
114  {
115  unset(
116  $this->productRepository,
117  $this->context,
118  $this->request,
119  $this->wishlistProvider,
120  $this->om,
121  $this->messageManager,
122  $this->url,
123  $this->eventManager
124  );
125  }
126 
132  public function prepareContext()
133  {
134  $actionFlag = $this->createMock(\Magento\Framework\App\ActionFlag::class);
135 
136  $this->context
137  ->expects($this->any())
138  ->method('getObjectManager')
139  ->willReturn($this->om);
140  $this->context
141  ->expects($this->any())
142  ->method('getRequest')
143  ->willReturn($this->request);
144  $this->context
145  ->expects($this->any())
146  ->method('getEventManager')
147  ->willReturn($this->eventManager);
148  $this->context
149  ->expects($this->any())
150  ->method('getUrl')
151  ->willReturn($this->url);
152  $this->context
153  ->expects($this->any())
154  ->method('getActionFlag')
155  ->willReturn($actionFlag);
156  $this->context
157  ->expects($this->any())
158  ->method('getMessageManager')
159  ->willReturn($this->messageManager);
160  $this->context->expects($this->any())
161  ->method('getResultFactory')
162  ->willReturn($this->resultFactoryMock);
163  }
164 
170  protected function getController()
171  {
172  $this->prepareContext();
173 
174  $this->formKeyValidator->expects($this->once())
175  ->method('validate')
176  ->with($this->request)
177  ->willReturn(true);
178 
179  return new \Magento\Wishlist\Controller\Index\UpdateItemOptions(
180  $this->context,
181  $this->customerSession,
182  $this->wishlistProvider,
183  $this->productRepository,
184  $this->formKeyValidator
185  );
186  }
187 
189  {
190  $this->prepareContext();
191 
192  $this->formKeyValidator->expects($this->once())
193  ->method('validate')
194  ->with($this->request)
195  ->willReturn(false);
196 
197  $this->resultRedirectMock->expects($this->once())
198  ->method('setPath')
199  ->with('*/*/')
200  ->willReturnSelf();
201 
202  $controller = new \Magento\Wishlist\Controller\Index\Remove(
203  $this->context,
204  $this->wishlistProvider,
205  $this->formKeyValidator
206  );
207 
208  $this->assertSame($this->resultRedirectMock, $controller->execute());
209  }
210 
216  public function testExecuteWithoutProductId()
217  {
218  $this->request
219  ->expects($this->once())
220  ->method('getParam')
221  ->with('product')
222  ->willReturn(null);
223  $this->resultRedirectMock->expects($this->once())
224  ->method('setPath')
225  ->with('*/', [])
226  ->willReturnSelf();
227 
228  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
229  }
230 
236  public function testExecuteWithoutProduct()
237  {
238  $this->request
239  ->expects($this->once())
240  ->method('getParam')
241  ->with('product')
242  ->willReturn(2);
243 
244  $this->productRepository
245  ->expects($this->once())
246  ->method('getById')
247  ->with(2)
248  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
249 
250  $this->messageManager
251  ->expects($this->once())
252  ->method('addError')
253  ->with('We can\'t specify a product.')
254  ->willReturn(true);
255  $this->resultRedirectMock->expects($this->once())
256  ->method('setPath')
257  ->with('*/', [])
258  ->willReturnSelf();
259 
260  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
261  }
262 
268  public function testExecuteWithoutWishList()
269  {
270  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
271  $item = $this->createMock(\Magento\Wishlist\Model\Item::class);
272 
273  $product
274  ->expects($this->once())
275  ->method('isVisibleInCatalog')
276  ->willReturn(true);
277 
278  $this->request
279  ->expects($this->at(0))
280  ->method('getParam')
281  ->with('product', null)
282  ->willReturn(2);
283  $this->request
284  ->expects($this->at(1))
285  ->method('getParam')
286  ->with('id', null)
287  ->willReturn(3);
288 
289  $this->productRepository
290  ->expects($this->once())
291  ->method('getById')
292  ->with(2)
293  ->willReturn($product);
294 
295  $this->messageManager
296  ->expects($this->never())
297  ->method('addError')
298  ->with('We can\'t specify a product.')
299  ->willReturn(true);
300 
301  $item
302  ->expects($this->once())
303  ->method('load')
304  ->with(3)
305  ->willReturnSelf();
306  $item
307  ->expects($this->once())
308  ->method('__call')
309  ->with('getWishlistId')
310  ->willReturn(12);
311 
312  $this->wishlistProvider
313  ->expects($this->once())
314  ->method('getWishlist')
315  ->with(12)
316  ->willReturn(null);
317 
318  $this->om
319  ->expects($this->once())
320  ->method('create')
321  ->with(\Magento\Wishlist\Model\Item::class)
322  ->willReturn($item);
323  $this->resultRedirectMock->expects($this->once())
324  ->method('setPath')
325  ->with('*/', [])
326  ->willReturnSelf();
327 
328  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
329  }
330 
338  {
339  $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
340  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
341  $item = $this->createMock(\Magento\Wishlist\Model\Item::class);
342  $helper = $this->createMock(\Magento\Wishlist\Helper\Data::class);
343 
344  $helper
345  ->expects($this->exactly(2))
346  ->method('calculate')
347  ->willReturn(true);
348 
349  $wishlist
350  ->expects($this->once())
351  ->method('getItem')
352  ->with(3)
353  ->willReturn($item);
354  $wishlist
355  ->expects($this->once())
356  ->method('updateItem')
357  ->with(3, new \Magento\Framework\DataObject([]))
358  ->willReturnSelf();
359  $wishlist
360  ->expects($this->once())
361  ->method('save')
362  ->willReturn(null);
363  $wishlist
364  ->expects($this->once())
365  ->method('getId')
366  ->willReturn(56);
367 
368  $product
369  ->expects($this->once())
370  ->method('isVisibleInCatalog')
371  ->willReturn(true);
372  $product
373  ->expects($this->once())
374  ->method('getName')
375  ->willReturn('Test name');
376 
377  $this->request
378  ->expects($this->at(0))
379  ->method('getParam')
380  ->with('product', null)
381  ->willReturn(2);
382  $this->request
383  ->expects($this->at(1))
384  ->method('getParam')
385  ->with('id', null)
386  ->willReturn(3);
387 
388  $this->productRepository
389  ->expects($this->once())
390  ->method('getById')
391  ->with(2)
392  ->willReturn($product);
393 
394  $item
395  ->expects($this->once())
396  ->method('load')
397  ->with(3)
398  ->willReturnSelf();
399  $item
400  ->expects($this->once())
401  ->method('__call')
402  ->with('getWishlistId')
403  ->willReturn(12);
404 
405  $this->wishlistProvider
406  ->expects($this->once())
407  ->method('getWishlist')
408  ->with(12)
409  ->willReturn($wishlist);
410 
411  $this->om
412  ->expects($this->once())
413  ->method('create')
414  ->with(\Magento\Wishlist\Model\Item::class)
415  ->willReturn($item);
416 
417  $this->request
418  ->expects($this->once())
419  ->method('getParams')
420  ->willReturn([]);
421 
422  $this->om
423  ->expects($this->exactly(2))
424  ->method('get')
425  ->with(\Magento\Wishlist\Helper\Data::class)
426  ->willReturn($helper);
427 
428  $this->eventManager
429  ->expects($this->once())
430  ->method('dispatch')
431  ->with('wishlist_update_item', ['wishlist' => $wishlist, 'product' => $product, 'item' => $item])
432  ->willReturn(true);
433 
434  $this->messageManager
435  ->expects($this->once())
436  ->method('addSuccess')
437  ->with('Test name has been updated in your Wish List.', null)
438  ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('error-message')));
439  $this->messageManager
440  ->expects($this->once())
441  ->method('addError')
442  ->with('error-message', null)
443  ->willReturn(true);
444  $this->resultRedirectMock->expects($this->once())
445  ->method('setPath')
446  ->with('*/*', ['wishlist_id' => 56])
447  ->willReturnSelf();
448 
449  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
450  }
451 
459  {
460  $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
461  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
462  $item = $this->createMock(\Magento\Wishlist\Model\Item::class);
463  $helper = $this->createMock(\Magento\Wishlist\Helper\Data::class);
464  $logger = $this->createMock(\Magento\Framework\Logger\Monolog::class);
465  $exception = new \Exception();
466 
467  $logger
468  ->expects($this->once())
469  ->method('critical')
470  ->with($exception)
471  ->willReturn(true);
472 
473  $helper
474  ->expects($this->exactly(2))
475  ->method('calculate')
476  ->willReturn(true);
477 
478  $wishlist
479  ->expects($this->once())
480  ->method('getItem')
481  ->with(3)
482  ->willReturn($item);
483  $wishlist
484  ->expects($this->once())
485  ->method('updateItem')
486  ->with(3, new \Magento\Framework\DataObject([]))
487  ->willReturnSelf();
488  $wishlist
489  ->expects($this->once())
490  ->method('save')
491  ->willReturn(null);
492  $wishlist
493  ->expects($this->once())
494  ->method('getId')
495  ->willReturn(56);
496 
497  $product
498  ->expects($this->once())
499  ->method('isVisibleInCatalog')
500  ->willReturn(true);
501  $product
502  ->expects($this->once())
503  ->method('getName')
504  ->willReturn('Test name');
505 
506  $this->request
507  ->expects($this->at(0))
508  ->method('getParam')
509  ->with('product', null)
510  ->willReturn(2);
511  $this->request
512  ->expects($this->at(1))
513  ->method('getParam')
514  ->with('id', null)
515  ->willReturn(3);
516 
517  $this->productRepository
518  ->expects($this->once())
519  ->method('getById')
520  ->with(2)
521  ->willReturn($product);
522 
523  $item
524  ->expects($this->once())
525  ->method('load')
526  ->with(3)
527  ->willReturnSelf();
528  $item
529  ->expects($this->once())
530  ->method('__call')
531  ->with('getWishlistId')
532  ->willReturn(12);
533 
534  $this->wishlistProvider
535  ->expects($this->once())
536  ->method('getWishlist')
537  ->with(12)
538  ->willReturn($wishlist);
539 
540  $this->om
541  ->expects($this->once())
542  ->method('create')
543  ->with(\Magento\Wishlist\Model\Item::class)
544  ->willReturn($item);
545 
546  $this->request
547  ->expects($this->once())
548  ->method('getParams')
549  ->willReturn([]);
550 
551  $this->om
552  ->expects($this->at(1))
553  ->method('get')
554  ->with(\Magento\Wishlist\Helper\Data::class)
555  ->willReturn($helper);
556  $this->om
557  ->expects($this->at(2))
558  ->method('get')
559  ->with(\Magento\Wishlist\Helper\Data::class)
560  ->willReturn($helper);
561  $this->om
562  ->expects($this->at(3))
563  ->method('get')
564  ->with(\Psr\Log\LoggerInterface::class)
565  ->willReturn($logger);
566 
567  $this->eventManager
568  ->expects($this->once())
569  ->method('dispatch')
570  ->with('wishlist_update_item', ['wishlist' => $wishlist, 'product' => $product, 'item' => $item])
571  ->willReturn(true);
572 
573  $this->messageManager
574  ->expects($this->once())
575  ->method('addSuccess')
576  ->with('Test name has been updated in your Wish List.', null)
577  ->willThrowException($exception);
578  $this->messageManager
579  ->expects($this->once())
580  ->method('addError')
581  ->with('We can\'t update your Wish List right now.', null)
582  ->willReturn(true);
583  $this->resultRedirectMock->expects($this->once())
584  ->method('setPath')
585  ->with('*/*', ['wishlist_id' => 56])
586  ->willReturnSelf();
587 
588  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
589  }
590 }
$helper
Definition: iframe.phtml:13
__()
Definition: __.php:13
$logger
$wishlist
Definition: wishlist.php:10
$controller
Definition: info.phtml:14