SaveShippingMethodTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Controller\Paypal;
  7. use Magento\Quote\Model\Quote;
  8. use Magento\Framework\View\Layout;
  9. use Magento\Checkout\Model\Session;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Framework\View\Result\Page;
  12. use Magento\Framework\App\Action\Context;
  13. use Magento\Framework\App\RequestInterface;
  14. use Magento\Framework\App\ResponseInterface;
  15. use Magento\Framework\Controller\ResultFactory;
  16. use Magento\Framework\Message\ManagerInterface;
  17. use Magento\Framework\App\Response\RedirectInterface;
  18. use Magento\Braintree\Block\Paypal\Checkout\Review;
  19. use Magento\Braintree\Gateway\Config\PayPal\Config;
  20. use Magento\Braintree\Controller\Paypal\SaveShippingMethod;
  21. use Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater;
  22. /**
  23. * Class SaveShippingMethodTest
  24. *
  25. * @see \Magento\Braintree\Controller\Paypal\SaveShippingMethod
  26. *
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class SaveShippingMethodTest extends \PHPUnit\Framework\TestCase
  30. {
  31. /**
  32. * @var ShippingMethodUpdater|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $shippingMethodUpdaterMock;
  35. /**
  36. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $configMock;
  39. /**
  40. * @var Session|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $checkoutSessionMock;
  43. /**
  44. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $requestMock;
  47. /**
  48. * @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $responseMock;
  51. /**
  52. * @var RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $redirectMock;
  55. /**
  56. * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. private $urlMock;
  59. /**
  60. * @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. private $resultFactoryMock;
  63. /**
  64. * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. private $messageManagerMock;
  67. /**
  68. * @var SaveShippingMethod
  69. */
  70. private $saveShippingMethod;
  71. protected function setUp()
  72. {
  73. /** @var Context|\PHPUnit_Framework_MockObject_MockObject $contextMock */
  74. $contextMock = $this->getMockBuilder(Context::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->requestMock = $this->getMockBuilder(RequestInterface::class)
  78. ->getMockForAbstractClass();
  79. $this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
  80. ->getMockForAbstractClass();
  81. $this->urlMock = $this->getMockBuilder(UrlInterface::class)
  82. ->getMockForAbstractClass();
  83. $this->responseMock = $this->getMockBuilder(ResponseInterface::class)
  84. ->setMethods(['setBody'])
  85. ->getMockForAbstractClass();
  86. $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->checkoutSessionMock = $this->getMockBuilder(Session::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->configMock = $this->getMockBuilder(Config::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $this->shippingMethodUpdaterMock = $this->getMockBuilder(ShippingMethodUpdater::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
  99. ->getMockForAbstractClass();
  100. $contextMock->expects(self::once())
  101. ->method('getRequest')
  102. ->willReturn($this->requestMock);
  103. $contextMock->expects(self::once())
  104. ->method('getRedirect')
  105. ->willReturn($this->redirectMock);
  106. $contextMock->expects(self::once())
  107. ->method('getResponse')
  108. ->willReturn($this->responseMock);
  109. $contextMock->expects(self::once())
  110. ->method('getUrl')
  111. ->willReturn($this->urlMock);
  112. $contextMock->expects(self::once())
  113. ->method('getResultFactory')
  114. ->willReturn($this->resultFactoryMock);
  115. $contextMock->expects(self::once())
  116. ->method('getMessageManager')
  117. ->willReturn($this->messageManagerMock);
  118. $this->saveShippingMethod = new SaveShippingMethod(
  119. $contextMock,
  120. $this->configMock,
  121. $this->checkoutSessionMock,
  122. $this->shippingMethodUpdaterMock
  123. );
  124. }
  125. public function testExecuteAjax()
  126. {
  127. $resultHtml = '<html>test</html>';
  128. $quoteMock = $this->getQuoteMock();
  129. $responsePageMock = $this->getResponsePageMock();
  130. $layoutMock = $this->getLayoutMock();
  131. $blockMock = $this->getBlockMock();
  132. $quoteMock->expects(self::once())
  133. ->method('getItemsCount')
  134. ->willReturn(1);
  135. $this->requestMock->expects(self::exactly(2))
  136. ->method('getParam')
  137. ->willReturnMap(
  138. [
  139. ['isAjax', null, true],
  140. ['shipping_method', null, 'test-shipping-method']
  141. ]
  142. );
  143. $this->checkoutSessionMock->expects(self::once())
  144. ->method('getQuote')
  145. ->willReturn($quoteMock);
  146. $this->shippingMethodUpdaterMock->expects(self::once())
  147. ->method('execute')
  148. ->with('test-shipping-method', $quoteMock);
  149. $this->resultFactoryMock->expects(self::once())
  150. ->method('create')
  151. ->with(ResultFactory::TYPE_PAGE)
  152. ->willReturn($responsePageMock);
  153. $responsePageMock->expects(self::once())
  154. ->method('addHandle')
  155. ->with('paypal_express_review_details')
  156. ->willReturnSelf();
  157. $responsePageMock->expects(self::once())
  158. ->method('getLayout')
  159. ->willReturn($layoutMock);
  160. $layoutMock->expects(self::once())
  161. ->method('getBlock')
  162. ->with('page.block')
  163. ->willReturn($blockMock);
  164. $blockMock->expects(self::once())
  165. ->method('toHtml')
  166. ->willReturn($resultHtml);
  167. $this->responseMock->expects(self::once())
  168. ->method('setBody')
  169. ->with($resultHtml);
  170. $this->urlMock->expects(self::never())
  171. ->method('getUrl');
  172. $this->saveShippingMethod->execute();
  173. }
  174. public function testExecuteAjaxException()
  175. {
  176. $redirectPath = 'path/to/redirect';
  177. $quoteMock = $this->getQuoteMock();
  178. $quoteMock->expects(self::once())
  179. ->method('getItemsCount')
  180. ->willReturn(0);
  181. $this->requestMock->expects(self::exactly(1))
  182. ->method('getParam')
  183. ->willReturnMap(
  184. [
  185. ['isAjax', null, false]
  186. ]
  187. );
  188. $this->checkoutSessionMock->expects(self::once())
  189. ->method('getQuote')
  190. ->willReturn($quoteMock);
  191. $this->shippingMethodUpdaterMock->expects(self::never())
  192. ->method('execute');
  193. $this->messageManagerMock->expects(self::once())
  194. ->method('addExceptionMessage')
  195. ->with(
  196. self::isInstanceOf('\InvalidArgumentException'),
  197. 'Checkout failed to initialize. Verify and try again.'
  198. );
  199. $this->urlMock->expects(self::once())
  200. ->method('getUrl')
  201. ->with('*/*/review', ['_secure' => true])
  202. ->willReturn($redirectPath);
  203. $this->redirectMock->expects(self::once())
  204. ->method('redirect')
  205. ->with($this->responseMock, $redirectPath, []);
  206. $this->saveShippingMethod->execute();
  207. }
  208. public function testExecuteException()
  209. {
  210. $redirectPath = 'path/to/redirect';
  211. $quoteMock = $this->getQuoteMock();
  212. $quoteMock->expects(self::once())
  213. ->method('getItemsCount')
  214. ->willReturn(0);
  215. $this->requestMock->expects(self::exactly(1))
  216. ->method('getParam')
  217. ->willReturnMap(
  218. [
  219. ['isAjax', null, true]
  220. ]
  221. );
  222. $this->checkoutSessionMock->expects(self::once())
  223. ->method('getQuote')
  224. ->willReturn($quoteMock);
  225. $this->shippingMethodUpdaterMock->expects(self::never())
  226. ->method('execute');
  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. $this->urlMock->expects(self::once())
  234. ->method('getUrl')
  235. ->with('*/*/review', ['_secure' => true])
  236. ->willReturn($redirectPath);
  237. $this->responseMock->expects(self::once())
  238. ->method('setBody')
  239. ->with(sprintf('<script>window.location.href = "%s";</script>', $redirectPath));
  240. $this->saveShippingMethod->execute();
  241. }
  242. /**
  243. * @return Review|\PHPUnit_Framework_MockObject_MockObject
  244. */
  245. private function getBlockMock()
  246. {
  247. return $this->getMockBuilder(Review::class)
  248. ->disableOriginalConstructor()
  249. ->getMock();
  250. }
  251. /**
  252. * @return Layout|\PHPUnit_Framework_MockObject_MockObject
  253. */
  254. private function getLayoutMock()
  255. {
  256. return $this->getMockBuilder(Layout::class)
  257. ->disableOriginalConstructor()
  258. ->getMock();
  259. }
  260. /**
  261. * @return Quote|\PHPUnit_Framework_MockObject_MockObject
  262. */
  263. private function getQuoteMock()
  264. {
  265. return $this->getMockBuilder(Quote::class)
  266. ->disableOriginalConstructor()
  267. ->getMock();
  268. }
  269. /**
  270. * @return Page|\PHPUnit_Framework_MockObject_MockObject
  271. */
  272. private function getResponsePageMock()
  273. {
  274. return $this->getMockBuilder(Page::class)
  275. ->disableOriginalConstructor()
  276. ->getMock();
  277. }
  278. }