GetNonceTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Payment;
  7. use Magento\Braintree\Controller\Payment\GetNonce;
  8. use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Framework\App\Request\Http;
  12. use Magento\Framework\Controller\ResultFactory;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  15. use Magento\Framework\Webapi\Exception;
  16. use Magento\Payment\Gateway\Command\ResultInterface as CommandResultInterface;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Class GetNonceTest
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class GetNonceTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var GetNonce
  28. */
  29. private $action;
  30. /**
  31. * @var GetPaymentNonceCommand|MockObject
  32. */
  33. private $commandMock;
  34. /**
  35. * @var Session|MockObject
  36. */
  37. private $sessionMock;
  38. /**
  39. * @var LoggerInterface|MockObject
  40. */
  41. private $loggerMock;
  42. /**
  43. * @var ResultFactory|MockObject
  44. */
  45. private $resultFactoryMock;
  46. /**
  47. * @var ResultInterface|MockObject
  48. */
  49. private $resultMock;
  50. /**
  51. * @var Http|MockObject
  52. */
  53. private $requestMock;
  54. /**
  55. * @var CommandResultInterface|MockObject
  56. */
  57. private $commandResultMock;
  58. protected function setUp()
  59. {
  60. $this->initResultFactoryMock();
  61. $this->requestMock = $this->getMockBuilder(RequestInterface::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['getParam'])
  64. ->getMock();
  65. $this->commandMock = $this->getMockBuilder(GetPaymentNonceCommand::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods(['execute', '__wakeup'])
  68. ->getMock();
  69. $this->commandResultMock = $this->getMockBuilder(CommandResultInterface::class)
  70. ->setMethods(['get'])
  71. ->getMock();
  72. $this->sessionMock = $this->getMockBuilder(Session::class)
  73. ->disableOriginalConstructor()
  74. ->setMethods(['getCustomerId', 'getStoreId'])
  75. ->getMock();
  76. $this->sessionMock->expects(static::once())
  77. ->method('getStoreId')
  78. ->willReturn(null);
  79. $this->loggerMock = $this->createMock(LoggerInterface::class);
  80. $context = $this->getMockBuilder(Context::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $context->expects(static::any())
  84. ->method('getRequest')
  85. ->willReturn($this->requestMock);
  86. $context->expects(static::any())
  87. ->method('getResultFactory')
  88. ->willReturn($this->resultFactoryMock);
  89. $managerHelper = new ObjectManager($this);
  90. $this->action = $managerHelper->getObject(GetNonce::class, [
  91. 'context' => $context,
  92. 'logger' => $this->loggerMock,
  93. 'session' => $this->sessionMock,
  94. 'command' => $this->commandMock,
  95. ]);
  96. }
  97. /**
  98. * @covers \Magento\Braintree\Controller\Payment\GetNonce::execute
  99. */
  100. public function testExecuteWithException()
  101. {
  102. $this->requestMock->expects(static::once())
  103. ->method('getParam')
  104. ->with('public_hash')
  105. ->willReturn(null);
  106. $this->sessionMock->expects(static::once())
  107. ->method('getCustomerId')
  108. ->willReturn(null);
  109. $exception = new \Exception('The "publicHash" field does not exists');
  110. $this->commandMock->expects(static::once())
  111. ->method('execute')
  112. ->willThrowException($exception);
  113. $this->loggerMock->expects(static::once())
  114. ->method('critical')
  115. ->with($exception);
  116. $this->resultMock->expects(static::once())
  117. ->method('setHttpResponseCode')
  118. ->with(Exception::HTTP_BAD_REQUEST);
  119. $this->resultMock->expects(static::once())
  120. ->method('setData')
  121. ->with(['message' => 'Sorry, but something went wrong']);
  122. $this->action->execute();
  123. }
  124. /**
  125. * @covers \Magento\Braintree\Controller\Payment\GetNonce::execute
  126. */
  127. public function testExecute()
  128. {
  129. $customerId = 1;
  130. $publicHash = '65b7bae0dcb690d93';
  131. $nonce = 'f1hc45';
  132. $this->requestMock->expects(static::once())
  133. ->method('getParam')
  134. ->with('public_hash')
  135. ->willReturn($publicHash);
  136. $this->sessionMock->expects(static::once())
  137. ->method('getCustomerId')
  138. ->willReturn($customerId);
  139. $this->commandResultMock->expects(static::once())
  140. ->method('get')
  141. ->willReturn([
  142. 'paymentMethodNonce' => $nonce
  143. ]);
  144. $this->commandMock->expects(static::once())
  145. ->method('execute')
  146. ->willReturn($this->commandResultMock);
  147. $this->resultMock->expects(static::once())
  148. ->method('setData')
  149. ->with(['paymentMethodNonce' => $nonce]);
  150. $this->loggerMock->expects(static::never())
  151. ->method('critical');
  152. $this->resultMock->expects(static::never())
  153. ->method('setHttpResponseCode');
  154. $this->action->execute();
  155. }
  156. /**
  157. * Create mock for result factory object
  158. */
  159. private function initResultFactoryMock()
  160. {
  161. $this->resultMock = $this->getMockBuilder(ResultInterface::class)
  162. ->setMethods(['setHttpResponseCode', 'renderResult', 'setHeader', 'setData'])
  163. ->getMock();
  164. $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  165. ->disableOriginalConstructor()
  166. ->setMethods(['create'])
  167. ->getMock();
  168. $this->resultFactoryMock->expects(static::once())
  169. ->method('create')
  170. ->willReturn($this->resultMock);
  171. }
  172. }