3
3
* Copyright © 2015 Magento. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Wishlist \Test \Unit \Controller \Index ;
8
7
8
+ use Magento \Framework \Controller \ResultFactory ;
9
+
9
10
class AddTest extends \PHPUnit_Framework_TestCase
10
11
{
11
12
/**
@@ -33,20 +34,31 @@ class AddTest extends \PHPUnit_Framework_TestCase
33
34
*/
34
35
protected $ controller ;
35
36
37
+ /**
38
+ * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
39
+ */
40
+ protected $ resultFactoryMock ;
41
+
42
+ /**
43
+ * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
44
+ */
45
+ protected $ resultRedirectMock ;
46
+
36
47
public function setUp ()
37
48
{
38
49
$ this ->context = $ this ->getMock (
39
50
'Magento\Framework\App\Action\Context ' ,
40
51
[
41
- 'getRequest ' ,
42
- 'getResponse ' ,
43
- 'getObjectManager ' ,
44
- 'getEventManager ' ,
45
- 'getUrl ' ,
46
- 'getActionFlag ' ,
47
- 'getRedirect ' ,
48
- 'getView ' ,
49
- 'getMessageManager '
52
+ // 'getRequest',
53
+ // 'getResponse',
54
+ // 'getObjectManager',
55
+ // 'getEventManager',
56
+ // 'getUrl',
57
+ // 'getActionFlag',
58
+ // 'getRedirect',
59
+ // 'getView',
60
+ // 'getMessageManager',
61
+ // 'getResultFactory'
50
62
],
51
63
[],
52
64
'' ,
@@ -79,6 +91,17 @@ public function setUp()
79
91
'' ,
80
92
false
81
93
);
94
+ $ this ->resultFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\ResultFactory ' )
95
+ ->disableOriginalConstructor ()
96
+ ->getMock ();
97
+ $ this ->resultRedirectMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Redirect ' )
98
+ ->disableOriginalConstructor ()
99
+ ->getMock ();
100
+
101
+ $ this ->resultFactoryMock ->expects ($ this ->any ())
102
+ ->method ('create ' )
103
+ ->with (ResultFactory::TYPE_REDIRECT , [])
104
+ ->willReturn ($ this ->resultRedirectMock );
82
105
}
83
106
84
107
/**
@@ -128,13 +151,6 @@ public function configureContext()
128
151
'' ,
129
152
false
130
153
);
131
- $ redirect = $ this ->getMock (
132
- '\Magento\Store\App\Response\Redirect ' ,
133
- null ,
134
- [],
135
- '' ,
136
- false
137
- );
138
154
$ view = $ this ->getMock (
139
155
'Magento\Framework\App\View ' ,
140
156
null ,
@@ -174,10 +190,6 @@ public function configureContext()
174
190
->expects ($ this ->any ())
175
191
->method ('getActionFlag ' )
176
192
->will ($ this ->returnValue ($ actionFlag ));
177
- $ this ->context
178
- ->expects ($ this ->any ())
179
- ->method ('getRedirect ' )
180
- ->will ($ this ->returnValue ($ redirect ));
181
193
$ this ->context
182
194
->expects ($ this ->any ())
183
195
->method ('getView ' )
@@ -186,6 +198,9 @@ public function configureContext()
186
198
->expects ($ this ->any ())
187
199
->method ('getMessageManager ' )
188
200
->will ($ this ->returnValue ($ messageManager ));
201
+ $ this ->context ->expects ($ this ->any ())
202
+ ->method ('getResultFactory ' )
203
+ ->willReturn ($ this ->resultFactoryMock );
189
204
}
190
205
191
206
public function configureCustomerSession ()
@@ -261,12 +276,10 @@ public function testExecuteWithoutProductId()
261
276
$ eventManager = $ this ->getMock ('Magento\Framework\Event\Manager ' , null , [], '' , false );
262
277
$ url = $ this ->getMock ('Magento\Framework\Url ' , null , [], '' , false );
263
278
$ actionFlag = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , null , [], '' , false );
264
- $ redirect = $ this ->getMock ('\Magento\Store\App\Response\Redirect ' , ['redirect ' ], [], '' , false );
265
- $ redirect
266
- ->expects ($ this ->once ())
267
- ->method ('redirect ' )
268
- ->with ($ response , '*/ ' , [])
269
- ->will ($ this ->returnValue (null ));
279
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
280
+ ->method ('setPath ' )
281
+ ->with ('*/ ' , [])
282
+ ->willReturnSelf ();
270
283
$ view = $ this ->getMock ('Magento\Framework\App\View ' , null , [], '' , false );
271
284
$ messageManager = $ this ->getMock ('Magento\Framework\Message\Manager ' , null , [], '' , false );
272
285
@@ -294,10 +307,6 @@ public function testExecuteWithoutProductId()
294
307
->expects ($ this ->any ())
295
308
->method ('getActionFlag ' )
296
309
->will ($ this ->returnValue ($ actionFlag ));
297
- $ this ->context
298
- ->expects ($ this ->any ())
299
- ->method ('getRedirect ' )
300
- ->will ($ this ->returnValue ($ redirect ));
301
310
$ this ->context
302
311
->expects ($ this ->any ())
303
312
->method ('getView ' )
@@ -327,10 +336,13 @@ public function testExecuteWithoutProductId()
327
336
->expects ($ this ->never ())
328
337
->method ('setBeforeWishlistUrl ' )
329
338
->will ($ this ->returnValue (null ));
339
+ $ this ->context ->expects ($ this ->any ())
340
+ ->method ('getResultFactory ' )
341
+ ->willReturn ($ this ->resultFactoryMock );
330
342
331
343
$ this ->createController ();
332
344
333
- $ this ->controller ->execute ();
345
+ $ this ->assertSame ( $ this -> resultRedirectMock , $ this -> controller ->execute () );
334
346
}
335
347
336
348
/**
@@ -344,7 +356,6 @@ public function testExecuteWithProductIdAndWithoutProduct()
344
356
->method ('getWishlist ' )
345
357
->will ($ this ->returnValue ($ wishlist ));
346
358
347
-
348
359
$ request = $ this ->getMock ('Magento\Framework\App\Request\Http ' , ['getParams ' ], [], '' , false );
349
360
$ request
350
361
->expects ($ this ->once ())
@@ -356,12 +367,10 @@ public function testExecuteWithProductIdAndWithoutProduct()
356
367
$ eventManager = $ this ->getMock ('Magento\Framework\Event\Manager ' , null , [], '' , false );
357
368
$ url = $ this ->getMock ('Magento\Framework\Url ' , null , [], '' , false );
358
369
$ actionFlag = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , null , [], '' , false );
359
- $ redirect = $ this ->getMock ('\Magento\Store\App\Response\Redirect ' , ['redirect ' ], [], '' , false );
360
- $ redirect
361
- ->expects ($ this ->once ())
362
- ->method ('redirect ' )
363
- ->with ($ response , '*/ ' , [])
364
- ->will ($ this ->returnValue (null ));
370
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
371
+ ->method ('setPath ' )
372
+ ->with ('*/ ' , [])
373
+ ->willReturnSelf ();
365
374
$ view = $ this ->getMock ('Magento\Framework\App\View ' , null , [], '' , false );
366
375
$ messageManager = $ this ->getMock ('Magento\Framework\Message\Manager ' , ['addError ' ], [], '' , false );
367
376
$ messageManager
@@ -394,10 +403,6 @@ public function testExecuteWithProductIdAndWithoutProduct()
394
403
->expects ($ this ->any ())
395
404
->method ('getActionFlag ' )
396
405
->will ($ this ->returnValue ($ actionFlag ));
397
- $ this ->context
398
- ->expects ($ this ->any ())
399
- ->method ('getRedirect ' )
400
- ->will ($ this ->returnValue ($ redirect ));
401
406
$ this ->context
402
407
->expects ($ this ->any ())
403
408
->method ('getView ' )
@@ -406,6 +411,9 @@ public function testExecuteWithProductIdAndWithoutProduct()
406
411
->expects ($ this ->any ())
407
412
->method ('getMessageManager ' )
408
413
->will ($ this ->returnValue ($ messageManager ));
414
+ $ this ->context ->expects ($ this ->any ())
415
+ ->method ('getResultFactory ' )
416
+ ->willReturn ($ this ->resultFactoryMock );
409
417
410
418
$ this ->customerSession
411
419
->expects ($ this ->exactly (1 ))
@@ -430,7 +438,7 @@ public function testExecuteWithProductIdAndWithoutProduct()
430
438
431
439
$ this ->createController ();
432
440
433
- $ this ->controller ->execute ();
441
+ $ this ->assertSame ( $ this -> resultRedirectMock , $ this -> controller ->execute () );
434
442
}
435
443
436
444
/**
@@ -466,12 +474,10 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
466
474
$ eventManager = $ this ->getMock ('Magento\Framework\Event\Manager ' , null , [], '' , false );
467
475
$ url = $ this ->getMock ('Magento\Framework\Url ' , null , [], '' , false );
468
476
$ actionFlag = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , null , [], '' , false );
469
- $ redirect = $ this ->getMock ('\Magento\Store\App\Response\Redirect ' , ['redirect ' ], [], '' , false );
470
- $ redirect
471
- ->expects ($ this ->once ())
472
- ->method ('redirect ' )
473
- ->with ($ response , '* ' , ['wishlist_id ' => 2 ])
474
- ->will ($ this ->returnValue (null ));
477
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
478
+ ->method ('setPath ' )
479
+ ->with ('* ' , ['wishlist_id ' => 2 ])
480
+ ->willReturnSelf ();
475
481
476
482
$ view = $ this ->getMock ('Magento\Framework\App\View ' , null , [], '' , false );
477
483
$ messageManager = $ this ->getMock ('Magento\Framework\Message\Manager ' , ['addError ' ], [], '' , false );
@@ -505,10 +511,6 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
505
511
->expects ($ this ->any ())
506
512
->method ('getActionFlag ' )
507
513
->will ($ this ->returnValue ($ actionFlag ));
508
- $ this ->context
509
- ->expects ($ this ->any ())
510
- ->method ('getRedirect ' )
511
- ->will ($ this ->returnValue ($ redirect ));
512
514
$ this ->context
513
515
->expects ($ this ->any ())
514
516
->method ('getView ' )
@@ -517,6 +519,9 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
517
519
->expects ($ this ->any ())
518
520
->method ('getMessageManager ' )
519
521
->will ($ this ->returnValue ($ messageManager ));
522
+ $ this ->context ->expects ($ this ->any ())
523
+ ->method ('getResultFactory ' )
524
+ ->willReturn ($ this ->resultFactoryMock );
520
525
521
526
$ this ->customerSession
522
527
->expects ($ this ->exactly (1 ))
@@ -559,7 +564,7 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
559
564
560
565
$ this ->createController ();
561
566
562
- $ this ->controller ->execute ();
567
+ $ this ->assertSame ( $ this -> resultRedirectMock , $ this -> controller ->execute () );
563
568
}
564
569
565
570
/**
@@ -683,12 +688,10 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
683
688
684
689
$ url = $ this ->getMock ('Magento\Framework\Url ' , null , [], '' , false );
685
690
$ actionFlag = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , null , [], '' , false );
686
- $ redirect = $ this ->getMock ('\Magento\Store\App\Response\Redirect ' , ['redirect ' ], [], '' , false );
687
- $ redirect
688
- ->expects ($ this ->once ())
689
- ->method ('redirect ' )
690
- ->with ($ response , '* ' , ['wishlist_id ' => 2 ])
691
- ->will ($ this ->returnValue (null ));
691
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
692
+ ->method ('setPath ' )
693
+ ->with ('* ' , ['wishlist_id ' => 2 ])
694
+ ->willReturnSelf ();
692
695
693
696
$ view = $ this ->getMock ('Magento\Framework\App\View ' , null , [], '' , false );
694
697
@@ -733,10 +736,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
733
736
->expects ($ this ->any ())
734
737
->method ('getActionFlag ' )
735
738
->will ($ this ->returnValue ($ actionFlag ));
736
- $ this ->context
737
- ->expects ($ this ->any ())
738
- ->method ('getRedirect ' )
739
- ->will ($ this ->returnValue ($ redirect ));
740
739
$ this ->context
741
740
->expects ($ this ->any ())
742
741
->method ('getView ' )
@@ -745,6 +744,9 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
745
744
->expects ($ this ->any ())
746
745
->method ('getMessageManager ' )
747
746
->will ($ this ->returnValue ($ messageManager ));
747
+ $ this ->context ->expects ($ this ->any ())
748
+ ->method ('getResultFactory ' )
749
+ ->willReturn ($ this ->resultFactoryMock );
748
750
749
751
$ this ->customerSession
750
752
->expects ($ this ->exactly (1 ))
@@ -766,6 +768,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
766
768
767
769
$ this ->createController ();
768
770
769
- $ this ->controller ->execute ();
771
+ $ this ->assertSame ( $ this -> resultRedirectMock , $ this -> controller ->execute () );
770
772
}
771
773
}
0 commit comments