Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AllcartTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class AllcartTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $wishlistProvider;
19 
23  protected $itemCarrier;
24 
28  protected $formKeyValidator;
29 
33  protected $context;
34 
38  protected $request;
39 
43  protected $response;
44 
48  protected $resultFactoryMock;
49 
54 
58  protected $resultForwardMock;
59 
60  protected function setUp()
61  {
62  $this->context = $this->createMock(\Magento\Framework\App\Action\Context::class);
63  $this->wishlistProvider = $this->createMock(\Magento\Wishlist\Controller\WishlistProvider::class);
64  $this->itemCarrier = $this->createMock(\Magento\Wishlist\Model\ItemCarrier::class);
65  $this->formKeyValidator = $this->createMock(\Magento\Framework\Data\Form\FormKey\Validator::class);
66  $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
67  $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
68  $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->resultRedirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->resultForwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77 
78  $this->resultFactoryMock->expects($this->any())
79  ->method('create')
80  ->willReturnMap(
81  [
82  [ResultFactory::TYPE_REDIRECT, [], $this->resultRedirectMock],
83  [ResultFactory::TYPE_FORWARD, [], $this->resultForwardMock]
84  ]
85  );
86  }
87 
88  protected function prepareContext()
89  {
90  $om = $this->createMock(\Magento\Framework\App\ObjectManager::class);
91  $eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
92  $url = $this->createMock(\Magento\Framework\Url::class);
93  $actionFlag = $this->createMock(\Magento\Framework\App\ActionFlag::class);
94  $redirect = $this->createMock(\Magento\Store\App\Response\Redirect::class);
95  $view = $this->createMock(\Magento\Framework\App\View::class);
96  $messageManager = $this->createMock(\Magento\Framework\Message\Manager::class);
97 
98  $this->context
99  ->expects($this->any())
100  ->method('getObjectManager')
101  ->will($this->returnValue($om));
102  $this->context
103  ->expects($this->any())
104  ->method('getRequest')
105  ->will($this->returnValue($this->request));
106  $this->context
107  ->expects($this->any())
108  ->method('getResponse')
109  ->will($this->returnValue($this->response));
110  $this->context
111  ->expects($this->any())
112  ->method('getEventManager')
113  ->will($this->returnValue($eventManager));
114  $this->context
115  ->expects($this->any())
116  ->method('getUrl')
117  ->will($this->returnValue($url));
118  $this->context
119  ->expects($this->any())
120  ->method('getActionFlag')
121  ->will($this->returnValue($actionFlag));
122  $this->context
123  ->expects($this->any())
124  ->method('getRedirect')
125  ->will($this->returnValue($redirect));
126  $this->context
127  ->expects($this->any())
128  ->method('getView')
129  ->will($this->returnValue($view));
130  $this->context
131  ->expects($this->any())
132  ->method('getMessageManager')
133  ->will($this->returnValue($messageManager));
134  $this->context->expects($this->any())
135  ->method('getResultFactory')
136  ->willReturn($this->resultFactoryMock);
137  }
138 
142  public function getController()
143  {
144  $this->prepareContext();
145  return new \Magento\Wishlist\Controller\Index\Allcart(
146  $this->context,
147  $this->wishlistProvider,
148  $this->formKeyValidator,
149  $this->itemCarrier
150  );
151  }
152 
153  public function testExecuteInvalidFormKey()
154  {
155  $this->formKeyValidator
156  ->expects($this->once())
157  ->method('validate')
158  ->with($this->request)
159  ->will($this->returnValue(false));
160  $this->resultForwardMock->expects($this->once())
161  ->method('forward')
162  ->with('noroute')
163  ->willReturnSelf();
164 
165  $controller = $this->getController();
166  $this->assertSame($this->resultForwardMock, $controller->execute());
167  }
168 
169  public function testExecuteWithoutWishlist()
170  {
171  $this->formKeyValidator
172  ->expects($this->once())
173  ->method('validate')
174  ->with($this->request)
175  ->will($this->returnValue(true));
176  $this->wishlistProvider
177  ->expects($this->once())
178  ->method('getWishlist')
179  ->will($this->returnValue(null));
180  $this->resultForwardMock->expects($this->once())
181  ->method('forward')
182  ->with('noroute')
183  ->willReturnSelf();
184 
185  $this->assertSame($this->resultForwardMock, $this->getController()->execute());
186  }
187 
188  public function testExecutePassed()
189  {
190  $url = 'http://redirect-url.com';
191  $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
192 
193  $this->formKeyValidator->expects($this->once())
194  ->method('validate')
195  ->with($this->request)
196  ->will($this->returnValue(true));
197  $this->request->expects($this->once())
198  ->method('getParam')
199  ->with('qty')
200  ->will($this->returnValue(2));
201  $this->wishlistProvider->expects($this->once())
202  ->method('getWishlist')
203  ->will($this->returnValue($wishlist));
204  $this->itemCarrier->expects($this->once())
205  ->method('moveAllToCart')
206  ->with($wishlist, 2)
207  ->willReturn($url);
208  $this->resultRedirectMock->expects($this->once())
209  ->method('setUrl')
210  ->with($url)
211  ->willReturnSelf();
212 
213  $this->assertSame($this->resultRedirectMock, $this->getController()->execute());
214  }
215 }
$wishlist
Definition: wishlist.php:10
$om
$controller
Definition: info.phtml:14