|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Braintree\Test\Unit\Model\InstantPurchase\Paypal; |
| 9 | + |
| 10 | +use Magento\Braintree\Model\InstantPurchase\CreditCard\TokenFormatter as PaypalTokenFormatter; |
| 11 | +use Magento\Vault\Api\Data\PaymentTokenInterface; |
| 12 | + |
| 13 | +class TokenFormatterTest extends \PHPUnit\Framework\TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject |
| 17 | + */ |
| 18 | + private $paymentTokenMock; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var PaypalTokenFormatter |
| 22 | + */ |
| 23 | + private $paypalTokenFormatter; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + private $tokenDetails = [ |
| 29 | + 'type' => 'visa', |
| 30 | + 'maskedCC' => '4444************9999', |
| 31 | + 'expirationDate' => '07-07-2025' |
| 32 | + ]; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->paymentTokenMock = $this->getMockBuilder(PaymentTokenInterface::class) |
| 37 | + ->getMockForAbstractClass(); |
| 38 | + |
| 39 | + $this->paypalTokenFormatter = new PaypalTokenFormatter(); |
| 40 | + } |
| 41 | + |
| 42 | + public function testFormatPaymentTokenWithKnownCardType() |
| 43 | + { |
| 44 | + $this->tokenDetails['type'] = key(PaypalTokenFormatter::$baseCardTypes); |
| 45 | + $this->paymentTokenMock->expects($this->once()) |
| 46 | + ->method('getTokenDetails') |
| 47 | + ->willReturn(json_encode($this->tokenDetails)); |
| 48 | + |
| 49 | + $formattedString = sprintf( |
| 50 | + '%s: %s, %s: %s (%s: %s)', |
| 51 | + __('Credit Card'), |
| 52 | + reset(PaypalTokenFormatter::$baseCardTypes), |
| 53 | + __('ending'), |
| 54 | + $this->tokenDetails['maskedCC'], |
| 55 | + __('expires'), |
| 56 | + $this->tokenDetails['expirationDate'] |
| 57 | + ); |
| 58 | + |
| 59 | + self::assertEquals($formattedString, $this->paypalTokenFormatter->formatPaymentToken($this->paymentTokenMock)); |
| 60 | + } |
| 61 | + |
| 62 | + public function testFormatPaymentTokenWithUnknownCardType() |
| 63 | + { |
| 64 | + $this->paymentTokenMock->expects($this->once()) |
| 65 | + ->method('getTokenDetails') |
| 66 | + ->willReturn(json_encode($this->tokenDetails)); |
| 67 | + |
| 68 | + $formattedString = sprintf( |
| 69 | + '%s: %s, %s: %s (%s: %s)', |
| 70 | + __('Credit Card'), |
| 71 | + $this->tokenDetails['type'], |
| 72 | + __('ending'), |
| 73 | + $this->tokenDetails['maskedCC'], |
| 74 | + __('expires'), |
| 75 | + $this->tokenDetails['expirationDate'] |
| 76 | + ); |
| 77 | + |
| 78 | + self::assertEquals($formattedString, $this->paypalTokenFormatter->formatPaymentToken($this->paymentTokenMock)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testFormatPaymentTokenWithWrongData() |
| 82 | + { |
| 83 | + unset($this->tokenDetails['type']); |
| 84 | + |
| 85 | + $this->paymentTokenMock->expects($this->once()) |
| 86 | + ->method('getTokenDetails') |
| 87 | + ->willReturn(json_encode($this->tokenDetails)); |
| 88 | + self::expectException('\InvalidArgumentException'); |
| 89 | + |
| 90 | + $this->paypalTokenFormatter->formatPaymentToken($this->paymentTokenMock); |
| 91 | + } |
| 92 | +} |
0 commit comments