ThreeDSecureDetailsHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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;
  7. use Braintree\Transaction;
  8. use Magento\Braintree\Gateway\Response\ThreeDSecureDetailsHandler;
  9. use Magento\Payment\Gateway\Data\PaymentDataObject;
  10. use Magento\Sales\Model\Order\Payment;
  11. use Magento\Braintree\Gateway\SubjectReader;
  12. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. /**
  14. * Class ThreeDSecureDetailsHandlerTest
  15. */
  16. class ThreeDSecureDetailsHandlerTest extends \PHPUnit\Framework\TestCase
  17. {
  18. const TRANSACTION_ID = '432er5ww3e';
  19. /**
  20. * @var \Magento\Braintree\Gateway\Response\ThreeDSecureDetailsHandler
  21. */
  22. private $handler;
  23. /**
  24. * @var \Magento\Sales\Model\Order\Payment|MockObject
  25. */
  26. private $paymentMock;
  27. /**
  28. * @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $subjectReaderMock;
  31. protected function setUp()
  32. {
  33. $this->paymentMock = $this->getMockBuilder(Payment::class)
  34. ->disableOriginalConstructor()
  35. ->setMethods([
  36. 'unsAdditionalInformation',
  37. 'hasAdditionalInformation',
  38. 'setAdditionalInformation',
  39. ])
  40. ->getMock();
  41. $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->handler = new ThreeDSecureDetailsHandler($this->subjectReaderMock);
  45. }
  46. /**
  47. * @covers \Magento\Braintree\Gateway\Response\ThreeDSecureDetailsHandler::handle
  48. */
  49. public function testHandle()
  50. {
  51. $paymentData = $this->getPaymentDataObjectMock();
  52. $transaction = $this->getBraintreeTransaction();
  53. $subject = ['payment' => $paymentData];
  54. $response = ['object' => $transaction];
  55. $this->subjectReaderMock->expects(self::once())
  56. ->method('readPayment')
  57. ->with($subject)
  58. ->willReturn($paymentData);
  59. $this->subjectReaderMock->expects(self::once())
  60. ->method('readTransaction')
  61. ->with($response)
  62. ->willReturn($transaction);
  63. $this->paymentMock->expects(static::at(1))
  64. ->method('setAdditionalInformation')
  65. ->with('liabilityShifted', 'Yes');
  66. $this->paymentMock->expects(static::at(2))
  67. ->method('setAdditionalInformation')
  68. ->with('liabilityShiftPossible', 'Yes');
  69. $this->handler->handle($subject, $response);
  70. }
  71. /**
  72. * Create mock for payment data object and order payment
  73. * @return MockObject
  74. */
  75. private function getPaymentDataObjectMock()
  76. {
  77. $mock = $this->getMockBuilder(PaymentDataObject::class)
  78. ->setMethods(['getPayment'])
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $mock->expects(static::once())
  82. ->method('getPayment')
  83. ->willReturn($this->paymentMock);
  84. return $mock;
  85. }
  86. /**
  87. * Create Braintree transaction
  88. * @return MockObject
  89. */
  90. private function getBraintreeTransaction()
  91. {
  92. $attributes = [
  93. 'id' => self::TRANSACTION_ID,
  94. 'threeDSecureInfo' => $this->getThreeDSecureInfo()
  95. ];
  96. $transaction = Transaction::factory($attributes);
  97. return $transaction;
  98. }
  99. /**
  100. * Get 3d secure details
  101. * @return array
  102. */
  103. private function getThreeDSecureInfo()
  104. {
  105. $attributes = [
  106. 'liabilityShifted' => 'Yes',
  107. 'liabilityShiftPossible' => 'Yes'
  108. ];
  109. return $attributes;
  110. }
  111. }