5
5
*/
6
6
namespace Magento \Braintree \Test \Unit \Gateway \Command ;
7
7
8
+ use Braintree \IsNode ;
9
+ use Braintree \MultipleValueNode ;
10
+ use Braintree \TextNode ;
8
11
use Magento \Braintree \Gateway \Command \CaptureStrategyCommand ;
9
12
use Magento \Braintree \Gateway \Helper \SubjectReader ;
10
13
use Magento \Framework \Api \FilterBuilder ;
18
21
use Magento \Sales \Model \Order \Payment ;
19
22
use Magento \Sales \Model \Order \Payment \Transaction ;
20
23
use Magento \Sales \Model \ResourceModel \Order \Payment \Transaction \CollectionFactory ;
24
+ use Magento \Braintree \Model \Adapter \BraintreeAdapter ;
25
+ use Magento \Braintree \Model \Adapter \BraintreeSearchAdapter ;
21
26
22
27
/**
23
28
* Class CaptureStrategyCommandTest
27
32
class CaptureStrategyCommandTest extends \PHPUnit_Framework_TestCase
28
33
{
29
34
/**
30
- * @var \Magento\Braintree\Gateway\Command\ CaptureStrategyCommand
35
+ * @var CaptureStrategyCommand
31
36
*/
32
37
private $ strategyCommand ;
33
38
34
39
/**
35
- * @var \Magento\Payment\Gateway\Command\ CommandPoolInterface|\PHPUnit_Framework_MockObject_MockObject
40
+ * @var CommandPoolInterface|\PHPUnit_Framework_MockObject_MockObject
36
41
*/
37
42
private $ commandPool ;
38
43
39
44
/**
40
- * @var \Magento\Sales\Api\ TransactionRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
45
+ * @var TransactionRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
41
46
*/
42
47
private $ transactionRepository ;
43
48
44
49
/**
45
- * @var \Magento\Framework\Api\ FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
50
+ * @var FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
46
51
*/
47
52
private $ filterBuilder ;
48
53
49
54
/**
50
- * @var \Magento\Framework\Api\ SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
55
+ * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
51
56
*/
52
57
private $ searchCriteriaBuilder ;
53
58
54
59
/**
55
- * @var \Magento\Sales\Model\Order\ Payment|\PHPUnit_Framework_MockObject_MockObject
60
+ * @var Payment|\PHPUnit_Framework_MockObject_MockObject
56
61
*/
57
62
private $ payment ;
58
63
59
64
/**
60
- * @var \Magento\Payment\Gateway\Command\ GatewayCommand|\PHPUnit_Framework_MockObject_MockObject
65
+ * @var GatewayCommand|\PHPUnit_Framework_MockObject_MockObject
61
66
*/
62
67
private $ command ;
63
68
@@ -66,6 +71,16 @@ class CaptureStrategyCommandTest extends \PHPUnit_Framework_TestCase
66
71
*/
67
72
private $ subjectReaderMock ;
68
73
74
+ /**
75
+ * @var BraintreeAdapter|\PHPUnit_Framework_MockObject_MockObject
76
+ */
77
+ private $ braintreeAdapter ;
78
+
79
+ /**
80
+ * @var BraintreeSearchAdapter
81
+ */
82
+ private $ braintreeSearchAdapter ;
83
+
69
84
protected function setUp ()
70
85
{
71
86
$ this ->commandPool = $ this ->getMockBuilder (CommandPoolInterface::class)
@@ -82,12 +97,19 @@ protected function setUp()
82
97
$ this ->initFilterBuilderMock ();
83
98
$ this ->initSearchCriteriaBuilderMock ();
84
99
100
+ $ this ->braintreeAdapter = $ this ->getMockBuilder (BraintreeAdapter::class)
101
+ ->disableOriginalConstructor ()
102
+ ->getMock ();
103
+ $ this ->braintreeSearchAdapter = new BraintreeSearchAdapter ();
104
+
85
105
$ this ->strategyCommand = new CaptureStrategyCommand (
86
106
$ this ->commandPool ,
87
107
$ this ->transactionRepository ,
88
108
$ this ->filterBuilder ,
89
109
$ this ->searchCriteriaBuilder ,
90
- $ this ->subjectReaderMock
110
+ $ this ->subjectReaderMock ,
111
+ $ this ->braintreeAdapter ,
112
+ $ this ->braintreeSearchAdapter
91
113
);
92
114
}
93
115
@@ -133,6 +155,7 @@ public function testCaptureExecute()
133
155
{
134
156
$ paymentData = $ this ->getPaymentDataObjectMock ();
135
157
$ subject ['payment ' ] = $ paymentData ;
158
+ $ lastTransId = 'txnds ' ;
136
159
137
160
$ this ->subjectReaderMock ->expects (self ::once ())
138
161
->method ('readPayment ' )
@@ -142,6 +165,9 @@ public function testCaptureExecute()
142
165
$ this ->payment ->expects (static ::once ())
143
166
->method ('getAuthorizationTransaction ' )
144
167
->willReturn (true );
168
+ $ this ->payment ->expects (static ::once ())
169
+ ->method ('getLastTransId ' )
170
+ ->willReturn ($ lastTransId );
145
171
146
172
$ this ->payment ->expects (static ::once ())
147
173
->method ('getId ' )
@@ -153,6 +179,12 @@ public function testCaptureExecute()
153
179
->method ('getTotalCount ' )
154
180
->willReturn (0 );
155
181
182
+ // authorization transaction was not expired
183
+ $ collection = $ this ->getNotExpiredExpectedCollection ($ lastTransId );
184
+ $ collection ->expects (static ::once ())
185
+ ->method ('maximumCount ' )
186
+ ->willReturn (0 );
187
+
156
188
$ this ->commandPool ->expects (static ::once ())
157
189
->method ('get ' )
158
190
->with (CaptureStrategyCommand::CAPTURE )
@@ -161,6 +193,91 @@ public function testCaptureExecute()
161
193
$ this ->strategyCommand ->execute ($ subject );
162
194
}
163
195
196
+ /**
197
+ * @param string $lastTransactionId
198
+ * @return \Braintree\ResourceCollection|\PHPUnit_Framework_MockObject_MockObject
199
+ */
200
+ private function getNotExpiredExpectedCollection ($ lastTransactionId )
201
+ {
202
+ $ isExpectations = [
203
+ 'id ' => ['is ' => $ lastTransactionId ],
204
+ 'status ' => [\Braintree \Transaction::AUTHORIZATION_EXPIRED ]
205
+ ];
206
+
207
+ $ collection = $ this ->getMockBuilder (\Braintree \ResourceCollection::class)
208
+ ->disableOriginalConstructor ()
209
+ ->getMock ();
210
+
211
+ $ this ->braintreeAdapter ->expects (static ::once ())
212
+ ->method ('search ' )
213
+ ->with (
214
+ static ::callback (
215
+ function (array $ filters ) use ($ isExpectations ) {
216
+ foreach ($ filters as $ filter ) {
217
+ /** @var IsNode $filter */
218
+ if (!isset ($ isExpectations [$ filter ->name ])) {
219
+ return false ;
220
+ }
221
+
222
+ if ($ isExpectations [$ filter ->name ] !== $ filter ->toParam ()) {
223
+ return false ;
224
+ }
225
+ }
226
+
227
+ return true ;
228
+ }
229
+ )
230
+ )
231
+ ->willReturn ($ collection );
232
+
233
+ return $ collection ;
234
+ }
235
+
236
+ /**
237
+ * @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
238
+ */
239
+ public function testExpiredAuthorizationPerformVaultCaptureExecute ()
240
+ {
241
+ $ paymentData = $ this ->getPaymentDataObjectMock ();
242
+ $ subject ['payment ' ] = $ paymentData ;
243
+ $ lastTransId = 'txnds ' ;
244
+
245
+ $ this ->subjectReaderMock ->expects (self ::once ())
246
+ ->method ('readPayment ' )
247
+ ->with ($ subject )
248
+ ->willReturn ($ paymentData );
249
+
250
+ $ this ->payment ->expects (static ::once ())
251
+ ->method ('getAuthorizationTransaction ' )
252
+ ->willReturn (true );
253
+ $ this ->payment ->expects (static ::once ())
254
+ ->method ('getLastTransId ' )
255
+ ->willReturn ($ lastTransId );
256
+
257
+ $ this ->payment ->expects (static ::once ())
258
+ ->method ('getId ' )
259
+ ->willReturn (1 );
260
+
261
+ $ this ->buildSearchCriteria ();
262
+
263
+ $ this ->transactionRepository ->expects (static ::once ())
264
+ ->method ('getTotalCount ' )
265
+ ->willReturn (0 );
266
+
267
+ // authorization transaction was expired
268
+ $ collection = $ this ->getNotExpiredExpectedCollection ($ lastTransId );
269
+ $ collection ->expects (static ::once ())
270
+ ->method ('maximumCount ' )
271
+ ->willReturn (1 );
272
+
273
+ $ this ->commandPool ->expects (static ::once ())
274
+ ->method ('get ' )
275
+ ->with (CaptureStrategyCommand::VAULT_CAPTURE )
276
+ ->willReturn ($ this ->command );
277
+
278
+ $ this ->strategyCommand ->execute ($ subject );
279
+ }
280
+
164
281
/**
165
282
* @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
166
283
*/
@@ -204,7 +321,6 @@ private function getPaymentDataObjectMock()
204
321
{
205
322
$ this ->payment = $ this ->getMockBuilder (Payment::class)
206
323
->disableOriginalConstructor ()
207
- ->setMethods (['getAuthorizationTransaction ' , 'getId ' , 'getExtensionAttributes ' ])
208
324
->getMock ();
209
325
210
326
$ mock = $ this ->getMockBuilder (PaymentDataObject::class)
0 commit comments