GatewayCommandTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Gateway\Command;
  7. use Magento\Payment\Gateway\Command\GatewayCommand;
  8. use Magento\Payment\Gateway\ErrorMapper\ErrorMessageMapperInterface;
  9. use Magento\Payment\Gateway\Http\ClientInterface;
  10. use Magento\Payment\Gateway\Http\TransferFactoryInterface;
  11. use Magento\Payment\Gateway\Http\TransferInterface;
  12. use Magento\Payment\Gateway\Request\BuilderInterface;
  13. use Magento\Payment\Gateway\Response\HandlerInterface;
  14. use Magento\Payment\Gateway\Validator\ResultInterface;
  15. use Magento\Payment\Gateway\Validator\ValidatorInterface;
  16. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class GatewayCommandTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var GatewayCommand
  25. */
  26. private $command;
  27. /**
  28. * @var BuilderInterface|MockObject
  29. */
  30. private $requestBuilder;
  31. /**
  32. * @var TransferFactoryInterface|MockObject
  33. */
  34. private $transferFactory;
  35. /**
  36. * @var ClientInterface|MockObject
  37. */
  38. private $client;
  39. /**
  40. * @var HandlerInterface|MockObject
  41. */
  42. private $responseHandler;
  43. /**
  44. * @var ValidatorInterface|MockObject
  45. */
  46. private $validator;
  47. /**
  48. * @var LoggerInterface|MockObject
  49. */
  50. private $logger;
  51. /**
  52. * @var ErrorMessageMapperInterface|MockObject
  53. */
  54. private $errorMessageMapper;
  55. protected function setUp()
  56. {
  57. $this->requestBuilder = $this->createMock(BuilderInterface::class);
  58. $this->transferFactory = $this->createMock(TransferFactoryInterface::class);
  59. $this->client = $this->createMock(ClientInterface::class);
  60. $this->responseHandler = $this->createMock(HandlerInterface::class);
  61. $this->validator = $this->createMock(ValidatorInterface::class);
  62. $this->logger = $this->createMock(LoggerInterface::class);
  63. $this->errorMessageMapper = $this->createMock(ErrorMessageMapperInterface::class);
  64. $this->command = new GatewayCommand(
  65. $this->requestBuilder,
  66. $this->transferFactory,
  67. $this->client,
  68. $this->logger,
  69. $this->responseHandler,
  70. $this->validator,
  71. $this->errorMessageMapper
  72. );
  73. }
  74. public function testExecute()
  75. {
  76. $commandSubject = ['authorize'];
  77. $this->processRequest($commandSubject, true);
  78. $this->responseHandler->method('handle')
  79. ->with($commandSubject, ['response_field1' => 'response_value1']);
  80. $this->command->execute($commandSubject);
  81. }
  82. /**
  83. * Checks a case when request fails.
  84. *
  85. * @expectedException \Magento\Payment\Gateway\Command\CommandException
  86. * @expectedExceptionMessage Transaction has been declined. Please try again later.
  87. */
  88. public function testExecuteValidationFail()
  89. {
  90. $commandSubject = ['authorize'];
  91. $validationFailures = [
  92. __('Failure #1'),
  93. __('Failure #2'),
  94. ];
  95. $this->processRequest($commandSubject, false, $validationFailures);
  96. $this->logger->expects(self::exactly(count($validationFailures)))
  97. ->method('critical')
  98. ->withConsecutive(
  99. [self::equalTo('Payment Error: ' . $validationFailures[0])],
  100. [self::equalTo('Payment Error: ' . $validationFailures[1])]
  101. );
  102. $this->command->execute($commandSubject);
  103. }
  104. /**
  105. * Checks a case when request fails and response errors are mapped.
  106. *
  107. * @expectedException \Magento\Payment\Gateway\Command\CommandException
  108. * @expectedExceptionMessage Failure Mapped
  109. */
  110. public function testExecuteValidationFailWithMappedErrors()
  111. {
  112. $commandSubject = ['authorize'];
  113. $validationFailures = [
  114. __('Failure #1'),
  115. __('Failure #2'),
  116. ];
  117. $errorCodes = ['401'];
  118. $this->processRequest($commandSubject, false, $validationFailures, $errorCodes);
  119. $this->errorMessageMapper->method('getMessage')
  120. ->willReturnMap(
  121. [
  122. ['401', 'Unauthorized'],
  123. ['Failure #1', 'Failure Mapped'],
  124. ['Failure #2', null]
  125. ]
  126. );
  127. $this->logger->expects(self::exactly(count(array_merge($validationFailures, $errorCodes))))
  128. ->method('critical')
  129. ->withConsecutive(
  130. [self::equalTo('Payment Error: Unauthorized')],
  131. [self::equalTo('Payment Error: Failure Mapped')],
  132. [self::equalTo('Payment Error: Failure #2')]
  133. );
  134. $this->command->execute($commandSubject);
  135. }
  136. /**
  137. * Performs command actions like request, response and validation.
  138. *
  139. * @param array $commandSubject
  140. * @param bool $validationResult
  141. * @param array $validationFailures
  142. * @param array $errorCodes
  143. */
  144. private function processRequest(
  145. array $commandSubject,
  146. bool $validationResult,
  147. array $validationFailures = [],
  148. array $errorCodes = []
  149. ) {
  150. $request = [
  151. 'request_field1' => 'request_value1',
  152. 'request_field2' => 'request_value2'
  153. ];
  154. $response = ['response_field1' => 'response_value1'];
  155. $transferO = $this->getMockBuilder(TransferInterface::class)
  156. ->getMockForAbstractClass();
  157. $this->requestBuilder->method('build')
  158. ->with($commandSubject)
  159. ->willReturn($request);
  160. $this->transferFactory->method('create')
  161. ->with($request)
  162. ->willReturn($transferO);
  163. $this->client->method('placeRequest')
  164. ->with($transferO)
  165. ->willReturn($response);
  166. $result = $this->getMockBuilder(ResultInterface::class)
  167. ->getMockForAbstractClass();
  168. $this->validator->method('validate')
  169. ->with(array_merge($commandSubject, ['response' => $response]))
  170. ->willReturn($result);
  171. $result->method('isValid')
  172. ->willReturn($validationResult);
  173. $result->method('getFailsDescription')
  174. ->willReturn($validationFailures);
  175. $result->method('getErrorCodes')
  176. ->willReturn($errorCodes);
  177. }
  178. }