Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CaptureStrategyCommandTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\IsNode;
23 use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 
31 class CaptureStrategyCommandTest extends \PHPUnit\Framework\TestCase
32 {
36  private $strategyCommand;
37 
41  private $commandPoolMock;
42 
46  private $transactionRepositoryMock;
47 
51  private $filterBuilderMock;
52 
56  private $searchCriteriaBuilderMock;
57 
61  private $paymentMock;
62 
66  private $commandMock;
67 
71  private $subjectReaderMock;
72 
76  private $braintreeAdapterMock;
77 
81  private $braintreeSearchAdapter;
82 
83  protected function setUp()
84  {
85  $this->commandPoolMock = $this->getMockBuilder(CommandPoolInterface::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['get', '__wakeup'])
88  ->getMock();
89 
90  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93 
94  $this->initCommandMock();
95  $this->initTransactionRepositoryMock();
96  $this->initFilterBuilderMock();
97  $this->initSearchCriteriaBuilderMock();
98 
99  $this->braintreeAdapterMock = $this->getMockBuilder(BraintreeAdapter::class)
100  ->disableOriginalConstructor()
101  ->getMock();
103  $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106  $adapterFactoryMock->expects(self::any())
107  ->method('create')
108  ->willReturn($this->braintreeAdapterMock);
109 
110  $this->braintreeSearchAdapter = new BraintreeSearchAdapter();
111 
112  $this->strategyCommand = new CaptureStrategyCommand(
113  $this->commandPoolMock,
114  $this->transactionRepositoryMock,
115  $this->filterBuilderMock,
116  $this->searchCriteriaBuilderMock,
117  $this->subjectReaderMock,
118  $adapterFactoryMock,
119  $this->braintreeSearchAdapter
120  );
121  }
122 
126  public function testSaleExecute()
127  {
128  $paymentData = $this->getPaymentDataObjectMock();
129  $subject['payment'] = $paymentData;
130 
131  $this->subjectReaderMock->expects(self::once())
132  ->method('readPayment')
133  ->with($subject)
134  ->willReturn($paymentData);
135 
136  $this->paymentMock->expects(static::once())
137  ->method('getAuthorizationTransaction')
138  ->willReturn(false);
139 
140  $this->paymentMock->expects(static::once())
141  ->method('getId')
142  ->willReturn(1);
143 
144  $this->buildSearchCriteria();
145 
146  $this->transactionRepositoryMock->expects(static::once())
147  ->method('getTotalCount')
148  ->willReturn(0);
149 
150  $this->commandPoolMock->expects(static::once())
151  ->method('get')
153  ->willReturn($this->commandMock);
154 
155  $this->strategyCommand->execute($subject);
156  }
157 
161  public function testCaptureExecute()
162  {
163  $paymentData = $this->getPaymentDataObjectMock();
164  $subject['payment'] = $paymentData;
165  $lastTransId = 'txnds';
166 
167  $this->subjectReaderMock->expects(self::once())
168  ->method('readPayment')
169  ->with($subject)
170  ->willReturn($paymentData);
171 
172  $this->paymentMock->expects(static::once())
173  ->method('getAuthorizationTransaction')
174  ->willReturn(true);
175  $this->paymentMock->expects(static::once())
176  ->method('getLastTransId')
177  ->willReturn($lastTransId);
178 
179  $this->paymentMock->expects(static::once())
180  ->method('getId')
181  ->willReturn(1);
182 
183  $this->buildSearchCriteria();
184 
185  $this->transactionRepositoryMock->expects(static::once())
186  ->method('getTotalCount')
187  ->willReturn(0);
188 
189  // authorization transaction was not expired
190  $collection = $this->getNotExpiredExpectedCollection($lastTransId);
191  $collection->expects(static::once())
192  ->method('maximumCount')
193  ->willReturn(0);
194 
195  $this->commandPoolMock->expects(static::once())
196  ->method('get')
198  ->willReturn($this->commandMock);
199 
200  $this->strategyCommand->execute($subject);
201  }
202 
207  private function getNotExpiredExpectedCollection($lastTransactionId)
208  {
209  $isExpectations = [
210  'id' => ['is' => $lastTransactionId],
211  'status' => [\Braintree\Transaction::AUTHORIZATION_EXPIRED]
212  ];
213 
214  $collection = $this->getMockBuilder(\Braintree\ResourceCollection::class)
215  ->disableOriginalConstructor()
216  ->getMock();
217 
218  $this->braintreeAdapterMock->expects(static::once())
219  ->method('search')
220  ->with(
221  static::callback(
222  function (array $filters) use ($isExpectations) {
223  foreach ($filters as $filter) {
225  if (!isset($isExpectations[$filter->name])) {
226  return false;
227  }
228 
229  if ($isExpectations[$filter->name] !== $filter->toParam()) {
230  return false;
231  }
232  }
233 
234  return true;
235  }
236  )
237  )
238  ->willReturn($collection);
239 
240  return $collection;
241  }
242 
247  {
248  $paymentData = $this->getPaymentDataObjectMock();
249  $subject['payment'] = $paymentData;
250  $lastTransId = 'txnds';
251 
252  $this->subjectReaderMock->expects(self::once())
253  ->method('readPayment')
254  ->with($subject)
255  ->willReturn($paymentData);
256 
257  $this->paymentMock->expects(static::once())
258  ->method('getAuthorizationTransaction')
259  ->willReturn(true);
260  $this->paymentMock->expects(static::once())
261  ->method('getLastTransId')
262  ->willReturn($lastTransId);
263 
264  $this->paymentMock->expects(static::once())
265  ->method('getId')
266  ->willReturn(1);
267 
268  $this->buildSearchCriteria();
269 
270  $this->transactionRepositoryMock->expects(static::once())
271  ->method('getTotalCount')
272  ->willReturn(0);
273 
274  // authorization transaction was expired
275  $collection = $this->getNotExpiredExpectedCollection($lastTransId);
276  $collection->expects(static::once())
277  ->method('maximumCount')
278  ->willReturn(1);
279 
280  $this->commandPoolMock->expects(static::once())
281  ->method('get')
283  ->willReturn($this->commandMock);
284 
285  $this->strategyCommand->execute($subject);
286  }
287 
291  public function testVaultCaptureExecute()
292  {
293  $paymentData = $this->getPaymentDataObjectMock();
294  $subject['payment'] = $paymentData;
295 
296  $this->subjectReaderMock->expects(self::once())
297  ->method('readPayment')
298  ->with($subject)
299  ->willReturn($paymentData);
300 
301  $this->paymentMock->expects(static::once())
302  ->method('getAuthorizationTransaction')
303  ->willReturn(true);
304 
305  $this->paymentMock->expects(static::once())
306  ->method('getId')
307  ->willReturn(1);
308 
309  $this->buildSearchCriteria();
310 
311  $this->transactionRepositoryMock->expects(static::once())
312  ->method('getTotalCount')
313  ->willReturn(1);
314 
315  $this->commandPoolMock->expects(static::once())
316  ->method('get')
318  ->willReturn($this->commandMock);
319 
320  $this->strategyCommand->execute($subject);
321  }
322 
327  private function getPaymentDataObjectMock()
328  {
329  $this->paymentMock = $this->getMockBuilder(Payment::class)
330  ->disableOriginalConstructor()
331  ->getMock();
332 
333  $mock = $this->getMockBuilder(PaymentDataObject::class)
334  ->setMethods(['getPayment', 'getOrder'])
335  ->disableOriginalConstructor()
336  ->getMock();
337 
338  $mock->expects(static::once())
339  ->method('getPayment')
340  ->willReturn($this->paymentMock);
341 
342  $orderMock = $this->getMockBuilder(OrderAdapterInterface::class)
343  ->disableOriginalConstructor()
344  ->getMock();
345 
346  $mock->method('getOrder')
347  ->willReturn($orderMock);
348 
349  return $mock;
350  }
351 
355  private function initCommandMock()
356  {
357  $this->commandMock = $this->getMockBuilder(GatewayCommand::class)
358  ->disableOriginalConstructor()
359  ->setMethods(['execute'])
360  ->getMock();
361 
362  $this->commandMock->expects(static::once())
363  ->method('execute')
364  ->willReturn([]);
365  }
366 
370  private function initFilterBuilderMock()
371  {
372  $this->filterBuilderMock = $this->getMockBuilder(FilterBuilder::class)
373  ->disableOriginalConstructor()
374  ->setMethods(['setField', 'setValue', 'create', '__wakeup'])
375  ->getMock();
376  }
377 
381  private function buildSearchCriteria()
382  {
383  $this->filterBuilderMock->expects(static::exactly(2))
384  ->method('setField')
385  ->willReturnSelf();
386  $this->filterBuilderMock->expects(static::exactly(2))
387  ->method('setValue')
388  ->willReturnSelf();
389 
390  $searchCriteria = new SearchCriteria();
391  $this->searchCriteriaBuilderMock->expects(static::exactly(2))
392  ->method('addFilters')
393  ->willReturnSelf();
394  $this->searchCriteriaBuilderMock->expects(static::once())
395  ->method('create')
396  ->willReturn($searchCriteria);
397 
398  $this->transactionRepositoryMock->expects(static::once())
399  ->method('getList')
400  ->with($searchCriteria)
401  ->willReturnSelf();
402  }
403 
407  private function initSearchCriteriaBuilderMock()
408  {
409  $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
410  ->disableOriginalConstructor()
411  ->setMethods(['addFilters', 'create', '__wakeup'])
412  ->getMock();
413  }
414 
418  private function initTransactionRepositoryMock()
419  {
420  $this->transactionRepositoryMock = $this->getMockBuilder(TransactionRepositoryInterface::class)
421  ->disableOriginalConstructor()
422  ->setMethods(['getList', 'getTotalCount', 'delete', 'get', 'save', 'create', '__wakeup'])
423  ->getMock();
424  }
425 }
if(!defined( 'PHP_VERSION_ID')||!(PHP_VERSION_ID===70002||PHP_VERSION_ID===70004||PHP_VERSION_ID >=70006))
Definition: bootstrap.php:14
$searchCriteria
$filters
Definition: uploader.phtml:11