123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\AuthorizenetAcceptjs\Test\Unit\Gateway\Command;
- use Magento\AuthorizenetAcceptjs\Gateway\Command\GatewayQueryCommand;
- use Magento\Payment\Gateway\Command\Result\ArrayResult;
- use Magento\Payment\Gateway\Http\ClientInterface;
- use Magento\Payment\Gateway\Http\TransferFactoryInterface;
- use Magento\Payment\Gateway\Http\TransferInterface;
- use Magento\Payment\Gateway\Request\BuilderInterface;
- use Magento\Payment\Gateway\Validator\Result;
- use Magento\Payment\Gateway\Validator\ValidatorInterface;
- use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- class GatewayQueryCommandTest extends TestCase
- {
- /**
- * @var GatewayQueryCommand
- */
- private $command;
- /**
- * @var BuilderInterface|MockObject|InvocationMocker
- */
- private $requestBuilderMock;
- /**
- * @var TransferFactoryInterface|MockObject|InvocationMocker
- */
- private $transferFactoryMock;
- /**
- * @var ClientInterface|MockObject|InvocationMocker
- */
- private $clientMock;
- /**
- * @var LoggerInterface|MockObject|InvocationMocker
- */
- private $loggerMock;
- /**
- * @var ValidatorInterface|MockObject|InvocationMocker
- */
- private $validatorMock;
- /**
- * @var TransferInterface|MockObject|InvocationMocker
- */
- private $transferMock;
- protected function setUp()
- {
- $this->requestBuilderMock = $this->createMock(BuilderInterface::class);
- $this->transferFactoryMock = $this->createMock(TransferFactoryInterface::class);
- $this->transferMock = $this->createMock(TransferInterface::class);
- $this->clientMock = $this->createMock(ClientInterface::class);
- $this->loggerMock = $this->createMock(LoggerInterface::class);
- $this->validatorMock = $this->createMock(ValidatorInterface::class);
- $this->command = new GatewayQueryCommand(
- $this->requestBuilderMock,
- $this->transferFactoryMock,
- $this->clientMock,
- $this->loggerMock,
- $this->validatorMock
- );
- }
- public function testNormalExecution()
- {
- $buildSubject = [
- 'foo' => '123'
- ];
- $request = [
- 'bar' => '321'
- ];
- $response = [
- 'transaction' => [
- 'transactionType' => 'foo',
- 'transactionStatus' => 'bar',
- 'responseCode' => 'baz'
- ]
- ];
- $validationSubject = $buildSubject;
- $validationSubject['response'] = $response;
- $this->requestBuilderMock->method('build')
- ->with($buildSubject)
- ->willReturn($request);
- $this->transferFactoryMock->method('create')
- ->with($request)
- ->willReturn($this->transferMock);
- $this->clientMock->method('placeRequest')
- ->with($this->transferMock)
- ->willReturn($response);
- $this->validatorMock->method('validate')
- ->with($validationSubject)
- ->willReturn(new Result(true));
- $result = $this->command->execute($buildSubject);
- $this->assertInstanceOf(ArrayResult::class, $result);
- $this->assertEquals($response, $result->get());
- }
- /**
- * @expectedExceptionMessage There was an error while trying to process the request.
- * @expectedException \Magento\Payment\Gateway\Command\CommandException
- */
- public function testExceptionIsThrownAndLoggedWhenRequestFails()
- {
- $buildSubject = [
- 'foo' => '123'
- ];
- $request = [
- 'bar' => '321'
- ];
- $this->requestBuilderMock->method('build')
- ->with($buildSubject)
- ->willReturn($request);
- $this->transferFactoryMock->method('create')
- ->with($request)
- ->willReturn($this->transferMock);
- $e = new \Exception('foobar');
- $this->clientMock->method('placeRequest')
- ->with($this->transferMock)
- ->willThrowException($e);
- // assert the exception is logged
- $this->loggerMock->expects($this->once())
- ->method('critical')
- ->with($e);
- $this->command->execute($buildSubject);
- }
- /**
- * @expectedExceptionMessage There was an error while trying to process the request.
- * @expectedException \Magento\Payment\Gateway\Command\CommandException
- */
- public function testExceptionIsThrownWhenResponseIsInvalid()
- {
- $buildSubject = [
- 'foo' => '123'
- ];
- $request = [
- 'bar' => '321'
- ];
- $response = [
- 'baz' => '456'
- ];
- $validationSubject = $buildSubject;
- $validationSubject['response'] = $response;
- $this->requestBuilderMock->method('build')
- ->with($buildSubject)
- ->willReturn($request);
- $this->transferFactoryMock->method('create')
- ->with($request)
- ->willReturn($this->transferMock);
- $this->clientMock->method('placeRequest')
- ->with($this->transferMock)
- ->willReturn($response);
- $this->validatorMock->method('validate')
- ->with($validationSubject)
- ->willReturn(new Result(false));
- $this->command->execute($buildSubject);
- }
- }
|