RequestSecureTokenTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Controller\Transparent;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\Controller\Result\JsonFactory;
  9. use Magento\Framework\Session\Generic;
  10. use Magento\Framework\Session\SessionManager;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Paypal\Controller\Transparent\RequestSecureToken;
  13. use Magento\Paypal\Model\Payflow\Service\Request\SecureToken;
  14. use Magento\Paypal\Model\Payflow\Transparent;
  15. /**
  16. * Class RequestSecureTokenTest
  17. *
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class RequestSecureTokenTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var Transparent|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $transparentMock;
  26. /**
  27. * @var RequestSecureToken|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $controller;
  30. /**
  31. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $contextMock;
  34. /**
  35. * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $resultJsonFactoryMock;
  38. /**
  39. * @var Generic|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $sessionTransparentMock;
  42. /**
  43. * @var SecureToken|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $secureTokenServiceMock;
  46. /**
  47. * @var SessionManager|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $sessionManagerMock;
  50. /**
  51. * Set up
  52. *
  53. * @return void
  54. */
  55. protected function setUp()
  56. {
  57. $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
  61. ->setMethods(['create'])
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->sessionTransparentMock = $this->getMockBuilder(\Magento\Framework\Session\Generic::class)
  65. ->setMethods(['setQuoteId'])
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->secureTokenServiceMock = $this->getMockBuilder(
  69. \Magento\Paypal\Model\Payflow\Service\Request\SecureToken::class
  70. )
  71. ->setMethods(['requestToken'])
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->sessionManagerMock = $this->getMockBuilder(\Magento\Framework\Session\SessionManager::class)
  75. ->setMethods(['getQuote'])
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->transparentMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Transparent::class)
  79. ->setMethods(['getCode'])
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->controller = new \Magento\Paypal\Controller\Transparent\RequestSecureToken(
  83. $this->contextMock,
  84. $this->resultJsonFactoryMock,
  85. $this->sessionTransparentMock,
  86. $this->secureTokenServiceMock,
  87. $this->sessionManagerMock,
  88. $this->transparentMock
  89. );
  90. }
  91. public function testExecuteSuccess()
  92. {
  93. $quoteId = 99;
  94. $tokenFields = ['fields-1', 'fields-2', 'fields-3'];
  95. $secureToken = 'token_hash';
  96. $resultExpectation = [
  97. 'transparent' => [
  98. 'fields' => ['fields-1', 'fields-2', 'fields-3']
  99. ],
  100. 'success' => true,
  101. 'error' => false
  102. ];
  103. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $tokenMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->sessionManagerMock->expects($this->atLeastOnce())
  113. ->method('getQuote')
  114. ->willReturn($quoteMock);
  115. $quoteMock->expects($this->once())
  116. ->method('getId')
  117. ->willReturn($quoteId);
  118. $this->sessionTransparentMock->expects($this->once())
  119. ->method('setQuoteId')
  120. ->with($quoteId);
  121. $this->secureTokenServiceMock->expects($this->once())
  122. ->method('requestToken')
  123. ->with($quoteMock)
  124. ->willReturn($tokenMock);
  125. $this->transparentMock->expects($this->once())
  126. ->method('getCode')
  127. ->willReturn('transparent');
  128. $tokenMock->expects($this->atLeastOnce())
  129. ->method('getData')
  130. ->willReturnMap(
  131. [
  132. ['', null, $tokenFields],
  133. ['securetoken', null, $secureToken]
  134. ]
  135. );
  136. $this->resultJsonFactoryMock->expects($this->once())
  137. ->method('create')
  138. ->willReturn($jsonMock);
  139. $jsonMock->expects($this->once())
  140. ->method('setData')
  141. ->with($resultExpectation)
  142. ->willReturnSelf();
  143. $this->assertEquals($jsonMock, $this->controller->execute());
  144. }
  145. public function testExecuteTokenRequestException()
  146. {
  147. $quoteId = 99;
  148. $resultExpectation = [
  149. 'success' => false,
  150. 'error' => true,
  151. 'error_messages' => __('Your payment has been declined. Please try again.')
  152. ];
  153. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. $this->sessionManagerMock->expects($this->atLeastOnce())
  160. ->method('getQuote')
  161. ->willReturn($quoteMock);
  162. $quoteMock->expects($this->once())
  163. ->method('getId')
  164. ->willReturn($quoteId);
  165. $this->sessionTransparentMock->expects($this->once())
  166. ->method('setQuoteId')
  167. ->with($quoteId);
  168. $this->secureTokenServiceMock->expects($this->once())
  169. ->method('requestToken')
  170. ->with($quoteMock)
  171. ->willThrowException(new \Exception());
  172. $this->resultJsonFactoryMock->expects($this->once())
  173. ->method('create')
  174. ->willReturn($jsonMock);
  175. $jsonMock->expects($this->once())
  176. ->method('setData')
  177. ->with($resultExpectation)
  178. ->willReturnSelf();
  179. $this->assertEquals($jsonMock, $this->controller->execute());
  180. }
  181. public function testExecuteEmptyQuoteError()
  182. {
  183. $resultExpectation = [
  184. 'success' => false,
  185. 'error' => true,
  186. 'error_messages' => __('Your payment has been declined. Please try again.')
  187. ];
  188. $quoteMock = null;
  189. $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. $this->sessionManagerMock->expects($this->atLeastOnce())
  193. ->method('getQuote')
  194. ->willReturn($quoteMock);
  195. $this->resultJsonFactoryMock->expects($this->once())
  196. ->method('create')
  197. ->willReturn($jsonMock);
  198. $jsonMock->expects($this->once())
  199. ->method('setData')
  200. ->with($resultExpectation)
  201. ->willReturnSelf();
  202. $this->assertEquals($jsonMock, $this->controller->execute());
  203. }
  204. }