Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionServiceTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class TransactionServiceTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $httpClientMock;
18 
22  protected $authorizenetMock;
23 
28 
29  protected function setUp()
30  {
31  $httpClientFactoryMock = $this->getHttpClientFactoryMock();
32 
33  $this->authorizenetMock = $this->getMockBuilder(\Magento\Authorizenet\Model\Directpost::class)
34  ->disableOriginalConstructor()
35  ->getMock();
36 
37  $this->authorizenetMock->method('getConfigData')
38  ->willReturnMap([
39  ['login', 'test login'],
40  ['trans_key', 'test key'],
41  ['cgi_url_td', 'https://apitest.authorize.net/xml/v1/request.api']
42  ]);
43 
44  $objectManagerHelper = new ObjectManager($this);
45  $xmlSecurity = $objectManagerHelper->getObject(\Magento\Framework\Xml\Security::class);
46  $this->transactionService = $objectManagerHelper->getObject(
47  \Magento\Authorizenet\Model\TransactionService::class,
48  [
49  'xmlSecurityHelper' => $xmlSecurity,
50  'httpClientFactory' => $httpClientFactoryMock
51  ]
52  );
53  }
54 
65  public function testLoadVoidedTransactionDetails($transactionId, $resultStatus, $responseStatus, $responseCode)
66  {
67  $document = $this->getResponseBody(
68  $transactionId,
70  $resultStatus,
71  $responseStatus,
72  $responseCode
73  );
74  $this->httpClientMock->expects(static::once())
75  ->method('getBody')
76  ->willReturn($document);
77 
78  $result = $this->transactionService->getTransactionDetails($this->authorizenetMock, $transactionId);
79 
80  static::assertEquals($responseCode, (string)$result->transaction->responseCode);
81  static::assertEquals($responseCode, (string)$result->transaction->responseReasonCode);
82  static::assertEquals($responseStatus, (string)$result->transaction->transactionStatus);
83  }
84 
89  public function dataProviderTransaction()
90  {
91  return [
92  [
93  'transactionId' => '9941997799',
94  'resultStatus' => 'Successful.',
95  'responseStatus' => 'voided',
96  'responseCode' => 1
97  ]
98  ];
99  }
100 
105  private function getHttpClientFactoryMock()
106  {
107  $this->httpClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
108  ->disableOriginalConstructor()
109  ->setMethods(['request', 'getBody', '__wakeup'])
110  ->getMock();
111 
112  $this->httpClientMock->expects(static::once())
113  ->method('request')
114  ->willReturnSelf();
115 
116  $httpClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
117  ->disableOriginalConstructor()
118  ->setMethods(['create'])
119  ->getMock();
120 
121  $httpClientFactoryMock->expects(static::once())
122  ->method('create')
123  ->willReturn($this->httpClientMock);
124  return $httpClientFactoryMock;
125  }
126 
136  private function getResponseBody($transactionId, $resultCode, $resultStatus, $responseStatus, $responseCode)
137  {
138  return sprintf(
139  '<?xml version="1.0" encoding="utf-8"?>
140  <getTransactionDetailsResponse
141  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
142  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
143  xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
144  <messages>
145  <resultCode>%s</resultCode>
146  <message>
147  <code>I00001</code>
148  <text>%s</text>
149  </message>
150  </messages>
151  <transaction>
152  <transId>%s</transId>
153  <transactionType>authOnlyTransaction</transactionType>
154  <transactionStatus>%s</transactionStatus>
155  <responseCode>%s</responseCode>
156  <responseReasonCode>%s</responseReasonCode>
157  </transaction>
158  </getTransactionDetailsResponse>',
159  $resultCode,
160  $resultStatus,
161  $transactionId,
162  $responseStatus,
163  $responseCode,
164  $responseCode
165  );
166  }
167 }
testLoadVoidedTransactionDetails($transactionId, $resultStatus, $responseStatus, $responseCode)