Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VaultTest.php
Go to the documentation of this file.
1 <?php
7 
17 use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 
30 class VaultTest extends \PHPUnit\Framework\TestCase
31 {
35  private $objectManager;
36 
40  private $vaultProvider;
41 
42  public function setUp()
43  {
44  $this->objectManager = new ObjectManager($this);
45  $this->vaultProvider = $this->createMock(MethodInterface::class);
46  }
47 
52  public function testAuthorizeNotOrderPayment()
53  {
54  $paymentModel = $this->createMock(InfoInterface::class);
55 
57  $model = $this->objectManager->getObject(Vault::class);
58  $model->authorize($paymentModel, 0);
59  }
60 
67  public function testAuthorizeNoTokenMetadata(array $additionalInfo)
68  {
69  $paymentModel = $this->getMockBuilder(Payment::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $paymentModel->expects(static::once())
74  ->method('getAdditionalInformation')
75  ->willReturn($additionalInfo);
76 
78  $model = $this->objectManager->getObject(Vault::class);
79  $model->authorize($paymentModel, 0);
80  }
81 
86  public function additionalInfoDataProvider()
87  {
88  return [
89  ['additionalInfo' => []],
90  ['additionalInfo' => ['customer_id' => 1]],
91  ['additionalInfo' => ['public_hash' => null]],
92  ];
93  }
94 
99  public function testAuthorizeNoToken()
100  {
101  $customerId = 1;
102  $publicHash = 'token_public_hash';
103 
104  $paymentModel = $this->getMockBuilder(Payment::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107  $tokenManagement = $this->createMock(PaymentTokenManagementInterface::class);
108 
109  $paymentModel->expects(static::once())
110  ->method('getAdditionalInformation')
111  ->willReturn(
112  [
115  ]
116  );
117  $tokenManagement->expects(static::once())
118  ->method('getByPublicHash')
119  ->with($publicHash, $customerId)
120  ->willReturn(null);
121 
123  $model = $this->objectManager->getObject(
124  Vault::class,
125  [
126  'tokenManagement' => $tokenManagement
127  ]
128  );
129  $model->authorize($paymentModel, 0);
130  }
131 
132  public function testAuthorize()
133  {
134  $customerId = 1;
135  $publicHash = 'token_public_hash';
136  $vaultProviderCode = 'vault_provider_code';
137  $amount = 10.01;
138 
139  $paymentModel = $this->getMockBuilder(Payment::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $extensionAttributes = $this->getMockBuilder(OrderPaymentExtensionInterface::class)
143  ->setMethods(['setVaultPaymentToken'])
144  ->getMockForAbstractClass();
145 
146  $commandManagerPool = $this->createMock(CommandManagerPoolInterface::class);
147  $commandManager = $this->createMock(CommandManagerInterface::class);
148 
149  $tokenManagement = $this->createMock(PaymentTokenManagementInterface::class);
150  $token = $this->createMock(PaymentTokenInterface::class);
151 
152  $paymentModel->expects(static::once())
153  ->method('getAdditionalInformation')
154  ->willReturn(
155  [
158  ]
159  );
160  $tokenManagement->expects(static::once())
161  ->method('getByPublicHash')
162  ->with($publicHash, $customerId)
163  ->willReturn($token);
164  $paymentModel->expects(static::once())
165  ->method('getExtensionAttributes')
166  ->willReturn($extensionAttributes);
167  $extensionAttributes->expects(static::once())
168  ->method('setVaultPaymentToken')
169  ->with($token);
170 
171  $this->vaultProvider->expects(static::atLeastOnce())
172  ->method('getCode')
173  ->willReturn($vaultProviderCode);
174  $commandManagerPool->expects(static::once())
175  ->method('get')
176  ->with($vaultProviderCode)
177  ->willReturn($commandManager);
178  $commandManager->expects(static::once())
179  ->method('executeByCode')
180  ->with(
182  $paymentModel,
183  [
184  'amount' => $amount
185  ]
186  );
187 
188  $paymentModel->expects(static::once())
189  ->method('setMethod')
190  ->with($vaultProviderCode);
191 
193  $model = $this->objectManager->getObject(
194  Vault::class,
195  [
196  'tokenManagement' => $tokenManagement,
197  'commandManagerPool' => $commandManagerPool,
198  'vaultProvider' => $this->vaultProvider
199  ]
200  );
201  $model->authorize($paymentModel, $amount);
202  }
203 
208  public function testCaptureNotOrderPayment()
209  {
210  $paymentModel = $this->createMock(InfoInterface::class);
211 
213  $model = $this->objectManager->getObject(Vault::class);
214  $model->capture($paymentModel, 0);
215  }
216 
221  public function testCapture()
222  {
223  $paymentModel = $this->getMockBuilder(Payment::class)
224  ->disableOriginalConstructor()
225  ->getMock();
226 
227  $authorizationTransaction = $this->createMock(TransactionInterface::class);
228  $paymentModel->expects(static::once())
229  ->method('getAuthorizationTransaction')
230  ->willReturn($authorizationTransaction);
231 
233  $model = $this->objectManager->getObject(Vault::class);
234  $model->capture($paymentModel, 0);
235  }
236 
241  public function testIsAvailable($isAvailableProvider, $isActive, $expected)
242  {
243  $storeId = 1;
244  $quote = $this->getMockForAbstractClass(CartInterface::class);
245  $config = $this->getMockForAbstractClass(ConfigInterface::class);
246 
247  $this->vaultProvider->expects(static::once())
248  ->method('isAvailable')
249  ->with($quote)
250  ->willReturn($isAvailableProvider);
251 
252  $config->expects(static::any())
253  ->method('getValue')
254  ->with('active', $storeId)
255  ->willReturn($isActive);
256 
257  $quote->expects(static::any())
258  ->method('getStoreId')
259  ->willReturn($storeId);
260 
262  $model = $this->objectManager->getObject(Vault::class, [
263  'config' => $config,
264  'vaultProvider' => $this->vaultProvider
265  ]);
266  $actual = $model->isAvailable($quote);
267  static::assertEquals($expected, $actual);
268  }
269 
274  public function isAvailableDataProvider()
275  {
276  return [
277  ['isAvailableProvider' => true, 'isActiveVault' => false, 'expected' => false],
278  ['isAvailableProvider' => false, 'isActiveVault' => false, 'expected' => false],
279  ['isAvailableProvider' => false, 'isActiveVault' => true, 'expected' => false],
280  ['isAvailableProvider' => true, 'isActiveVault' => true, 'expected' => true],
281  ];
282  }
283 
287  public function testIsAvailableWithoutQuote()
288  {
289  $quote = null;
290  $config = $this->getMockForAbstractClass(ConfigInterface::class);
291 
292  $this->vaultProvider->expects(static::once())
293  ->method('isAvailable')
294  ->with($quote)
295  ->willReturn(true);
296 
297  $config->expects(static::once())
298  ->method('getValue')
299  ->with('active', $quote)
300  ->willReturn(false);
301 
303  $model = $this->objectManager->getObject(Vault::class, [
304  'config' => $config,
305  'vaultProvider' => $this->vaultProvider
306  ]);
307  static::assertFalse($model->isAvailable($quote));
308  }
309 
317  public function testCanUseInternal($configValue, $paymentValue, $expected)
318  {
319  $handlerPool = $this->createMock(ValueHandlerPoolInterface::class);
320  $handler = $this->createMock(ValueHandlerInterface::class);
321 
322  $handlerPool->expects(static::once())
323  ->method('get')
324  ->with('can_use_internal')
325  ->willReturn($handler);
326 
327  $handler->expects(static::once())
328  ->method('handle')
329  ->with(['field' => 'can_use_internal'], null)
330  ->willReturn($configValue);
331 
332  $this->vaultProvider->expects(static::any())
333  ->method('canUseInternal')
334  ->willReturn($paymentValue);
335 
337  $model = $this->objectManager->getObject(Vault::class, [
338  'vaultProvider' => $this->vaultProvider,
339  'valueHandlerPool' => $handlerPool,
340  ]);
341  static::assertEquals($expected, $model->canUseInternal());
342  }
343 
348  public function internalUsageDataProvider()
349  {
350  return [
351  ['configValue' => true, 'paymentValue' => true, 'expected' => true],
352  ['configValue' => true, 'paymentValue' => null, 'expected' => true],
353  ['configValue' => true, 'paymentValue' => false, 'expected' => true],
354  ['configValue' => false, 'paymentValue' => true, 'expected' => false],
355  ['configValue' => false, 'paymentValue' => false, 'expected' => false],
356  ['configValue' => null, 'paymentValue' => true, 'expected' => true],
357  ['configValue' => null, 'paymentValue' => false, 'expected' => false],
358  ['configValue' => null, 'paymentValue' => null, 'expected' => false],
359  ];
360  }
361 }
$config
Definition: fraud_order.php:17
$quote
$amount
Definition: order.php:14
$extensionAttributes
Definition: payment.php:22
catch(\Exception $e) $handler
Definition: index.php:30