Skip to content

Commit d734b61

Browse files
author
Yuri Kovsher
committed
MAGETWO-36101: Refactor controllers in the Wishlist module
1 parent 4123e23 commit d734b61

File tree

7 files changed

+393
-416
lines changed

7 files changed

+393
-416
lines changed

app/code/Magento/Wishlist/Test/Unit/Controller/Index/AddTest.php

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Wishlist\Test\Unit\Controller\Index;
87

8+
use Magento\Framework\Controller\ResultFactory;
9+
910
class AddTest extends \PHPUnit_Framework_TestCase
1011
{
1112
/**
@@ -33,20 +34,31 @@ class AddTest extends \PHPUnit_Framework_TestCase
3334
*/
3435
protected $controller;
3536

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+
3647
public function setUp()
3748
{
3849
$this->context = $this->getMock(
3950
'Magento\Framework\App\Action\Context',
4051
[
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'
5062
],
5163
[],
5264
'',
@@ -79,6 +91,17 @@ public function setUp()
7991
'',
8092
false
8193
);
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);
82105
}
83106

84107
/**
@@ -128,13 +151,6 @@ public function configureContext()
128151
'',
129152
false
130153
);
131-
$redirect = $this->getMock(
132-
'\Magento\Store\App\Response\Redirect',
133-
null,
134-
[],
135-
'',
136-
false
137-
);
138154
$view = $this->getMock(
139155
'Magento\Framework\App\View',
140156
null,
@@ -174,10 +190,6 @@ public function configureContext()
174190
->expects($this->any())
175191
->method('getActionFlag')
176192
->will($this->returnValue($actionFlag));
177-
$this->context
178-
->expects($this->any())
179-
->method('getRedirect')
180-
->will($this->returnValue($redirect));
181193
$this->context
182194
->expects($this->any())
183195
->method('getView')
@@ -186,6 +198,9 @@ public function configureContext()
186198
->expects($this->any())
187199
->method('getMessageManager')
188200
->will($this->returnValue($messageManager));
201+
$this->context->expects($this->any())
202+
->method('getResultFactory')
203+
->willReturn($this->resultFactoryMock);
189204
}
190205

191206
public function configureCustomerSession()
@@ -261,12 +276,10 @@ public function testExecuteWithoutProductId()
261276
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
262277
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
263278
$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();
270283
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
271284
$messageManager = $this->getMock('Magento\Framework\Message\Manager', null, [], '', false);
272285

@@ -294,10 +307,6 @@ public function testExecuteWithoutProductId()
294307
->expects($this->any())
295308
->method('getActionFlag')
296309
->will($this->returnValue($actionFlag));
297-
$this->context
298-
->expects($this->any())
299-
->method('getRedirect')
300-
->will($this->returnValue($redirect));
301310
$this->context
302311
->expects($this->any())
303312
->method('getView')
@@ -327,10 +336,13 @@ public function testExecuteWithoutProductId()
327336
->expects($this->never())
328337
->method('setBeforeWishlistUrl')
329338
->will($this->returnValue(null));
339+
$this->context->expects($this->any())
340+
->method('getResultFactory')
341+
->willReturn($this->resultFactoryMock);
330342

331343
$this->createController();
332344

333-
$this->controller->execute();
345+
$this->assertSame($this->resultRedirectMock, $this->controller->execute());
334346
}
335347

336348
/**
@@ -344,7 +356,6 @@ public function testExecuteWithProductIdAndWithoutProduct()
344356
->method('getWishlist')
345357
->will($this->returnValue($wishlist));
346358

347-
348359
$request = $this->getMock('Magento\Framework\App\Request\Http', ['getParams'], [], '', false);
349360
$request
350361
->expects($this->once())
@@ -356,12 +367,10 @@ public function testExecuteWithProductIdAndWithoutProduct()
356367
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
357368
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
358369
$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();
365374
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
366375
$messageManager = $this->getMock('Magento\Framework\Message\Manager', ['addError'], [], '', false);
367376
$messageManager
@@ -394,10 +403,6 @@ public function testExecuteWithProductIdAndWithoutProduct()
394403
->expects($this->any())
395404
->method('getActionFlag')
396405
->will($this->returnValue($actionFlag));
397-
$this->context
398-
->expects($this->any())
399-
->method('getRedirect')
400-
->will($this->returnValue($redirect));
401406
$this->context
402407
->expects($this->any())
403408
->method('getView')
@@ -406,6 +411,9 @@ public function testExecuteWithProductIdAndWithoutProduct()
406411
->expects($this->any())
407412
->method('getMessageManager')
408413
->will($this->returnValue($messageManager));
414+
$this->context->expects($this->any())
415+
->method('getResultFactory')
416+
->willReturn($this->resultFactoryMock);
409417

410418
$this->customerSession
411419
->expects($this->exactly(1))
@@ -430,7 +438,7 @@ public function testExecuteWithProductIdAndWithoutProduct()
430438

431439
$this->createController();
432440

433-
$this->controller->execute();
441+
$this->assertSame($this->resultRedirectMock, $this->controller->execute());
434442
}
435443

436444
/**
@@ -466,12 +474,10 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
466474
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
467475
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
468476
$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();
475481

476482
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
477483
$messageManager = $this->getMock('Magento\Framework\Message\Manager', ['addError'], [], '', false);
@@ -505,10 +511,6 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
505511
->expects($this->any())
506512
->method('getActionFlag')
507513
->will($this->returnValue($actionFlag));
508-
$this->context
509-
->expects($this->any())
510-
->method('getRedirect')
511-
->will($this->returnValue($redirect));
512514
$this->context
513515
->expects($this->any())
514516
->method('getView')
@@ -517,6 +519,9 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
517519
->expects($this->any())
518520
->method('getMessageManager')
519521
->will($this->returnValue($messageManager));
522+
$this->context->expects($this->any())
523+
->method('getResultFactory')
524+
->willReturn($this->resultFactoryMock);
520525

521526
$this->customerSession
522527
->expects($this->exactly(1))
@@ -559,7 +564,7 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
559564

560565
$this->createController();
561566

562-
$this->controller->execute();
567+
$this->assertSame($this->resultRedirectMock, $this->controller->execute());
563568
}
564569

565570
/**
@@ -683,12 +688,10 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
683688

684689
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
685690
$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();
692695

693696
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
694697

@@ -733,10 +736,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
733736
->expects($this->any())
734737
->method('getActionFlag')
735738
->will($this->returnValue($actionFlag));
736-
$this->context
737-
->expects($this->any())
738-
->method('getRedirect')
739-
->will($this->returnValue($redirect));
740739
$this->context
741740
->expects($this->any())
742741
->method('getView')
@@ -745,6 +744,9 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
745744
->expects($this->any())
746745
->method('getMessageManager')
747746
->will($this->returnValue($messageManager));
747+
$this->context->expects($this->any())
748+
->method('getResultFactory')
749+
->willReturn($this->resultFactoryMock);
748750

749751
$this->customerSession
750752
->expects($this->exactly(1))
@@ -766,6 +768,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
766768

767769
$this->createController();
768770

769-
$this->controller->execute();
771+
$this->assertSame($this->resultRedirectMock, $this->controller->execute());
770772
}
771773
}

0 commit comments

Comments
 (0)