GetPaymentNonceCommandTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Gateway\Command;
  7. use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand;
  8. use Magento\Braintree\Gateway\SubjectReader;
  9. use Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator;
  10. use Magento\Braintree\Model\Adapter\BraintreeAdapter;
  11. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  12. use Magento\Payment\Gateway\Command\Result\ArrayResult;
  13. use Magento\Payment\Gateway\Command\Result\ArrayResultFactory;
  14. use Magento\Payment\Gateway\Validator\ResultInterface;
  15. use Magento\Vault\Model\PaymentToken;
  16. use Magento\Vault\Model\PaymentTokenManagement;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. /**
  19. * Class GetPaymentNonceCommandTest
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class GetPaymentNonceCommandTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var GetPaymentNonceCommand
  27. */
  28. private $command;
  29. /**
  30. * @var BraintreeAdapter|MockObject
  31. */
  32. private $adapterMock;
  33. /**
  34. * @var PaymentTokenManagement|MockObject
  35. */
  36. private $tokenManagementMock;
  37. /**
  38. * @var PaymentToken|MockObject
  39. */
  40. private $paymentTokenMock;
  41. /**
  42. * @var ArrayResultFactory|MockObject
  43. */
  44. private $resultFactoryMock;
  45. /**
  46. * @var SubjectReader|MockObject
  47. */
  48. private $subjectReaderMock;
  49. /**
  50. * @var PaymentNonceResponseValidator|MockObject
  51. */
  52. private $responseValidatorMock;
  53. /**
  54. * @var ResultInterface|MockObject
  55. */
  56. private $validationResultMock;
  57. protected function setUp()
  58. {
  59. $this->paymentTokenMock = $this->getMockBuilder(PaymentToken::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['getGatewayToken'])
  62. ->getMock();
  63. $this->tokenManagementMock = $this->getMockBuilder(PaymentTokenManagement::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods(['getByPublicHash'])
  66. ->getMock();
  67. $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods(['createNonce'])
  70. ->getMock();
  71. /** @var BraintreeAdapterFactory|MockObject $adapterFactoryMock */
  72. $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $adapterFactoryMock->expects(self::any())
  76. ->method('create')
  77. ->willReturn($this->adapterMock);
  78. $this->resultFactoryMock = $this->getMockBuilder(ArrayResultFactory::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods(['create'])
  81. ->getMock();
  82. $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods(['readPublicHash', 'readCustomerId'])
  85. ->getMock();
  86. $this->validationResultMock = $this->getMockBuilder(ResultInterface::class)
  87. ->setMethods(['isValid', 'getFailsDescription', 'getErrorCodes'])
  88. ->getMock();
  89. $this->responseValidatorMock = $this->getMockBuilder(PaymentNonceResponseValidator::class)
  90. ->disableOriginalConstructor()
  91. ->setMethods(['validate', 'isValid', 'getFailsDescription'])
  92. ->getMock();
  93. $this->command = new GetPaymentNonceCommand(
  94. $this->tokenManagementMock,
  95. $adapterFactoryMock,
  96. $this->resultFactoryMock,
  97. $this->subjectReaderMock,
  98. $this->responseValidatorMock
  99. );
  100. }
  101. /**
  102. * @covers \Magento\Braintree\Gateway\Command\GetPaymentNonceCommand::execute
  103. * @expectedException \InvalidArgumentException
  104. * @expectedExceptionMessage The "publicHash" field does not exists
  105. */
  106. public function testExecuteWithExceptionForPublicHash()
  107. {
  108. $exception = new \InvalidArgumentException('The "publicHash" field does not exists');
  109. $this->subjectReaderMock->expects(static::once())
  110. ->method('readPublicHash')
  111. ->willThrowException($exception);
  112. $this->subjectReaderMock->expects(self::never())
  113. ->method('readCustomerId');
  114. $this->command->execute([]);
  115. }
  116. /**
  117. * @covers \Magento\Braintree\Gateway\Command\GetPaymentNonceCommand::execute
  118. * @expectedException \InvalidArgumentException
  119. * @expectedExceptionMessage The "customerId" field does not exists
  120. */
  121. public function testExecuteWithExceptionForCustomerId()
  122. {
  123. $publicHash = '3wv2m24d2er3';
  124. $this->subjectReaderMock->expects(static::once())
  125. ->method('readPublicHash')
  126. ->willReturn($publicHash);
  127. $exception = new \InvalidArgumentException('The "customerId" field does not exists');
  128. $this->subjectReaderMock->expects(static::once())
  129. ->method('readCustomerId')
  130. ->willThrowException($exception);
  131. $this->tokenManagementMock->expects(static::never())
  132. ->method('getByPublicHash');
  133. $this->command->execute(['publicHash' => $publicHash]);
  134. }
  135. /**
  136. * @covers \Magento\Braintree\Gateway\Command\GetPaymentNonceCommand::execute
  137. * @expectedException \Exception
  138. * @expectedExceptionMessage No available payment tokens
  139. */
  140. public function testExecuteWithExceptionForTokenManagement()
  141. {
  142. $publicHash = '3wv2m24d2er3';
  143. $customerId = 1;
  144. $this->subjectReaderMock->expects(static::once())
  145. ->method('readPublicHash')
  146. ->willReturn($publicHash);
  147. $this->subjectReaderMock->expects(static::once())
  148. ->method('readCustomerId')
  149. ->willReturn($customerId);
  150. $exception = new \Exception('No available payment tokens');
  151. $this->tokenManagementMock->expects(static::once())
  152. ->method('getByPublicHash')
  153. ->willThrowException($exception);
  154. $this->paymentTokenMock->expects(self::never())
  155. ->method('getGatewayToken');
  156. $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
  157. }
  158. /**
  159. * @covers \Magento\Braintree\Gateway\Command\GetPaymentNonceCommand::execute
  160. * @expectedException \Exception
  161. * @expectedExceptionMessage Payment method nonce can't be retrieved.
  162. */
  163. public function testExecuteWithFailedValidation()
  164. {
  165. $publicHash = '3wv2m24d2er3';
  166. $customerId = 1;
  167. $token = 'jd2vnq';
  168. $this->subjectReaderMock->expects(static::once())
  169. ->method('readPublicHash')
  170. ->willReturn($publicHash);
  171. $this->subjectReaderMock->expects(static::once())
  172. ->method('readCustomerId')
  173. ->willReturn($customerId);
  174. $this->tokenManagementMock->expects(static::once())
  175. ->method('getByPublicHash')
  176. ->with($publicHash, $customerId)
  177. ->willReturn($this->paymentTokenMock);
  178. $this->paymentTokenMock->expects(static::once())
  179. ->method('getGatewayToken')
  180. ->willReturn($token);
  181. $obj = new \stdClass();
  182. $obj->success = false;
  183. $this->adapterMock->expects(static::once())
  184. ->method('createNonce')
  185. ->with($token)
  186. ->willReturn($obj);
  187. $this->responseValidatorMock->expects(static::once())
  188. ->method('validate')
  189. ->with(['response' => ['object' => $obj]])
  190. ->willReturn($this->validationResultMock);
  191. $this->validationResultMock->expects(static::once())
  192. ->method('isValid')
  193. ->willReturn(false);
  194. $this->validationResultMock->expects(static::once())
  195. ->method('getFailsDescription')
  196. ->willReturn(['Payment method nonce can\'t be retrieved.']);
  197. $this->resultFactoryMock->expects(static::never())
  198. ->method('create');
  199. $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
  200. }
  201. /**
  202. * @covers \Magento\Braintree\Gateway\Command\GetPaymentNonceCommand::execute
  203. */
  204. public function testExecute()
  205. {
  206. $publicHash = '3wv2m24d2er3';
  207. $customerId = 1;
  208. $token = 'jd2vnq';
  209. $nonce = 's1dj23';
  210. $this->subjectReaderMock->expects(static::once())
  211. ->method('readPublicHash')
  212. ->willReturn($publicHash);
  213. $this->subjectReaderMock->expects(static::once())
  214. ->method('readCustomerId')
  215. ->willReturn($customerId);
  216. $this->tokenManagementMock->expects(static::once())
  217. ->method('getByPublicHash')
  218. ->with($publicHash, $customerId)
  219. ->willReturn($this->paymentTokenMock);
  220. $this->paymentTokenMock->expects(static::once())
  221. ->method('getGatewayToken')
  222. ->willReturn($token);
  223. $obj = new \stdClass();
  224. $obj->success = true;
  225. $obj->paymentMethodNonce = new \stdClass();
  226. $obj->paymentMethodNonce->nonce = $nonce;
  227. $this->adapterMock->expects(static::once())
  228. ->method('createNonce')
  229. ->with($token)
  230. ->willReturn($obj);
  231. $this->responseValidatorMock->expects(static::once())
  232. ->method('validate')
  233. ->with(['response' => ['object' => $obj]])
  234. ->willReturn($this->validationResultMock);
  235. $this->validationResultMock->expects(static::once())
  236. ->method('isValid')
  237. ->willReturn(true);
  238. $this->validationResultMock->expects(self::never())
  239. ->method('getFailsDescription');
  240. $expected = $this->getMockBuilder(ArrayResult::class)
  241. ->disableOriginalConstructor()
  242. ->setMethods(['get'])
  243. ->getMock();
  244. $expected->expects(static::once())
  245. ->method('get')
  246. ->willReturn(['paymentMethodNonce' => $nonce]);
  247. $this->resultFactoryMock->expects(static::once())
  248. ->method('create')
  249. ->willReturn($expected);
  250. $actual = $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
  251. self::assertEquals($expected, $actual);
  252. self::assertEquals($nonce, $actual->get()['paymentMethodNonce']);
  253. }
  254. }