Skip to content

Commit 3823134

Browse files
eduard13jignesh-baldha
authored andcommitted
Covering Magento\Braintree\Model\InstantPurchase\CreditCard\TokenFormatter by UnitTest
1 parent 05f9df7 commit 3823134

File tree

1 file changed

+122
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)