TransactionServiceTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorizenet\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\Simplexml\Element;
  9. use Magento\Authorizenet\Model\TransactionService;
  10. class TransactionServiceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $httpClientMock;
  16. /**
  17. * @var \Magento\Authorizenet\Model\Authorizenet|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $authorizenetMock;
  20. /**
  21. * @var \Magento\Authorizenet\Model\TransactionService
  22. */
  23. protected $transactionService;
  24. protected function setUp()
  25. {
  26. $httpClientFactoryMock = $this->getHttpClientFactoryMock();
  27. $this->authorizenetMock = $this->getMockBuilder(\Magento\Authorizenet\Model\Directpost::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->authorizenetMock->method('getConfigData')
  31. ->willReturnMap([
  32. ['login', 'test login'],
  33. ['trans_key', 'test key'],
  34. ['cgi_url_td', 'https://apitest.authorize.net/xml/v1/request.api']
  35. ]);
  36. $objectManagerHelper = new ObjectManager($this);
  37. $xmlSecurity = $objectManagerHelper->getObject(\Magento\Framework\Xml\Security::class);
  38. $this->transactionService = $objectManagerHelper->getObject(
  39. \Magento\Authorizenet\Model\TransactionService::class,
  40. [
  41. 'xmlSecurityHelper' => $xmlSecurity,
  42. 'httpClientFactory' => $httpClientFactoryMock
  43. ]
  44. );
  45. }
  46. /**
  47. * @covers \Magento\Authorizenet\Model\TransactionService::loadTransactionDetails
  48. * @param $transactionId
  49. * @param $resultStatus
  50. * @param $responseStatus
  51. * @param $responseCode
  52. * @return void
  53. *
  54. * @dataProvider dataProviderTransaction
  55. */
  56. public function testLoadVoidedTransactionDetails($transactionId, $resultStatus, $responseStatus, $responseCode)
  57. {
  58. $document = $this->getResponseBody(
  59. $transactionId,
  60. TransactionService::PAYMENT_UPDATE_STATUS_CODE_SUCCESS,
  61. $resultStatus,
  62. $responseStatus,
  63. $responseCode
  64. );
  65. $this->httpClientMock->expects(static::once())
  66. ->method('getBody')
  67. ->willReturn($document);
  68. $result = $this->transactionService->getTransactionDetails($this->authorizenetMock, $transactionId);
  69. static::assertEquals($responseCode, (string)$result->transaction->responseCode);
  70. static::assertEquals($responseCode, (string)$result->transaction->responseReasonCode);
  71. static::assertEquals($responseStatus, (string)$result->transaction->transactionStatus);
  72. }
  73. /**
  74. * Get data for tests
  75. * @return array
  76. */
  77. public function dataProviderTransaction()
  78. {
  79. return [
  80. [
  81. 'transactionId' => '9941997799',
  82. 'resultStatus' => 'Successful.',
  83. 'responseStatus' => 'voided',
  84. 'responseCode' => 1
  85. ]
  86. ];
  87. }
  88. /**
  89. * Create and return mock for http client factory
  90. * @return \PHPUnit_Framework_MockObject_MockObject
  91. */
  92. private function getHttpClientFactoryMock()
  93. {
  94. $this->httpClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(['request', 'getBody', '__wakeup'])
  97. ->getMock();
  98. $this->httpClientMock->expects(static::once())
  99. ->method('request')
  100. ->willReturnSelf();
  101. $httpClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
  102. ->disableOriginalConstructor()
  103. ->setMethods(['create'])
  104. ->getMock();
  105. $httpClientFactoryMock->expects(static::once())
  106. ->method('create')
  107. ->willReturn($this->httpClientMock);
  108. return $httpClientFactoryMock;
  109. }
  110. /**
  111. * Get body for xml request
  112. * @param string $transactionId
  113. * @param int $resultCode
  114. * @param string $resultStatus
  115. * @param string $responseStatus
  116. * @param string $responseCode
  117. * @return string
  118. */
  119. private function getResponseBody($transactionId, $resultCode, $resultStatus, $responseStatus, $responseCode)
  120. {
  121. return sprintf(
  122. '<?xml version="1.0" encoding="utf-8"?>
  123. <getTransactionDetailsResponse
  124. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  125. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  126. xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  127. <messages>
  128. <resultCode>%s</resultCode>
  129. <message>
  130. <code>I00001</code>
  131. <text>%s</text>
  132. </message>
  133. </messages>
  134. <transaction>
  135. <transId>%s</transId>
  136. <transactionType>authOnlyTransaction</transactionType>
  137. <transactionStatus>%s</transactionStatus>
  138. <responseCode>%s</responseCode>
  139. <responseReasonCode>%s</responseReasonCode>
  140. </transaction>
  141. </getTransactionDetailsResponse>',
  142. $resultCode,
  143. $resultStatus,
  144. $transactionId,
  145. $responseStatus,
  146. $responseCode,
  147. $responseCode
  148. );
  149. }
  150. }