ReviewTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\View\Result\Page;
  11. use Magento\Framework\App\Action\Context;
  12. use Magento\Framework\App\RequestInterface;
  13. use Magento\Framework\Message\ManagerInterface;
  14. use Magento\Framework\Controller\ResultFactory;
  15. use Magento\Framework\Controller\Result\Redirect;
  16. use Magento\Framework\View\Element\AbstractBlock;
  17. use Magento\Braintree\Controller\Paypal\Review;
  18. use Magento\Braintree\Gateway\Config\PayPal\Config;
  19. use Magento\Braintree\Model\Paypal\Helper\QuoteUpdater;
  20. use Magento\Braintree\Block\Paypal\Checkout\Review as CheckoutReview;
  21. /**
  22. * Class ReviewTest
  23. *
  24. * @see \Magento\Braintree\Controller\Paypal\Review
  25. *
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class ReviewTest extends \PHPUnit\Framework\TestCase
  29. {
  30. /**
  31. * @var QuoteUpdater|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $quoteUpdaterMock;
  34. /**
  35. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $configMock;
  38. /**
  39. * @var Session|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $checkoutSessionMock;
  42. /**
  43. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $requestMock;
  46. /**
  47. * @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $resultFactoryMock;
  50. /**
  51. * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $messageManagerMock;
  54. /**
  55. * @var Review
  56. */
  57. private $review;
  58. protected function setUp()
  59. {
  60. /** @var Context|\PHPUnit_Framework_MockObject_MockObject $contextMock */
  61. $contextMock = $this->getMockBuilder(Context::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->requestMock = $this->getMockBuilder(RequestInterface::class)
  65. ->setMethods(['getPostValue'])
  66. ->getMockForAbstractClass();
  67. $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->checkoutSessionMock = $this->getMockBuilder(Session::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->configMock = $this->getMockBuilder(Config::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->quoteUpdaterMock = $this->getMockBuilder(QuoteUpdater::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
  80. ->getMockForAbstractClass();
  81. $contextMock->expects(self::once())
  82. ->method('getRequest')
  83. ->willReturn($this->requestMock);
  84. $contextMock->expects(self::once())
  85. ->method('getResultFactory')
  86. ->willReturn($this->resultFactoryMock);
  87. $contextMock->expects(self::once())
  88. ->method('getMessageManager')
  89. ->willReturn($this->messageManagerMock);
  90. $this->review = new Review(
  91. $contextMock,
  92. $this->configMock,
  93. $this->checkoutSessionMock,
  94. $this->quoteUpdaterMock
  95. );
  96. }
  97. public function testExecute()
  98. {
  99. $result = '{"nonce": ["test-value"], "details": ["test-value"]}';
  100. $resultPageMock = $this->getResultPageMock();
  101. $layoutMock = $this->getLayoutMock();
  102. $blockMock = $this->getBlockMock();
  103. $quoteMock = $this->getQuoteMock();
  104. $childBlockMock = $this->getChildBlockMock();
  105. $quoteMock->expects(self::once())
  106. ->method('getItemsCount')
  107. ->willReturn(1);
  108. $this->requestMock->expects(self::once())
  109. ->method('getPostValue')
  110. ->with('result', '{}')
  111. ->willReturn($result);
  112. $this->checkoutSessionMock->expects(self::once())
  113. ->method('getQuote')
  114. ->willReturn($quoteMock);
  115. $this->quoteUpdaterMock->expects(self::once())
  116. ->method('execute')
  117. ->with(['test-value'], ['test-value'], $quoteMock);
  118. $this->resultFactoryMock->expects(self::once())
  119. ->method('create')
  120. ->with(ResultFactory::TYPE_PAGE)
  121. ->willReturn($resultPageMock);
  122. $resultPageMock->expects(self::once())
  123. ->method('getLayout')
  124. ->willReturn($layoutMock);
  125. $layoutMock->expects(self::once())
  126. ->method('getBlock')
  127. ->with('braintree.paypal.review')
  128. ->willReturn($blockMock);
  129. $blockMock->expects(self::once())
  130. ->method('setQuote')
  131. ->with($quoteMock);
  132. $blockMock->expects(self::once())
  133. ->method('getChildBlock')
  134. ->with('shipping_method')
  135. ->willReturn($childBlockMock);
  136. $childBlockMock->expects(self::once())
  137. ->method('setData')
  138. ->with('quote', $quoteMock);
  139. self::assertEquals($this->review->execute(), $resultPageMock);
  140. }
  141. public function testExecuteException()
  142. {
  143. $result = '{}';
  144. $quoteMock = $this->getQuoteMock();
  145. $resultRedirectMock = $this->getResultRedirectMock();
  146. $quoteMock->expects(self::once())
  147. ->method('getItemsCount')
  148. ->willReturn(0);
  149. $this->requestMock->expects(self::once())
  150. ->method('getPostValue')
  151. ->with('result', '{}')
  152. ->willReturn($result);
  153. $this->checkoutSessionMock->expects(self::once())
  154. ->method('getQuote')
  155. ->willReturn($quoteMock);
  156. $this->quoteUpdaterMock->expects(self::never())
  157. ->method('execute');
  158. $this->messageManagerMock->expects(self::once())
  159. ->method('addExceptionMessage')
  160. ->with(
  161. self::isInstanceOf('\InvalidArgumentException'),
  162. 'Checkout failed to initialize. Verify and try again.'
  163. );
  164. $this->resultFactoryMock->expects(self::once())
  165. ->method('create')
  166. ->with(ResultFactory::TYPE_REDIRECT)
  167. ->willReturn($resultRedirectMock);
  168. $resultRedirectMock->expects(self::once())
  169. ->method('setPath')
  170. ->with('checkout/cart')
  171. ->willReturnSelf();
  172. self::assertEquals($this->review->execute(), $resultRedirectMock);
  173. }
  174. public function testExecuteExceptionPaymentWithoutNonce()
  175. {
  176. $result = '{}';
  177. $quoteMock = $this->getQuoteMock();
  178. $resultRedirectMock = $this->getResultRedirectMock();
  179. $quoteMock->expects(self::once())
  180. ->method('getItemsCount')
  181. ->willReturn(1);
  182. $paymentMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Payment::class)
  183. ->disableOriginalConstructor()
  184. ->getMock();
  185. $quoteMock->expects(self::once())
  186. ->method('getPayment')
  187. ->willReturn($paymentMock);
  188. $this->requestMock->expects(self::once())
  189. ->method('getPostValue')
  190. ->with('result', '{}')
  191. ->willReturn($result);
  192. $this->checkoutSessionMock->expects(self::once())
  193. ->method('getQuote')
  194. ->willReturn($quoteMock);
  195. $this->messageManagerMock->expects(self::once())
  196. ->method('addExceptionMessage')
  197. ->with(
  198. self::isInstanceOf(\Magento\Framework\Exception\LocalizedException::class),
  199. 'Checkout failed to initialize. Verify and try again.'
  200. );
  201. $this->resultFactoryMock->expects(self::once())
  202. ->method('create')
  203. ->with(ResultFactory::TYPE_REDIRECT)
  204. ->willReturn($resultRedirectMock);
  205. $resultRedirectMock->expects(self::once())
  206. ->method('setPath')
  207. ->with('checkout/cart')
  208. ->willReturnSelf();
  209. self::assertEquals($this->review->execute(), $resultRedirectMock);
  210. }
  211. /**
  212. * @return Redirect|\PHPUnit_Framework_MockObject_MockObject
  213. */
  214. private function getResultRedirectMock()
  215. {
  216. return $this->getMockBuilder(Redirect::class)
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. }
  220. /**
  221. * @return AbstractBlock|\PHPUnit_Framework_MockObject_MockObject
  222. */
  223. private function getChildBlockMock()
  224. {
  225. return $this->getMockBuilder(AbstractBlock::class)
  226. ->disableOriginalConstructor()
  227. ->getMock();
  228. }
  229. /**
  230. * @return CheckoutReview|\PHPUnit_Framework_MockObject_MockObject
  231. */
  232. private function getBlockMock()
  233. {
  234. return $this->getMockBuilder(CheckoutReview::class)
  235. ->disableOriginalConstructor()
  236. ->getMock();
  237. }
  238. /**
  239. * @return Layout|\PHPUnit_Framework_MockObject_MockObject
  240. */
  241. private function getLayoutMock()
  242. {
  243. return $this->getMockBuilder(Layout::class)
  244. ->disableOriginalConstructor()
  245. ->getMock();
  246. }
  247. /**
  248. * @return Page|\PHPUnit_Framework_MockObject_MockObject
  249. */
  250. private function getResultPageMock()
  251. {
  252. return $this->getMockBuilder(Page::class)
  253. ->disableOriginalConstructor()
  254. ->getMock();
  255. }
  256. /**
  257. * @return Quote|\PHPUnit_Framework_MockObject_MockObject
  258. */
  259. private function getQuoteMock()
  260. {
  261. return $this->getMockBuilder(Quote::class)
  262. ->disableOriginalConstructor()
  263. ->getMock();
  264. }
  265. }