GatewayQueryCommandTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Command;
  8. use Magento\AuthorizenetAcceptjs\Gateway\Command\GatewayQueryCommand;
  9. use Magento\Payment\Gateway\Command\Result\ArrayResult;
  10. use Magento\Payment\Gateway\Http\ClientInterface;
  11. use Magento\Payment\Gateway\Http\TransferFactoryInterface;
  12. use Magento\Payment\Gateway\Http\TransferInterface;
  13. use Magento\Payment\Gateway\Request\BuilderInterface;
  14. use Magento\Payment\Gateway\Validator\Result;
  15. use Magento\Payment\Gateway\Validator\ValidatorInterface;
  16. use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use PHPUnit\Framework\TestCase;
  19. use Psr\Log\LoggerInterface;
  20. class GatewayQueryCommandTest extends TestCase
  21. {
  22. /**
  23. * @var GatewayQueryCommand
  24. */
  25. private $command;
  26. /**
  27. * @var BuilderInterface|MockObject|InvocationMocker
  28. */
  29. private $requestBuilderMock;
  30. /**
  31. * @var TransferFactoryInterface|MockObject|InvocationMocker
  32. */
  33. private $transferFactoryMock;
  34. /**
  35. * @var ClientInterface|MockObject|InvocationMocker
  36. */
  37. private $clientMock;
  38. /**
  39. * @var LoggerInterface|MockObject|InvocationMocker
  40. */
  41. private $loggerMock;
  42. /**
  43. * @var ValidatorInterface|MockObject|InvocationMocker
  44. */
  45. private $validatorMock;
  46. /**
  47. * @var TransferInterface|MockObject|InvocationMocker
  48. */
  49. private $transferMock;
  50. protected function setUp()
  51. {
  52. $this->requestBuilderMock = $this->createMock(BuilderInterface::class);
  53. $this->transferFactoryMock = $this->createMock(TransferFactoryInterface::class);
  54. $this->transferMock = $this->createMock(TransferInterface::class);
  55. $this->clientMock = $this->createMock(ClientInterface::class);
  56. $this->loggerMock = $this->createMock(LoggerInterface::class);
  57. $this->validatorMock = $this->createMock(ValidatorInterface::class);
  58. $this->command = new GatewayQueryCommand(
  59. $this->requestBuilderMock,
  60. $this->transferFactoryMock,
  61. $this->clientMock,
  62. $this->loggerMock,
  63. $this->validatorMock
  64. );
  65. }
  66. public function testNormalExecution()
  67. {
  68. $buildSubject = [
  69. 'foo' => '123'
  70. ];
  71. $request = [
  72. 'bar' => '321'
  73. ];
  74. $response = [
  75. 'transaction' => [
  76. 'transactionType' => 'foo',
  77. 'transactionStatus' => 'bar',
  78. 'responseCode' => 'baz'
  79. ]
  80. ];
  81. $validationSubject = $buildSubject;
  82. $validationSubject['response'] = $response;
  83. $this->requestBuilderMock->method('build')
  84. ->with($buildSubject)
  85. ->willReturn($request);
  86. $this->transferFactoryMock->method('create')
  87. ->with($request)
  88. ->willReturn($this->transferMock);
  89. $this->clientMock->method('placeRequest')
  90. ->with($this->transferMock)
  91. ->willReturn($response);
  92. $this->validatorMock->method('validate')
  93. ->with($validationSubject)
  94. ->willReturn(new Result(true));
  95. $result = $this->command->execute($buildSubject);
  96. $this->assertInstanceOf(ArrayResult::class, $result);
  97. $this->assertEquals($response, $result->get());
  98. }
  99. /**
  100. * @expectedExceptionMessage There was an error while trying to process the request.
  101. * @expectedException \Magento\Payment\Gateway\Command\CommandException
  102. */
  103. public function testExceptionIsThrownAndLoggedWhenRequestFails()
  104. {
  105. $buildSubject = [
  106. 'foo' => '123'
  107. ];
  108. $request = [
  109. 'bar' => '321'
  110. ];
  111. $this->requestBuilderMock->method('build')
  112. ->with($buildSubject)
  113. ->willReturn($request);
  114. $this->transferFactoryMock->method('create')
  115. ->with($request)
  116. ->willReturn($this->transferMock);
  117. $e = new \Exception('foobar');
  118. $this->clientMock->method('placeRequest')
  119. ->with($this->transferMock)
  120. ->willThrowException($e);
  121. // assert the exception is logged
  122. $this->loggerMock->expects($this->once())
  123. ->method('critical')
  124. ->with($e);
  125. $this->command->execute($buildSubject);
  126. }
  127. /**
  128. * @expectedExceptionMessage There was an error while trying to process the request.
  129. * @expectedException \Magento\Payment\Gateway\Command\CommandException
  130. */
  131. public function testExceptionIsThrownWhenResponseIsInvalid()
  132. {
  133. $buildSubject = [
  134. 'foo' => '123'
  135. ];
  136. $request = [
  137. 'bar' => '321'
  138. ];
  139. $response = [
  140. 'baz' => '456'
  141. ];
  142. $validationSubject = $buildSubject;
  143. $validationSubject['response'] = $response;
  144. $this->requestBuilderMock->method('build')
  145. ->with($buildSubject)
  146. ->willReturn($request);
  147. $this->transferFactoryMock->method('create')
  148. ->with($request)
  149. ->willReturn($this->transferMock);
  150. $this->clientMock->method('placeRequest')
  151. ->with($this->transferMock)
  152. ->willReturn($response);
  153. $this->validatorMock->method('validate')
  154. ->with($validationSubject)
  155. ->willReturn(new Result(false));
  156. $this->command->execute($buildSubject);
  157. }
  158. }