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\PayPalDetails;
16 use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
17 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
22 use PHPUnit\Framework\TestCase;
23 use PHPUnit_Framework_MockObject_MockObject as MockObject;
24 
30 class VaultDetailsHandlerTest extends TestCase
31 {
32  private static $transactionId = '1n2suy';
33 
34  private static $token = 'rc39al';
35 
36  private static $payerEmail = '[email protected]';
37 
41  private $paymentDataObjectMock;
42 
46  private $paymentInfoMock;
47 
51  private $paymentTokenFactoryMock;
52 
56  protected $paymentTokenMock;
57 
61  private $paymentExtensionMock;
62 
66  private $paymentExtensionFactoryMock;
67 
71  private $handler;
72 
76  private $dateTimeFactoryMock;
77 
81  private $subject = [];
82 
83  protected function setUp()
84  {
85  $objectManager = new ObjectManager($this);
86 
87  $this->paymentDataObjectMock = $this->getMockForAbstractClass(PaymentDataObjectInterface::class);
88 
89  $this->paymentInfoMock = $this->getMockBuilder(Payment::class)
90  ->disableOriginalConstructor()
91  ->setMethods(['__wakeup', 'getExtensionAttributes'])
92  ->getMock();
93 
94  $this->paymentTokenMock = $objectManager->getObject(PaymentToken::class);
95 
96  $this->paymentTokenFactoryMock = $this->getMockBuilder(PaymentTokenFactoryInterface::class)
97  ->setMethods(['create'])
98  ->disableOriginalConstructor()
99  ->getMock();
100 
101  $this->paymentExtensionMock = $this->getMockBuilder(OrderPaymentExtensionInterface::class)
102  ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken'])
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->paymentExtensionFactoryMock = $this->getMockBuilder(OrderPaymentExtensionInterfaceFactory::class)
106  ->disableOriginalConstructor()
107  ->setMethods(['create'])
108  ->getMock();
109 
110  $this->paymentInfoMock->expects(self::any())
111  ->method('getExtensionAttributes')
112  ->willReturn($this->paymentExtensionMock);
113 
114  $this->subject = [
115  'payment' => $this->paymentDataObjectMock,
116  ];
117 
118  $this->dateTimeFactoryMock = $this->getMockBuilder(DateTimeFactory::class)
119  ->disableOriginalConstructor()
120  ->setMethods(['create'])
121  ->getMock();
122 
123  $this->handler = new VaultDetailsHandler(
124  $this->paymentTokenFactoryMock,
125  $this->paymentExtensionFactoryMock,
126  new SubjectReader(),
127  $this->dateTimeFactoryMock
128  );
129  }
130 
131  public function testHandle()
132  {
133  $transaction = $this->getTransaction();
134  $response = [
135  'object' => $transaction
136  ];
137 
138  $this->paymentExtensionMock->method('setVaultPaymentToken')
139  ->with($this->paymentTokenMock);
140  $this->paymentExtensionMock->method('getVaultPaymentToken')
141  ->willReturn($this->paymentTokenMock);
142 
143  $this->paymentDataObjectMock->method('getPayment')
144  ->willReturn($this->paymentInfoMock);
145 
146  $this->paymentTokenFactoryMock->method('create')
148  ->willReturn($this->paymentTokenMock);
149 
150  $this->paymentExtensionFactoryMock->method('create')
151  ->willReturn($this->paymentExtensionMock);
152 
153  $dateTime = new \DateTime('2016-07-05 00:00:00', new \DateTimeZone('UTC'));
154  $expirationDate = '2017-07-05 00:00:00';
155  $this->dateTimeFactoryMock->method('create')
156  ->willReturn($dateTime);
157 
158  $this->handler->handle($this->subject, $response);
159 
160  $extensionAttributes = $this->paymentInfoMock->getExtensionAttributes();
161  $paymentToken = $extensionAttributes->getVaultPaymentToken();
162  self::assertNotNull($paymentToken);
163 
164  $tokenDetails = json_decode($paymentToken->getTokenDetails(), true);
165 
166  self::assertSame($this->paymentTokenMock, $paymentToken);
167  self::assertEquals(self::$token, $paymentToken->getGatewayToken());
168  self::assertEquals(self::$payerEmail, $tokenDetails['payerEmail']);
169  self::assertEquals($expirationDate, $paymentToken->getExpiresAt());
170  }
171 
172  public function testHandleWithoutToken()
173  {
174  $transaction = $this->getTransaction();
175  $transaction->transaction->paypalDetails->token = null;
176 
177  $response = [
178  'object' => $transaction
179  ];
180 
181  $this->paymentDataObjectMock->method('getPayment')
182  ->willReturn($this->paymentInfoMock);
183 
184  $this->paymentTokenFactoryMock->expects(self::never())
185  ->method('create');
186 
187  $this->dateTimeFactoryMock->expects(self::never())
188  ->method('create');
189 
190  $this->handler->handle($this->subject, $response);
191  self::assertNotNull($this->paymentInfoMock->getExtensionAttributes());
192  }
193 
199  private function getTransaction(): Successful
200  {
201  $attributes = [
202  'id' => self::$transactionId,
203  'paypalDetails' => $this->getPayPalDetails()
204  ];
205 
206  $transaction = Transaction::factory($attributes);
207  $result = new Successful(['transaction' => $transaction]);
208 
209  return $result;
210  }
211 
217  private function getPayPalDetails(): PayPalDetails
218  {
219  $attributes = [
220  'token' => self::$token,
221  'payerEmail' => self::$payerEmail
222  ];
223 
224  $details = new PayPalDetails($attributes);
225 
226  return $details;
227  }
228 }
$transaction
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$details
Definition: vault.phtml:10
$extensionAttributes
Definition: payment.php:22
$attributes
Definition: matrix.phtml:13
$payerEmail
$dateTime