18 use Magento\Vault\Api\Data\PaymentTokenSearchResultsInterfaceFactory;
35 private $paymentTokenManagement;
40 private $paymentTokenRepository;
45 private $paymentTokenResourceModel;
50 private $resourceModel;
55 private $paymentTokenFactory;
60 private $searchResultsFactory;
65 private $filterBuilder;
70 private $searchCriteriaBuilder;
80 private $dateTimeFactory;
87 $this->paymentTokenRepository = $this->getMockBuilder(PaymentTokenRepositoryInterface::class)
88 ->getMockForAbstractClass();
89 $this->paymentTokenResourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
90 ->disableOriginalConstructor()
92 $this->resourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
93 ->disableOriginalConstructor()
95 $this->paymentTokenFactory = $this->getMockBuilder(PaymentTokenFactory::class)
96 ->setMethods([
'create'])
97 ->disableOriginalConstructor()
99 $this->searchResultsFactory = $this->getMockBuilder(PaymentTokenSearchResultsInterfaceFactory::class)
100 ->setMethods([
'create'])
101 ->disableOriginalConstructor()
103 $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
104 ->disableOriginalConstructor()
106 $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
107 ->disableOriginalConstructor()
109 $this->encryptor = $this->createMock(EncryptorInterface::class);
110 $this->dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
111 ->disableOriginalConstructor()
115 $this->paymentTokenRepository,
116 $this->paymentTokenResourceModel,
117 $this->paymentTokenFactory,
118 $this->filterBuilder,
119 $this->searchCriteriaBuilder,
120 $this->searchResultsFactory,
122 $this->dateTimeFactory
129 public function testGetListByCustomerId()
132 $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
133 ->getMockForAbstractClass();
135 $filterMock = $this->getMockBuilder(Filter::class)
136 ->disableOriginalConstructor()
139 ->disableOriginalConstructor()
141 $searchResult = $this->getMockBuilder(PaymentTokenSearchResultsInterface::class)
142 ->getMockForAbstractClass();
144 $this->filterBuilder->expects(self::once())
148 $this->filterBuilder->expects(self::once())
152 $this->filterBuilder->expects(self::once())
154 ->willReturn($filterMock);
156 $this->searchCriteriaBuilder->expects(self::once())
157 ->method(
'addFilters')
158 ->with([$filterMock])
160 $this->searchCriteriaBuilder->expects(self::once())
164 $this->paymentTokenRepository->expects(self::once())
167 ->willReturn($searchResult);
169 $searchResult->expects(self::once())
171 ->willReturn([$tokenMock]);
173 self::assertEquals([$tokenMock], $this->paymentTokenManagement->getListByCustomerId(1));
179 public function testGetByPaymentId()
182 $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
183 ->getMockForAbstractClass();
185 $this->paymentTokenResourceModel->expects(self::once())
186 ->method(
'getByOrderPaymentId')
188 ->willReturn([
'token-data']);
190 $this->paymentTokenFactory->expects(self::once())
192 ->with([
'data' => [
'token-data']])
193 ->willReturn($tokenMock);
195 self::assertEquals($tokenMock, $this->paymentTokenManagement->getByPaymentId(1));
203 $this->paymentTokenResourceModel->expects(self::once())
204 ->method(
'getByOrderPaymentId')
208 $this->paymentTokenFactory->expects(self::never())
211 self::assertEquals(
null, $this->paymentTokenManagement->getByPaymentId(1));
217 public function testGetByGatewayToken()
220 $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
221 ->getMockForAbstractClass();
223 $this->paymentTokenResourceModel->expects(self::once())
224 ->method(
'getByGatewayToken')
225 ->with(
'token', 1, 1)
226 ->willReturn([
'token-data']);
228 $this->paymentTokenFactory->expects(self::once())
230 ->with([
'data' => [
'token-data']])
231 ->willReturn($tokenMock);
233 self::assertEquals($tokenMock, $this->paymentTokenManagement->getByGatewayToken(
'token', 1, 1));
241 $this->paymentTokenResourceModel->expects(self::once())
242 ->method(
'getByGatewayToken')
243 ->with(
'some-not-exists-token', 1, 1)
246 $this->paymentTokenFactory->expects(self::never())
249 self::assertEquals(
null, $this->paymentTokenManagement->getByGatewayToken(
'some-not-exists-token', 1, 1));
257 $this->paymentTokenResourceModel->expects(self::once())
258 ->method(
'getByPublicHash')
259 ->with(
'some-not-exists-token', 1)
262 $this->paymentTokenFactory->expects(self::never())
265 self::assertEquals(
null, $this->paymentTokenManagement->getByPublicHash(
'some-not-exists-token', 1));
271 public function testSaveTokenWithPaymentLinkNoDuplicate()
274 $paymentMock = $this->createMock(OrderPaymentInterface::class);
276 $tokenMock = $this->createMock(PaymentTokenInterface::class);
280 $publicHash =
'some-not-existing-token';
283 $tokenMock->expects(static::atLeastOnce())
284 ->method(
'getPublicHash')
285 ->willReturn($publicHash);
286 $tokenMock->expects(static::atLeastOnce())
287 ->method(
'getCustomerId')
290 $this->paymentTokenResourceModel->expects(self::once())
291 ->method(
'getByPublicHash')
292 ->with($publicHash, 1)
295 $this->paymentTokenFactory->expects(self::never())
298 $tokenMock->expects(self::once())
299 ->method(
'getEntityId')
300 ->willReturn($entityId);
301 $this->paymentTokenRepository->expects(self::once())
305 $paymentMock->expects(self::once())
306 ->method(
'getEntityId')
307 ->willReturn($paymentId);
309 $this->paymentTokenResourceModel->expects(static::once())
310 ->method(
'addLinkToOrderPayment')
311 ->with($entityId, $paymentId);
313 $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
319 public function testSaveTokenWithPaymentLinkWithDuplicateTokenVisible()
322 $paymentMock = $this->createMock(OrderPaymentInterface::class);
324 $tokenMock = $this->createMock(PaymentTokenInterface::class);
326 $duplicateToken = $this->createMock(PaymentTokenInterface::class);
331 $publicHash =
'existing-token';
332 $duplicateTokenData = [
333 'entity_id' => $entityId
336 $tokenMock->expects(static::atLeastOnce())
337 ->method(
'getPublicHash')
338 ->willReturn($publicHash);
339 $tokenMock->expects(static::atLeastOnce())
340 ->method(
'getCustomerId')
343 $this->paymentTokenResourceModel->expects(self::once())
344 ->method(
'getByPublicHash')
346 ->willReturn($duplicateTokenData);
348 $this->paymentTokenFactory->expects(self::once())
350 ->with([
'data' => $duplicateTokenData])
351 ->willReturn($duplicateToken);
352 $tokenMock->expects(static::once())
353 ->method(
'getIsVisible')
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())
365 $paymentMock->expects(self::once())
366 ->method(
'getEntityId')
367 ->willReturn($paymentId);
369 $this->paymentTokenResourceModel->expects(static::once())
370 ->method(
'addLinkToOrderPayment')
371 ->with($entityId, $paymentId);
373 $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
379 public function testSaveTokenWithPaymentLinkWithDuplicateTokenNotVisible()
382 $paymentMock = $this->createMock(OrderPaymentInterface::class);
384 $tokenMock = $this->createMock(PaymentTokenInterface::class);
386 $duplicateToken = $this->createMock(PaymentTokenInterface::class);
392 $gatewayToken =
'xs4vf3';
393 $publicHash =
'existing-token';
394 $duplicateTokenData = [
395 'entity_id' => $entityId
397 $newHash =
'new-token2';
399 $tokenMock->expects(static::atLeastOnce())
400 ->method(
'getPublicHash')
401 ->willReturn($publicHash);
402 $tokenMock->expects(static::atLeastOnce())
403 ->method(
'getCustomerId')
406 $this->paymentTokenResourceModel->expects(self::once())
407 ->method(
'getByPublicHash')
409 ->willReturn($duplicateTokenData);
411 $this->paymentTokenFactory->expects(self::once())
413 ->with([
'data' => $duplicateTokenData])
414 ->willReturn($duplicateToken);
415 $tokenMock->expects(static::atLeastOnce())
416 ->method(
'getIsVisible')
418 $tokenMock->expects(static::atLeastOnce())
419 ->method(
'getCustomerId')
421 $tokenMock->expects(static::atLeastOnce())
422 ->method(
'getGatewayToken')
423 ->willReturn($gatewayToken);
425 $this->encryptor->expects(static::once())
427 ->with($publicHash . $gatewayToken)
428 ->willReturn($newHash);
429 $tokenMock->expects(static::once())
430 ->method(
'setPublicHash')
433 $this->paymentTokenRepository->expects(self::once())
436 $tokenMock->expects(static::atLeastOnce())
437 ->method(
'getEntityId')
438 ->willReturn($newEntityId);
440 $paymentMock->expects(self::atLeastOnce())
441 ->method(
'getEntityId')
442 ->willReturn($paymentId);
443 $this->paymentTokenResourceModel->expects(static::once())
444 ->method(
'addLinkToOrderPayment')
445 ->with($newEntityId, $paymentId);
447 $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
455 ->disableOriginalConstructor()
457 $searchResult = $this->getMockForAbstractClass(PaymentTokenSearchResultsInterface::class);
458 $token = $this->getMockForAbstractClass(PaymentTokenInterface::class);
465 $expiresAtFilter = $this->createExpectedFilter(
467 '2015-01-01 00:00:00',
470 $this->filterBuilder->expects(static::once())
471 ->method(
'setConditionType')
475 $date = $this->getMockBuilder(\DateTime::class)
476 ->disableOriginalConstructor()
478 $this->dateTimeFactory->expects(static::once())
480 ->with(
"now",
new \DateTimeZone(
'UTC'))
482 $date->expects(static::once())
484 ->with(
'Y-m-d 00:00:00')
485 ->willReturn(
'2015-01-01 00:00:00');
487 $this->searchCriteriaBuilder->expects(self::exactly(4))
488 ->method(
'addFilters')
489 ->withConsecutive($customerFilter, $visibilityFilter, $isActiveFilter, $expiresAtFilter)
492 $this->searchCriteriaBuilder->expects(self::once())
496 $this->paymentTokenRepository->expects(self::once())
499 ->willReturn($searchResult);
501 $searchResult->expects(self::once())
505 static::assertEquals(
507 $this->paymentTokenManagement->getVisibleAvailableTokens(
$customerId)
518 private function createExpectedFilter($field,
$value, $atIndex)
520 $filterObject = $this->getMockBuilder(Filter::class)
521 ->disableOriginalConstructor()
533 ->willReturn($filterObject);
535 return $filterObject;
testGetByGatewayTokenNull()
testGetVisibleAvailableTokens()