ClientTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AuthorizenetAcceptjs\Test\Unit\Gateway\Http;
  8. use Magento\AuthorizenetAcceptjs\Gateway\Http\Client;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\HTTP\ZendClientFactory;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. use Magento\Payment\Gateway\Http\TransferInterface;
  14. use Magento\Payment\Model\Method\Logger;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use PHPUnit\Framework\TestCase;
  17. use Psr\Log\LoggerInterface;
  18. use Zend_Http_Client;
  19. use Zend_Http_Response;
  20. class ClientTest extends TestCase
  21. {
  22. /**
  23. * @var ObjectManager
  24. */
  25. private $objectManager;
  26. /**
  27. * @var Logger
  28. */
  29. private $paymentLogger;
  30. /**
  31. * @var ZendClientFactory
  32. */
  33. private $httpClientFactory;
  34. /**
  35. * @var Zend_Http_Client
  36. */
  37. private $httpClient;
  38. /**
  39. * @var Zend_Http_Response
  40. */
  41. private $httpResponse;
  42. /**
  43. * @var LoggerInterface
  44. */
  45. private $logger;
  46. protected function setUp()
  47. {
  48. $this->objectManager = new ObjectManager($this);
  49. $this->paymentLogger = $this->createMock(Logger::class);
  50. $this->httpClientFactory = $this->createMock(ZendClientFactory::class);
  51. $this->httpClient = $this->createMock(Zend_Http_Client::class);
  52. $this->httpResponse = $this->createMock(Zend_Http_Response::class);
  53. $this->httpClientFactory->method('create')->will($this->returnValue($this->httpClient));
  54. $this->httpClient->method('request')
  55. ->willReturn($this->httpResponse);
  56. /** @var MockObject $logger */
  57. $this->logger = $this->createMock(LoggerInterface::class);
  58. }
  59. public function testCanSendRequest()
  60. {
  61. // Assert the raw data was set on the client
  62. $this->httpClient->expects($this->once())
  63. ->method('setRawData')
  64. ->with(
  65. '{"doSomeThing":{"foobar":"baz"}}',
  66. 'application/json'
  67. );
  68. $request = [
  69. 'payload_type' => 'doSomeThing',
  70. 'foobar' => 'baz'
  71. ];
  72. // Authorize.net returns a BOM and refuses to fix it
  73. $response = pack('CCC', 0xef, 0xbb, 0xbf) . '{"foo":{"bar":"baz"}}';
  74. $this->httpResponse->method('getBody')
  75. ->willReturn($response);
  76. // Assert the logger was given the data
  77. $this->paymentLogger->expects($this->once())
  78. ->method('debug')
  79. ->with(['request' => $request, 'response' => '{"foo":{"bar":"baz"}}']);
  80. /**
  81. * @var $apiClient Client
  82. */
  83. $apiClient = $this->objectManager->getObject(Client::class, [
  84. 'httpClientFactory' => $this->httpClientFactory,
  85. 'paymentLogger' => $this->paymentLogger,
  86. 'json' => new Json()
  87. ]);
  88. $result = $apiClient->placeRequest($this->getTransferObjectMock($request));
  89. $this->assertSame('baz', $result['foo']['bar']);
  90. }
  91. /**
  92. * @expectedException \Magento\Framework\Exception\LocalizedException
  93. * @expectedExceptionMessage Something went wrong in the payment gateway.
  94. */
  95. public function testExceptionIsThrownWhenEmptyResponseIsReceived()
  96. {
  97. // Assert the client has the raw data set
  98. $this->httpClient->expects($this->once())
  99. ->method('setRawData')
  100. ->with(
  101. '{"doSomeThing":{"foobar":"baz"}}',
  102. 'application/json'
  103. );
  104. $this->httpResponse->method('getBody')
  105. ->willReturn('');
  106. // Assert the exception is given to the logger
  107. $this->logger->expects($this->once())
  108. ->method('critical')
  109. ->with($this->callback(function ($e) {
  110. return $e instanceof \Exception
  111. && $e->getMessage() === 'Invalid JSON was returned by the gateway';
  112. }));
  113. $request = [
  114. 'payload_type' => 'doSomeThing',
  115. 'foobar' => 'baz'
  116. ];
  117. // Assert the logger was given the data
  118. $this->paymentLogger->expects($this->once())
  119. ->method('debug')
  120. ->with(['request' => $request, 'response' => '']);
  121. /**
  122. * @var $apiClient Client
  123. */
  124. $apiClient = $this->objectManager->getObject(Client::class, [
  125. 'httpClientFactory' => $this->httpClientFactory,
  126. 'paymentLogger' => $this->paymentLogger,
  127. 'logger' => $this->logger,
  128. 'json' => new Json()
  129. ]);
  130. $apiClient->placeRequest($this->getTransferObjectMock($request));
  131. }
  132. /**
  133. * @expectedException \Magento\Framework\Exception\LocalizedException
  134. * @expectedExceptionMessage Something went wrong in the payment gateway.
  135. */
  136. public function testExceptionIsThrownWhenInvalidResponseIsReceived()
  137. {
  138. // Assert the client was given the raw data
  139. $this->httpClient->expects($this->once())
  140. ->method('setRawData')
  141. ->with(
  142. '{"doSomeThing":{"foobar":"baz"}}',
  143. 'application/json'
  144. );
  145. $this->httpResponse->method('getBody')
  146. ->willReturn('bad');
  147. $request = [
  148. 'payload_type' => 'doSomeThing',
  149. 'foobar' => 'baz'
  150. ];
  151. // Assert the logger was given the data
  152. $this->paymentLogger->expects($this->once())
  153. ->method('debug')
  154. ->with(['request' => $request, 'response' => 'bad']);
  155. // Assert the exception was given to the logger
  156. $this->logger->expects($this->once())
  157. ->method('critical')
  158. ->with($this->callback(function ($e) {
  159. return $e instanceof \Exception
  160. && $e->getMessage() === 'Invalid JSON was returned by the gateway';
  161. }));
  162. /**
  163. * @var $apiClient Client
  164. */
  165. $apiClient = $this->objectManager->getObject(Client::class, [
  166. 'httpClientFactory' => $this->httpClientFactory,
  167. 'paymentLogger' => $this->paymentLogger,
  168. 'logger' => $this->logger,
  169. 'json' => new Json()
  170. ]);
  171. $apiClient->placeRequest($this->getTransferObjectMock($request));
  172. }
  173. /**
  174. * Creates mock object for TransferInterface.
  175. *
  176. * @return TransferInterface|MockObject
  177. */
  178. private function getTransferObjectMock(array $data)
  179. {
  180. $transferObjectMock = $this->createMock(TransferInterface::class);
  181. $transferObjectMock->method('getBody')
  182. ->willReturn($data);
  183. return $transferObjectMock;
  184. }
  185. }