Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentTokenManagementTest.php
Go to the documentation of this file.
1 <?php
7 
18 use Magento\Vault\Api\Data\PaymentTokenSearchResultsInterfaceFactory;
23 
30 class PaymentTokenManagementTest extends \PHPUnit\Framework\TestCase
31 {
35  private $paymentTokenManagement;
36 
40  private $paymentTokenRepository;
41 
45  private $paymentTokenResourceModel;
46 
50  private $resourceModel;
51 
55  private $paymentTokenFactory;
56 
60  private $searchResultsFactory;
61 
65  private $filterBuilder;
66 
70  private $searchCriteriaBuilder;
71 
75  private $encryptor;
76 
80  private $dateTimeFactory;
81 
85  protected function setUp()
86  {
87  $this->paymentTokenRepository = $this->getMockBuilder(PaymentTokenRepositoryInterface::class)
88  ->getMockForAbstractClass();
89  $this->paymentTokenResourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->resourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->paymentTokenFactory = $this->getMockBuilder(PaymentTokenFactory::class)
96  ->setMethods(['create'])
97  ->disableOriginalConstructor()
98  ->getMock();
99  $this->searchResultsFactory = $this->getMockBuilder(PaymentTokenSearchResultsInterfaceFactory::class)
100  ->setMethods(['create'])
101  ->disableOriginalConstructor()
102  ->getMock();
103  $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106  $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $this->encryptor = $this->createMock(EncryptorInterface::class);
110  $this->dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113 
114  $this->paymentTokenManagement = new PaymentTokenManagement(
115  $this->paymentTokenRepository,
116  $this->paymentTokenResourceModel,
117  $this->paymentTokenFactory,
118  $this->filterBuilder,
119  $this->searchCriteriaBuilder,
120  $this->searchResultsFactory,
121  $this->encryptor,
122  $this->dateTimeFactory
123  );
124  }
125 
129  public function testGetListByCustomerId()
130  {
132  $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
133  ->getMockForAbstractClass();
135  $filterMock = $this->getMockBuilder(Filter::class)
136  ->disableOriginalConstructor()
137  ->getMock();
138  $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
139  ->disableOriginalConstructor()
140  ->getMock();
141  $searchResult = $this->getMockBuilder(PaymentTokenSearchResultsInterface::class)
142  ->getMockForAbstractClass();
143 
144  $this->filterBuilder->expects(self::once())
145  ->method('setField')
147  ->willReturnSelf();
148  $this->filterBuilder->expects(self::once())
149  ->method('setValue')
150  ->with(1)
151  ->willReturnSelf();
152  $this->filterBuilder->expects(self::once())
153  ->method('create')
154  ->willReturn($filterMock);
155 
156  $this->searchCriteriaBuilder->expects(self::once())
157  ->method('addFilters')
158  ->with([$filterMock])
159  ->willReturnSelf();
160  $this->searchCriteriaBuilder->expects(self::once())
161  ->method('create')
162  ->willReturn($searchCriteria);
163 
164  $this->paymentTokenRepository->expects(self::once())
165  ->method('getList')
166  ->with($searchCriteria)
167  ->willReturn($searchResult);
168 
169  $searchResult->expects(self::once())
170  ->method('getItems')
171  ->willReturn([$tokenMock]);
172 
173  self::assertEquals([$tokenMock], $this->paymentTokenManagement->getListByCustomerId(1));
174  }
175 
179  public function testGetByPaymentId()
180  {
182  $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
183  ->getMockForAbstractClass();
184 
185  $this->paymentTokenResourceModel->expects(self::once())
186  ->method('getByOrderPaymentId')
187  ->with(1)
188  ->willReturn(['token-data']);
189 
190  $this->paymentTokenFactory->expects(self::once())
191  ->method('create')
192  ->with(['data' => ['token-data']])
193  ->willReturn($tokenMock);
194 
195  self::assertEquals($tokenMock, $this->paymentTokenManagement->getByPaymentId(1));
196  }
197 
201  public function testGetByPaymentIdNull()
202  {
203  $this->paymentTokenResourceModel->expects(self::once())
204  ->method('getByOrderPaymentId')
205  ->with(1)
206  ->willReturn([]);
207 
208  $this->paymentTokenFactory->expects(self::never())
209  ->method('create');
210 
211  self::assertEquals(null, $this->paymentTokenManagement->getByPaymentId(1));
212  }
213 
217  public function testGetByGatewayToken()
218  {
220  $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
221  ->getMockForAbstractClass();
222 
223  $this->paymentTokenResourceModel->expects(self::once())
224  ->method('getByGatewayToken')
225  ->with('token', 1, 1)
226  ->willReturn(['token-data']);
227 
228  $this->paymentTokenFactory->expects(self::once())
229  ->method('create')
230  ->with(['data' => ['token-data']])
231  ->willReturn($tokenMock);
232 
233  self::assertEquals($tokenMock, $this->paymentTokenManagement->getByGatewayToken('token', 1, 1));
234  }
235 
239  public function testGetByGatewayTokenNull()
240  {
241  $this->paymentTokenResourceModel->expects(self::once())
242  ->method('getByGatewayToken')
243  ->with('some-not-exists-token', 1, 1)
244  ->willReturn([]);
245 
246  $this->paymentTokenFactory->expects(self::never())
247  ->method('create');
248 
249  self::assertEquals(null, $this->paymentTokenManagement->getByGatewayToken('some-not-exists-token', 1, 1));
250  }
251 
255  public function testGetByPublicHash()
256  {
257  $this->paymentTokenResourceModel->expects(self::once())
258  ->method('getByPublicHash')
259  ->with('some-not-exists-token', 1)
260  ->willReturn([]);
261 
262  $this->paymentTokenFactory->expects(self::never())
263  ->method('create');
264 
265  self::assertEquals(null, $this->paymentTokenManagement->getByPublicHash('some-not-exists-token', 1));
266  }
267 
271  public function testSaveTokenWithPaymentLinkNoDuplicate()
272  {
274  $paymentMock = $this->createMock(OrderPaymentInterface::class);
276  $tokenMock = $this->createMock(PaymentTokenInterface::class);
277 
278  $customerId = 1;
279  $entityId = 1;
280  $publicHash = 'some-not-existing-token';
281  $paymentId = 1;
282 
283  $tokenMock->expects(static::atLeastOnce())
284  ->method('getPublicHash')
285  ->willReturn($publicHash);
286  $tokenMock->expects(static::atLeastOnce())
287  ->method('getCustomerId')
288  ->willReturn($customerId);
289 
290  $this->paymentTokenResourceModel->expects(self::once())
291  ->method('getByPublicHash')
292  ->with($publicHash, 1)
293  ->willReturn([]);
294 
295  $this->paymentTokenFactory->expects(self::never())
296  ->method('create');
297 
298  $tokenMock->expects(self::once())
299  ->method('getEntityId')
300  ->willReturn($entityId);
301  $this->paymentTokenRepository->expects(self::once())
302  ->method('save')
303  ->with($tokenMock);
304 
305  $paymentMock->expects(self::once())
306  ->method('getEntityId')
307  ->willReturn($paymentId);
308 
309  $this->paymentTokenResourceModel->expects(static::once())
310  ->method('addLinkToOrderPayment')
311  ->with($entityId, $paymentId);
312 
313  $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
314  }
315 
319  public function testSaveTokenWithPaymentLinkWithDuplicateTokenVisible()
320  {
322  $paymentMock = $this->createMock(OrderPaymentInterface::class);
324  $tokenMock = $this->createMock(PaymentTokenInterface::class);
326  $duplicateToken = $this->createMock(PaymentTokenInterface::class);
327 
328  $entityId = 1;
329  $customerId = 1;
330  $paymentId = 1;
331  $publicHash = 'existing-token';
332  $duplicateTokenData = [
333  'entity_id' => $entityId
334  ];
335 
336  $tokenMock->expects(static::atLeastOnce())
337  ->method('getPublicHash')
338  ->willReturn($publicHash);
339  $tokenMock->expects(static::atLeastOnce())
340  ->method('getCustomerId')
341  ->willReturn($customerId);
342 
343  $this->paymentTokenResourceModel->expects(self::once())
344  ->method('getByPublicHash')
345  ->with($publicHash, $customerId)
346  ->willReturn($duplicateTokenData);
347 
348  $this->paymentTokenFactory->expects(self::once())
349  ->method('create')
350  ->with(['data' => $duplicateTokenData])
351  ->willReturn($duplicateToken);
352  $tokenMock->expects(static::once())
353  ->method('getIsVisible')
354  ->willReturn(true);
355  $duplicateToken->expects(static::once())
356  ->method('getEntityId')
357  ->willReturn($entityId);
358  $tokenMock->expects(self::once())
359  ->method('getEntityId')
360  ->willReturn($entityId);
361  $this->paymentTokenRepository->expects(self::once())
362  ->method('save')
363  ->with($tokenMock);
364 
365  $paymentMock->expects(self::once())
366  ->method('getEntityId')
367  ->willReturn($paymentId);
368 
369  $this->paymentTokenResourceModel->expects(static::once())
370  ->method('addLinkToOrderPayment')
371  ->with($entityId, $paymentId);
372 
373  $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
374  }
375 
379  public function testSaveTokenWithPaymentLinkWithDuplicateTokenNotVisible()
380  {
382  $paymentMock = $this->createMock(OrderPaymentInterface::class);
384  $tokenMock = $this->createMock(PaymentTokenInterface::class);
386  $duplicateToken = $this->createMock(PaymentTokenInterface::class);
387 
388  $entityId = 1;
389  $newEntityId = 1;
390  $paymentId = 1;
391  $customerId = 1;
392  $gatewayToken = 'xs4vf3';
393  $publicHash = 'existing-token';
394  $duplicateTokenData = [
395  'entity_id' => $entityId
396  ];
397  $newHash = 'new-token2';
398 
399  $tokenMock->expects(static::atLeastOnce())
400  ->method('getPublicHash')
401  ->willReturn($publicHash);
402  $tokenMock->expects(static::atLeastOnce())
403  ->method('getCustomerId')
404  ->willReturn($customerId);
405 
406  $this->paymentTokenResourceModel->expects(self::once())
407  ->method('getByPublicHash')
408  ->with($publicHash, $customerId)
409  ->willReturn($duplicateTokenData);
410 
411  $this->paymentTokenFactory->expects(self::once())
412  ->method('create')
413  ->with(['data' => $duplicateTokenData])
414  ->willReturn($duplicateToken);
415  $tokenMock->expects(static::atLeastOnce())
416  ->method('getIsVisible')
417  ->willReturn(false);
418  $tokenMock->expects(static::atLeastOnce())
419  ->method('getCustomerId')
420  ->willReturn($customerId);
421  $tokenMock->expects(static::atLeastOnce())
422  ->method('getGatewayToken')
423  ->willReturn($gatewayToken);
424 
425  $this->encryptor->expects(static::once())
426  ->method('getHash')
427  ->with($publicHash . $gatewayToken)
428  ->willReturn($newHash);
429  $tokenMock->expects(static::once())
430  ->method('setPublicHash')
431  ->with($newHash);
432 
433  $this->paymentTokenRepository->expects(self::once())
434  ->method('save')
435  ->with($tokenMock);
436  $tokenMock->expects(static::atLeastOnce())
437  ->method('getEntityId')
438  ->willReturn($newEntityId);
439 
440  $paymentMock->expects(self::atLeastOnce())
441  ->method('getEntityId')
442  ->willReturn($paymentId);
443  $this->paymentTokenResourceModel->expects(static::once())
444  ->method('addLinkToOrderPayment')
445  ->with($newEntityId, $paymentId);
446 
447  $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
448  }
449 
451  {
452  $customerId = 1;
453 
454  $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
455  ->disableOriginalConstructor()
456  ->getMock();
457  $searchResult = $this->getMockForAbstractClass(PaymentTokenSearchResultsInterface::class);
458  $token = $this->getMockForAbstractClass(PaymentTokenInterface::class);
459 
460  $customerFilter = $this->createExpectedFilter(PaymentTokenInterface::CUSTOMER_ID, $customerId, 0);
461  $visibilityFilter = $this->createExpectedFilter(PaymentTokenInterface::IS_VISIBLE, true, 1);
462  $isActiveFilter = $this->createExpectedFilter(PaymentTokenInterface::IS_ACTIVE, true, 2);
463 
464  // express at expectations
465  $expiresAtFilter = $this->createExpectedFilter(
467  '2015-01-01 00:00:00',
468  3
469  );
470  $this->filterBuilder->expects(static::once())
471  ->method('setConditionType')
472  ->with('gt')
473  ->willReturnSelf();
474 
475  $date = $this->getMockBuilder(\DateTime::class)
476  ->disableOriginalConstructor()
477  ->getMock();
478  $this->dateTimeFactory->expects(static::once())
479  ->method('create')
480  ->with("now", new \DateTimeZone('UTC'))
481  ->willReturn($date);
482  $date->expects(static::once())
483  ->method('format')
484  ->with('Y-m-d 00:00:00')
485  ->willReturn('2015-01-01 00:00:00');
486 
487  $this->searchCriteriaBuilder->expects(self::exactly(4))
488  ->method('addFilters')
489  ->withConsecutive($customerFilter, $visibilityFilter, $isActiveFilter, $expiresAtFilter)
490  ->willReturnSelf();
491 
492  $this->searchCriteriaBuilder->expects(self::once())
493  ->method('create')
494  ->willReturn($searchCriteria);
495 
496  $this->paymentTokenRepository->expects(self::once())
497  ->method('getList')
498  ->with($searchCriteria)
499  ->willReturn($searchResult);
500 
501  $searchResult->expects(self::once())
502  ->method('getItems')
503  ->willReturn([$token]);
504 
505  static::assertEquals(
506  [$token],
507  $this->paymentTokenManagement->getVisibleAvailableTokens($customerId)
508  );
509  }
510 
518  private function createExpectedFilter($field, $value, $atIndex)
519  {
520  $filterObject = $this->getMockBuilder(Filter::class)
521  ->disableOriginalConstructor()
522  ->getMock();
523  $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
524  ->method('setField')
525  ->with($field)
526  ->willReturnSelf();
527  $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
528  ->method('setValue')
529  ->with($value)
530  ->willReturnSelf();
531  $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
532  ->method('create')
533  ->willReturn($filterObject);
534 
535  return $filterObject;
536  }
537 }
$searchCriteria
$value
Definition: gender.phtml:16