Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VaultDetailsHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Result\Successful;
9 use Braintree\Transaction;
10 use Braintree\Transaction\CreditCardDetails;
18 use Magento\Sales\Api\Data\OrderPaymentExtension;
19 use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
20 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
24 use PHPUnit\Framework\TestCase;
25 use PHPUnit_Framework_MockObject_MockObject as MockObject;
26 
32 class VaultDetailsHandlerTest extends TestCase
33 {
34  private static $transactionId = '432erwwe';
35 
36  private static $token = 'rh3gd4';
37 
41  private $paymentHandler;
42 
46  private $payment;
47 
51  private $paymentTokenFactory;
52 
56  private $paymentExtension;
57 
61  private $paymentExtensionFactory;
62 
63  protected function setUp()
64  {
65  $objectManager = new ObjectManager($this);
66  $paymentToken = $objectManager->getObject(PaymentToken::class);
67  $this->paymentTokenFactory = $this->getMockBuilder(PaymentTokenFactoryInterface::class)
68  ->setMethods(['create'])
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->paymentTokenFactory->method('create')
73  ->willReturn($paymentToken);
74 
75  $this->initPaymentExtensionAttributesMock();
76  $this->paymentExtension->method('setVaultPaymentToken')
77  ->with($paymentToken);
78  $this->paymentExtension->method('getVaultPaymentToken')
79  ->willReturn($paymentToken);
80 
81  $this->payment = $this->getMockBuilder(Payment::class)
82  ->disableOriginalConstructor()
83  ->setMethods(['__wakeup', 'getExtensionAttributes'])
84  ->getMock();
85 
86  $this->payment->expects(self::any())->method('getExtensionAttributes')->willReturn($this->paymentExtension);
87 
88  $config = $this->getConfigMock();
89 
90  $this->paymentHandler = new VaultDetailsHandler(
91  $this->paymentTokenFactory,
92  $this->paymentExtensionFactory,
93  $config,
94  new SubjectReader(),
95  new Json()
96  );
97  }
98 
99  public function testHandle()
100  {
101  $paymentData = $this->getPaymentDataObjectMock();
102 
103  $subject = ['payment' => $paymentData];
104  $response = ['object' => $this->getBraintreeTransaction()];
105 
106  $this->paymentHandler->handle($subject, $response);
107  $paymentToken = $this->payment->getExtensionAttributes()
108  ->getVaultPaymentToken();
109 
110  self::assertEquals(self::$token, $paymentToken->getGatewayToken());
111  self::assertEquals('2022-01-01 00:00:00', $paymentToken->getExpiresAt());
112 
113  $details = json_decode($paymentToken->getTokenDetails(), true);
114  self::assertEquals(
115  [
116  'type' => 'AE',
117  'maskedCC' => 1231,
118  'expirationDate' => '12/2021'
119  ],
120  $details
121  );
122  }
123 
129  private function getPaymentDataObjectMock(): PaymentDataObject
130  {
131  $mock = $this->getMockBuilder(PaymentDataObject::class)
132  ->setMethods(['getPayment'])
133  ->disableOriginalConstructor()
134  ->getMock();
135 
136  $mock->method('getPayment')
137  ->willReturn($this->payment);
138 
139  return $mock;
140  }
141 
147  private function getBraintreeTransaction()
148  {
149  $attributes = [
150  'id' => self::$transactionId,
151  'creditCardDetails' => $this->getCreditCardDetails()
152  ];
153 
154  $transaction = Transaction::factory($attributes);
155  $result = new Successful(['transaction' => $transaction]);
156 
157  return $result;
158  }
159 
165  private function getCreditCardDetails(): CreditCardDetails
166  {
167  $attributes = [
168  'token' => self::$token,
169  'bin' => '5421',
170  'cardType' => 'American Express',
171  'expirationMonth' => 12,
172  'expirationYear' => 2021,
173  'last4' => 1231
174  ];
175 
176  $creditCardDetails = new CreditCardDetails($attributes);
177 
178  return $creditCardDetails;
179  }
180 
186  private function getConfigMock(): Config
187  {
188  $mapperArray = [
189  'american-express' => 'AE',
190  'discover' => 'DI',
191  'jcb' => 'JCB',
192  'mastercard' => 'MC',
193  'master-card' => 'MC',
194  'visa' => 'VI',
195  'maestro' => 'MI',
196  'diners-club' => 'DN',
197  'unionpay' => 'CUP'
198  ];
199 
200  $config = $this->getMockBuilder(Config::class)
201  ->setMethods(['getCctypesMapper'])
202  ->disableOriginalConstructor()
203  ->getMock();
204 
205  $config->method('getCctypesMapper')
206  ->willReturn($mapperArray);
207 
208  return $config;
209  }
210 
216  private function initPaymentExtensionAttributesMock()
217  {
218  $this->paymentExtension = $this->getMockBuilder(OrderPaymentExtensionInterface::class)
219  ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken'])
220  ->disableOriginalConstructor()
221  ->getMock();
222 
223  $this->paymentExtensionFactory = $this->getMockBuilder(OrderPaymentExtensionInterfaceFactory::class)
224  ->disableOriginalConstructor()
225  ->setMethods(['create'])
226  ->getMock();
227  $this->paymentExtensionFactory->method('create')
228  ->willReturn($this->paymentExtension);
229  }
230 }
$transaction
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
$details
Definition: vault.phtml:10
$attributes
Definition: matrix.phtml:13