1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?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
}
}