Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TokenFormatterTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use PHPUnit\Framework\TestCase;
13 use PHPUnit_Framework_MockObject_MockObject;
14 
15 class TokenFormatterTest extends TestCase
16 {
20  private $paymentTokenMock;
21 
25  private $creditCardTokenFormatter;
26 
30  private $tokenDetails = [
31  'type' => 'visa',
32  'maskedCC' => '1111************9999',
33  'expirationDate' => '01-01-2020'
34  ];
35 
41  protected function setUp()
42  {
43  $this->paymentTokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
44  ->getMockForAbstractClass();
45 
46  $this->creditCardTokenFormatter = new CreditCardTokenFormatter();
47  }
48 
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 
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 
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 }
__()
Definition: __.php:13