|
| 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\CreditCard; |
| 9 | + |
| 10 | +use Magento\Braintree\Model\InstantPurchase\CreditCard\TokenFormatter as CreditCardTokenFormatter; |
| 11 | +use Magento\Vault\Api\Data\PaymentTokenInterface; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use PHPUnit_Framework_MockObject_MockObject; |
| 14 | + |
| 15 | +class TokenFormatterTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var PaymentTokenInterface|PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + private $paymentTokenMock; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var CreditCardTokenFormatter |
| 24 | + */ |
| 25 | + private $creditCardTokenFormatter; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + private $tokenDetails = [ |
| 31 | + 'type' => 'visa', |
| 32 | + 'maskedCC' => '1111************9999', |
| 33 | + 'expirationDate' => '01-01-2020' |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * Set Up |
| 38 | + * |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + protected function setUp() |
| 42 | + { |
| 43 | + $this->paymentTokenMock = $this->getMockBuilder(PaymentTokenInterface::class) |
| 44 | + ->getMockForAbstractClass(); |
| 45 | + |
| 46 | + $this->creditCardTokenFormatter = new CreditCardTokenFormatter(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Testing the payment format with a known credit card type |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function testFormatPaymentTokenWithKnownCardType() |
| 55 | + { |
| 56 | + $this->tokenDetails['type'] = key(CreditCardTokenFormatter::$baseCardTypes); |
| 57 | + $this->paymentTokenMock->expects($this->once()) |
| 58 | + ->method('getTokenDetails') |
| 59 | + ->willReturn(json_encode($this->tokenDetails)); |
| 60 | + |
| 61 | + $formattedString = sprintf( |
| 62 | + '%s: %s, %s: %s (%s: %s)', |
| 63 | + __('Credit Card'), |
| 64 | + reset(CreditCardTokenFormatter::$baseCardTypes), |
| 65 | + __('ending'), |
| 66 | + $this->tokenDetails['maskedCC'], |
| 67 | + __('expires'), |
| 68 | + $this->tokenDetails['expirationDate'] |
| 69 | + ); |
| 70 | + |
| 71 | + self::assertEquals( |
| 72 | + $formattedString, |
| 73 | + $this->creditCardTokenFormatter->formatPaymentToken($this->paymentTokenMock) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Testing the payment format with a unknown credit card type |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function testFormatPaymentTokenWithUnknownCardType() |
| 83 | + { |
| 84 | + $this->paymentTokenMock->expects($this->once()) |
| 85 | + ->method('getTokenDetails') |
| 86 | + ->willReturn(json_encode($this->tokenDetails)); |
| 87 | + |
| 88 | + $formattedString = sprintf( |
| 89 | + '%s: %s, %s: %s (%s: %s)', |
| 90 | + __('Credit Card'), |
| 91 | + $this->tokenDetails['type'], |
| 92 | + __('ending'), |
| 93 | + $this->tokenDetails['maskedCC'], |
| 94 | + __('expires'), |
| 95 | + $this->tokenDetails['expirationDate'] |
| 96 | + ); |
| 97 | + |
| 98 | + self::assertEquals( |
| 99 | + $formattedString, |
| 100 | + $this->creditCardTokenFormatter->formatPaymentToken($this->paymentTokenMock) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Testing the payment format with wrong card data |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function testFormatPaymentTokenWithWrongData() |
| 110 | + { |
| 111 | + unset($this->tokenDetails['type']); |
| 112 | + $this->paymentTokenMock->expects($this->once()) |
| 113 | + ->method('getTokenDetails') |
| 114 | + ->willReturn(json_encode($this->tokenDetails)); |
| 115 | + self::expectException('\InvalidArgumentException'); |
| 116 | + |
| 117 | + $this->creditCardTokenFormatter->formatPaymentToken($this->paymentTokenMock); |
| 118 | + } |
| 119 | +} |
0 commit comments