Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveShippingMethodTest.php
Go to the documentation of this file.
1 <?php
8 
24 
32 class SaveShippingMethodTest extends \PHPUnit\Framework\TestCase
33 {
37  private $shippingMethodUpdaterMock;
38 
42  private $configMock;
43 
47  private $checkoutSessionMock;
48 
52  private $requestMock;
53 
57  private $responseMock;
58 
62  protected $redirectMock;
63 
67  private $urlMock;
68 
72  private $resultFactoryMock;
73 
77  private $messageManagerMock;
78 
82  private $saveShippingMethod;
83 
84  protected function setUp()
85  {
87  $contextMock = $this->getMockBuilder(Context::class)
88  ->disableOriginalConstructor()
89  ->getMock();
90  $this->requestMock = $this->getMockBuilder(RequestInterface::class)
91  ->getMockForAbstractClass();
92  $this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
93  ->getMockForAbstractClass();
94  $this->urlMock = $this->getMockBuilder(UrlInterface::class)
95  ->getMockForAbstractClass();
96  $this->responseMock = $this->getMockBuilder(ResponseInterface::class)
97  ->setMethods(['setBody'])
98  ->getMockForAbstractClass();
99  $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102  $this->checkoutSessionMock = $this->getMockBuilder(Session::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->configMock = $this->getMockBuilder(Config::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $this->shippingMethodUpdaterMock = $this->getMockBuilder(ShippingMethodUpdater::class)
109  ->disableOriginalConstructor()
110  ->getMock();
111  $this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
112  ->getMockForAbstractClass();
113 
114  $contextMock->expects(self::once())
115  ->method('getRequest')
116  ->willReturn($this->requestMock);
117  $contextMock->expects(self::once())
118  ->method('getRedirect')
119  ->willReturn($this->redirectMock);
120  $contextMock->expects(self::once())
121  ->method('getResponse')
122  ->willReturn($this->responseMock);
123  $contextMock->expects(self::once())
124  ->method('getUrl')
125  ->willReturn($this->urlMock);
126  $contextMock->expects(self::once())
127  ->method('getResultFactory')
128  ->willReturn($this->resultFactoryMock);
129  $contextMock->expects(self::once())
130  ->method('getMessageManager')
131  ->willReturn($this->messageManagerMock);
132 
133  $this->saveShippingMethod = new SaveShippingMethod(
134  $contextMock,
135  $this->configMock,
136  $this->checkoutSessionMock,
137  $this->shippingMethodUpdaterMock
138  );
139  }
140 
141  public function testExecuteAjax()
142  {
143  $resultHtml = '<html>test</html>';
144  $quoteMock = $this->getQuoteMock();
145  $responsePageMock = $this->getResponsePageMock();
146  $layoutMock = $this->getLayoutMock();
147  $blockMock = $this->getBlockMock();
148 
149  $quoteMock->expects(self::once())
150  ->method('getItemsCount')
151  ->willReturn(1);
152 
153  $this->requestMock->expects(self::exactly(2))
154  ->method('getParam')
155  ->willReturnMap(
156  [
157  ['isAjax', null, true],
158  ['shipping_method', null, 'test-shipping-method']
159  ]
160  );
161 
162  $this->checkoutSessionMock->expects(self::once())
163  ->method('getQuote')
164  ->willReturn($quoteMock);
165 
166  $this->shippingMethodUpdaterMock->expects(self::once())
167  ->method('execute')
168  ->with('test-shipping-method', $quoteMock);
169 
170  $this->resultFactoryMock->expects(self::once())
171  ->method('create')
173  ->willReturn($responsePageMock);
174 
175  $responsePageMock->expects(self::once())
176  ->method('addHandle')
177  ->with('paypal_express_review_details')
178  ->willReturnSelf();
179 
180  $responsePageMock->expects(self::once())
181  ->method('getLayout')
182  ->willReturn($layoutMock);
183 
184  $layoutMock->expects(self::once())
185  ->method('getBlock')
186  ->with('page.block')
187  ->willReturn($blockMock);
188 
189  $blockMock->expects(self::once())
190  ->method('toHtml')
191  ->willReturn($resultHtml);
192 
193  $this->responseMock->expects(self::once())
194  ->method('setBody')
195  ->with($resultHtml);
196 
197  $this->urlMock->expects(self::never())
198  ->method('getUrl');
199 
200  $this->saveShippingMethod->execute();
201  }
202 
203  public function testExecuteAjaxException()
204  {
205  $redirectPath = 'path/to/redirect';
206  $quoteMock = $this->getQuoteMock();
207 
208  $quoteMock->expects(self::once())
209  ->method('getItemsCount')
210  ->willReturn(0);
211 
212  $this->requestMock->expects(self::exactly(1))
213  ->method('getParam')
214  ->willReturnMap(
215  [
216  ['isAjax', null, false]
217  ]
218  );
219 
220  $this->checkoutSessionMock->expects(self::once())
221  ->method('getQuote')
222  ->willReturn($quoteMock);
223 
224  $this->shippingMethodUpdaterMock->expects(self::never())
225  ->method('execute');
226 
227  $this->messageManagerMock->expects(self::once())
228  ->method('addExceptionMessage')
229  ->with(
230  self::isInstanceOf('\InvalidArgumentException'),
231  'Checkout failed to initialize. Verify and try again.'
232  );
233 
234  $this->urlMock->expects(self::once())
235  ->method('getUrl')
236  ->with('*/*/review', ['_secure' => true])
237  ->willReturn($redirectPath);
238 
239  $this->redirectMock->expects(self::once())
240  ->method('redirect')
241  ->with($this->responseMock, $redirectPath, []);
242 
243  $this->saveShippingMethod->execute();
244  }
245 
246  public function testExecuteException()
247  {
248  $redirectPath = 'path/to/redirect';
249  $quoteMock = $this->getQuoteMock();
250 
251  $quoteMock->expects(self::once())
252  ->method('getItemsCount')
253  ->willReturn(0);
254 
255  $this->requestMock->expects(self::exactly(1))
256  ->method('getParam')
257  ->willReturnMap(
258  [
259  ['isAjax', null, true]
260  ]
261  );
262 
263  $this->checkoutSessionMock->expects(self::once())
264  ->method('getQuote')
265  ->willReturn($quoteMock);
266 
267  $this->shippingMethodUpdaterMock->expects(self::never())
268  ->method('execute');
269 
270  $this->messageManagerMock->expects(self::once())
271  ->method('addExceptionMessage')
272  ->with(
273  self::isInstanceOf('\InvalidArgumentException'),
274  'Checkout failed to initialize. Verify and try again.'
275  );
276 
277  $this->urlMock->expects(self::once())
278  ->method('getUrl')
279  ->with('*/*/review', ['_secure' => true])
280  ->willReturn($redirectPath);
281 
282  $this->responseMock->expects(self::once())
283  ->method('setBody')
284  ->with(sprintf('<script>window.location.href = "%s";</script>', $redirectPath));
285 
286  $this->saveShippingMethod->execute();
287  }
288 
292  private function getBlockMock()
293  {
294  return $this->getMockBuilder(Review::class)
295  ->disableOriginalConstructor()
296  ->getMock();
297  }
298 
302  private function getLayoutMock()
303  {
304  return $this->getMockBuilder(Layout::class)
305  ->disableOriginalConstructor()
306  ->getMock();
307  }
308 
312  private function getQuoteMock()
313  {
314  return $this->getMockBuilder(Quote::class)
315  ->disableOriginalConstructor()
316  ->getMock();
317  }
318 
322  private function getResponsePageMock()
323  {
324  return $this->getMockBuilder(Page::class)
325  ->disableOriginalConstructor()
326  ->getMock();
327  }
328 }