123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?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\CaptureStrategyCommand;
- use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\Api\Search\SearchCriteria;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Payment\Gateway\Command\CommandPoolInterface;
- use Magento\Payment\Gateway\Command\GatewayCommand;
- use Magento\Payment\Gateway\Data\PaymentDataObject;
- use Magento\Sales\Api\Data\TransactionSearchResultInterface;
- use Magento\Sales\Api\TransactionRepositoryInterface;
- use Magento\Sales\Model\Order\Payment;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- class CaptureStrategyCommandTest extends TestCase
- {
- /**
- * @var CaptureStrategyCommand
- */
- private $strategyCommand;
- /**
- * @var CommandPoolInterface|MockObject
- */
- private $commandPoolMock;
- /**
- * @var TransactionRepositoryInterface|MockObject
- */
- private $transactionRepositoryMock;
- /**
- * @var FilterBuilder|MockObject
- */
- private $filterBuilderMock;
- /**
- * @var SearchCriteriaBuilder|MockObject
- */
- private $searchCriteriaBuilderMock;
- /**
- * @var Payment|MockObject
- */
- private $paymentMock;
- /**
- * @var PaymentDataObject|MockObject
- */
- private $paymentDOMock;
- /**
- * @var GatewayCommand|MockObject
- */
- private $commandMock;
- /**
- * @var TransactionSearchResultInterface|MockObject
- */
- private $transactionsResult;
- protected function setUp()
- {
- // Simple mocks
- $this->paymentDOMock = $this->createMock(PaymentDataObject::class);
- $this->paymentMock = $this->createMock(Payment::class);
- $this->paymentDOMock->method('getPayment')
- ->willReturn($this->paymentMock);
- $this->commandMock = $this->createMock(GatewayCommand::class);
- $this->commandPoolMock = $this->createMock(CommandPoolInterface::class);
- $this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
- $this->transactionRepositoryMock = $this->createMock(TransactionRepositoryInterface::class);
- // The search criteria builder should return the criteria with the specified filters
- $this->filterBuilderMock = $this->createMock(FilterBuilder::class);
- // We aren't coupling the implementation to the test. The test only cares how the result is processed
- $this->filterBuilderMock->method('setField')
- ->willReturnSelf();
- $this->filterBuilderMock->method('setValue')
- ->willReturnSelf();
- $searchCriteria = new SearchCriteria();
- $this->searchCriteriaBuilderMock->method('addFilters')
- ->willReturnSelf();
- $this->searchCriteriaBuilderMock->method('create')
- ->willReturn($searchCriteria);
- // The transaction result can be customized per test to simulate different scenarios
- $this->transactionsResult = $this->createMock(TransactionSearchResultInterface::class);
- $this->transactionRepositoryMock->method('getList')
- ->with($searchCriteria)
- ->willReturn($this->transactionsResult);
- $this->strategyCommand = new CaptureStrategyCommand(
- $this->commandPoolMock,
- $this->transactionRepositoryMock,
- $this->filterBuilderMock,
- $this->searchCriteriaBuilderMock,
- new SubjectReader()
- );
- }
- public function testExecuteWillAuthorizeWhenNotAuthorizedAndNotCaptured()
- {
- $subject = ['payment' => $this->paymentDOMock];
- // Hasn't been authorized
- $this->paymentMock->method('getAuthorizationTransaction')
- ->willReturn(false);
- // Hasn't been captured
- $this->transactionsResult->method('getTotalCount')
- ->willReturn(0);
- // Assert authorize command was used
- $this->commandPoolMock->expects($this->once())
- ->method('get')
- ->with('sale')
- ->willReturn($this->commandMock);
- // Assert execute was called and with correct data
- $this->commandMock->expects($this->once())
- ->method('execute')
- ->with($subject);
- $this->strategyCommand->execute($subject);
- // Assertions are performed via mock expects above
- }
- public function testExecuteWillAuthorizeAndCaptureWhenAlreadyCaptured()
- {
- $subject = ['payment' => $this->paymentDOMock];
- // Already authorized
- $this->paymentMock->method('getAuthorizationTransaction')
- ->willReturn(true);
- // And already captured
- $this->transactionsResult->method('getTotalCount')
- ->willReturn(1);
- // Assert authorize command was used
- $this->commandPoolMock->expects($this->once())
- ->method('get')
- ->with('settle')
- ->willReturn($this->commandMock);
- // Assert execute was called and with correct data
- $this->commandMock->expects($this->once())
- ->method('execute')
- ->with($subject);
- $this->strategyCommand->execute($subject);
- // Assertions are performed via mock expects above
- }
- public function testExecuteWillCaptureWhenAlreadyAuthorizedButNotCaptured()
- {
- $subject = ['payment' => $this->paymentDOMock];
- // Was already authorized
- $this->paymentMock->method('getAuthorizationTransaction')
- ->willReturn(true);
- // But, hasn't been captured
- $this->transactionsResult->method('getTotalCount')
- ->willReturn(0);
- // Assert authorize command was used
- $this->commandPoolMock->expects($this->once())
- ->method('get')
- ->with('settle')
- ->willReturn($this->commandMock);
- // Assert execute was called and with correct data
- $this->commandMock->expects($this->once())
- ->method('execute')
- ->with($subject);
- $this->strategyCommand->execute($subject);
- // Assertions are performed via mock expects above
- }
- }
|