VaultDetailsHandlerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Response\PayPal;
  7. use Braintree\Result\Successful;
  8. use Braintree\Transaction;
  9. use Braintree\Transaction\PayPalDetails;
  10. use Magento\Braintree\Gateway\SubjectReader;
  11. use Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler;
  12. use Magento\Framework\Intl\DateTimeFactory;
  13. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  14. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  15. use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
  16. use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
  17. use Magento\Sales\Model\Order\Payment;
  18. use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
  19. use Magento\Vault\Api\Data\PaymentTokenInterface;
  20. use Magento\Vault\Model\PaymentToken;
  21. use PHPUnit\Framework\TestCase;
  22. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  23. /**
  24. * Tests \Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler.
  25. *
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class VaultDetailsHandlerTest extends TestCase
  29. {
  30. private static $transactionId = '1n2suy';
  31. private static $token = 'rc39al';
  32. private static $payerEmail = 'john.doe@example.com';
  33. /**
  34. * @var PaymentDataObjectInterface|MockObject
  35. */
  36. private $paymentDataObjectMock;
  37. /**
  38. * @var Payment|MockObject
  39. */
  40. private $paymentInfoMock;
  41. /**
  42. * @var PaymentTokenFactoryInterface|MockObject
  43. */
  44. private $paymentTokenFactoryMock;
  45. /**
  46. * @var PaymentTokenInterface|MockObject
  47. */
  48. protected $paymentTokenMock;
  49. /**
  50. * @var OrderPaymentExtension|MockObject
  51. */
  52. private $paymentExtensionMock;
  53. /**
  54. * @var OrderPaymentExtensionInterfaceFactory|MockObject
  55. */
  56. private $paymentExtensionFactoryMock;
  57. /**
  58. * @var VaultDetailsHandler
  59. */
  60. private $handler;
  61. /**
  62. * @var DateTimeFactory|MockObject
  63. */
  64. private $dateTimeFactoryMock;
  65. /**
  66. * @var array
  67. */
  68. private $subject = [];
  69. protected function setUp()
  70. {
  71. $objectManager = new ObjectManager($this);
  72. $this->paymentDataObjectMock = $this->getMockForAbstractClass(PaymentDataObjectInterface::class);
  73. $this->paymentInfoMock = $this->getMockBuilder(Payment::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['__wakeup', 'getExtensionAttributes'])
  76. ->getMock();
  77. $this->paymentTokenMock = $objectManager->getObject(PaymentToken::class);
  78. $this->paymentTokenFactoryMock = $this->getMockBuilder(PaymentTokenFactoryInterface::class)
  79. ->setMethods(['create'])
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->paymentExtensionMock = $this->getMockBuilder(OrderPaymentExtensionInterface::class)
  83. ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->paymentExtensionFactoryMock = $this->getMockBuilder(OrderPaymentExtensionInterfaceFactory::class)
  87. ->disableOriginalConstructor()
  88. ->setMethods(['create'])
  89. ->getMock();
  90. $this->paymentInfoMock->expects(self::any())
  91. ->method('getExtensionAttributes')
  92. ->willReturn($this->paymentExtensionMock);
  93. $this->subject = [
  94. 'payment' => $this->paymentDataObjectMock,
  95. ];
  96. $this->dateTimeFactoryMock = $this->getMockBuilder(DateTimeFactory::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods(['create'])
  99. ->getMock();
  100. $this->handler = new VaultDetailsHandler(
  101. $this->paymentTokenFactoryMock,
  102. $this->paymentExtensionFactoryMock,
  103. new SubjectReader(),
  104. $this->dateTimeFactoryMock
  105. );
  106. }
  107. public function testHandle()
  108. {
  109. $transaction = $this->getTransaction();
  110. $response = [
  111. 'object' => $transaction
  112. ];
  113. $this->paymentExtensionMock->method('setVaultPaymentToken')
  114. ->with($this->paymentTokenMock);
  115. $this->paymentExtensionMock->method('getVaultPaymentToken')
  116. ->willReturn($this->paymentTokenMock);
  117. $this->paymentDataObjectMock->method('getPayment')
  118. ->willReturn($this->paymentInfoMock);
  119. $this->paymentTokenFactoryMock->method('create')
  120. ->with(PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT)
  121. ->willReturn($this->paymentTokenMock);
  122. $this->paymentExtensionFactoryMock->method('create')
  123. ->willReturn($this->paymentExtensionMock);
  124. $dateTime = new \DateTime('2016-07-05 00:00:00', new \DateTimeZone('UTC'));
  125. $expirationDate = '2017-07-05 00:00:00';
  126. $this->dateTimeFactoryMock->method('create')
  127. ->willReturn($dateTime);
  128. $this->handler->handle($this->subject, $response);
  129. $extensionAttributes = $this->paymentInfoMock->getExtensionAttributes();
  130. $paymentToken = $extensionAttributes->getVaultPaymentToken();
  131. self::assertNotNull($paymentToken);
  132. $tokenDetails = json_decode($paymentToken->getTokenDetails(), true);
  133. self::assertSame($this->paymentTokenMock, $paymentToken);
  134. self::assertEquals(self::$token, $paymentToken->getGatewayToken());
  135. self::assertEquals(self::$payerEmail, $tokenDetails['payerEmail']);
  136. self::assertEquals($expirationDate, $paymentToken->getExpiresAt());
  137. }
  138. public function testHandleWithoutToken()
  139. {
  140. $transaction = $this->getTransaction();
  141. $transaction->transaction->paypalDetails->token = null;
  142. $response = [
  143. 'object' => $transaction
  144. ];
  145. $this->paymentDataObjectMock->method('getPayment')
  146. ->willReturn($this->paymentInfoMock);
  147. $this->paymentTokenFactoryMock->expects(self::never())
  148. ->method('create');
  149. $this->dateTimeFactoryMock->expects(self::never())
  150. ->method('create');
  151. $this->handler->handle($this->subject, $response);
  152. self::assertNotNull($this->paymentInfoMock->getExtensionAttributes());
  153. }
  154. /**
  155. * Creates Braintree transaction.
  156. *
  157. * @return Successful
  158. */
  159. private function getTransaction(): Successful
  160. {
  161. $attributes = [
  162. 'id' => self::$transactionId,
  163. 'paypalDetails' => $this->getPayPalDetails()
  164. ];
  165. $transaction = Transaction::factory($attributes);
  166. $result = new Successful(['transaction' => $transaction]);
  167. return $result;
  168. }
  169. /**
  170. * Gets PayPal transaction details.
  171. *
  172. * @return PayPalDetails
  173. */
  174. private function getPayPalDetails(): PayPalDetails
  175. {
  176. $attributes = [
  177. 'token' => self::$token,
  178. 'payerEmail' => self::$payerEmail
  179. ];
  180. $details = new PayPalDetails($attributes);
  181. return $details;
  182. }
  183. }