Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PayflowproTest.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Framework\HTTP\ZendClientFactory;
15 use Magento\Payment\Model\Method\ConfigInterfaceFactory;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 
29 class PayflowproTest extends \PHPUnit\Framework\TestCase
30 {
34  protected $payflowpro;
35 
39  protected $helper;
40 
44  protected $configMock;
45 
49  protected $storeManagerMock;
50 
54  protected $gatewayMock;
55 
59  protected $scopeConfigMock;
60 
64  private $eventManager;
65 
66  protected function setUp()
67  {
68  $this->configMock = $this->getMockBuilder(PayflowConfig::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
72  ->getMockForAbstractClass();
73  $this->gatewayMock = $this->getMockBuilder(Gateway::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
77  ->getMockForAbstractClass();
78 
79  $configFactoryMock = $this->getMockBuilder(ConfigInterfaceFactory::class)
80  ->setMethods(['create'])
81  ->disableOriginalConstructor()
82  ->getMock();
83  $configFactoryMock->method('create')
84  ->willReturn($this->configMock);
85 
86  $client = $this->getMockBuilder(ZendClient::class)
87  ->getMock();
88 
89  $clientFactory = $this->getMockBuilder(ZendClientFactory::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $clientFactory->method('create')->will($this->returnValue($client));
93 
94  $this->eventManager = $this->getMockBuilder(ManagerInterface::class)
95  ->getMockForAbstractClass();
96 
97  $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
98  $this->payflowpro = $this->helper->getObject(
99  \Magento\Paypal\Model\Payflowpro::class,
100  [
101  'eventDispatcher' => $this->eventManager,
102  'configFactory' => $configFactoryMock,
103  'httpClientFactory' => $clientFactory,
104  'storeManager' => $this->storeManagerMock,
105  'gateway' => $this->gatewayMock,
106  'scopeConfig' => $this->scopeConfigMock
107  ]
108  );
109  }
110 
119  public function testCanVoid($message, $amountPaid, $expected)
120  {
122  $payment = $this->getMockBuilder(Payment::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125  $payment->method('getAmountPaid')->willReturn($amountPaid);
126  $this->payflowpro->setInfoInstance($payment);
127 
128  $this->assertEquals($expected, $this->payflowpro->canVoid(), $message);
129  }
130 
134  public function canVoidDataProvider()
135  {
136  return [
137  ["Can void transaction if order's paid amount not set", null, true],
138  ["Can void transaction if order's paid amount equals zero", 0, true],
139  ["Can't void transaction if order's paid amount greater than zero", 10, false],
140  ];
141  }
142 
143  public function testCanCapturePartial()
144  {
145  $this->assertTrue($this->payflowpro->canCapturePartial());
146  }
147 
149  {
150  $this->assertTrue($this->payflowpro->canRefundPartialPerInvoice());
151  }
152 
157  {
159 
160  $this->gatewayMock->expects($this->once())
161  ->method('postRequest')
162  ->willReturn($response);
163  $this->initStoreMock();
164  $this->configMock->expects($this->once())->method('getBuildNotationCode')
165  ->will($this->returnValue('BNCODE'));
166  $payment = $this->createPartialMock(\Magento\Payment\Model\Info::class, ['setTransactionId', '__wakeup']);
167  $payment->expects($this->once())->method('setTransactionId')->will($this->returnSelf());
168  $this->payflowpro->fetchTransactionInfo($payment, 'AD49G8N825');
169  }
170 
175  public function testSetTransStatus($response, $paymentExpected)
176  {
177  $payment = $this->helper->getObject(\Magento\Payment\Model\Info::class);
178  $this->payflowpro->setTransStatus($payment, $response);
179  $this->assertEquals($paymentExpected->getData(), $payment->getData());
180  }
181 
185  public function setTransStatusDataProvider()
186  {
187  return [
188  [
189  'response' => new \Magento\Framework\DataObject(
190  [
191  'pnref' => 'V19A3D27B61E',
192  'result_code' => Payflowpro::RESPONSE_CODE_APPROVED,
193  ]
194  ),
195  'paymentExpected' => new \Magento\Framework\DataObject(
196  [
197  'transaction_id' => 'V19A3D27B61E',
198  'is_transaction_closed' => 0,
199  ]
200  ),
201  ],
202  [
203  'response' => new \Magento\Framework\DataObject(
204  [
205  'pnref' => 'V19A3D27B61E',
207  ]
208  ),
209  'paymentExpected' => new \Magento\Framework\DataObject(
210  [
211  'transaction_id' => 'V19A3D27B61E',
212  'is_transaction_closed' => 0,
213  'is_transaction_pending' => true,
214  'is_fraud_detected' => true,
215  ]
216  ),
217  ],
218  ];
219  }
220 
227  public function testIsActive(array $expectsMethods, $result)
228  {
229  $storeId = 15;
230 
231  $i = 0;
232  foreach ($expectsMethods as $method => $isActive) {
233  $this->scopeConfigMock->expects($this->at($i++))
234  ->method('getValue')
235  ->with(
236  "payment/{$method}/active",
238  $storeId
239  )->willReturn($isActive);
240  }
241 
242  $this->assertEquals($result, $this->payflowpro->isActive($storeId));
243  }
244 
249  {
250  $paymentMock = $this->getPaymentMock();
251  $orderMock = $this->getOrderMock();
252 
253  // test case to build basic request
254  $paymentMock->expects(static::once())
255  ->method('getAdditionalInformation')
256  ->with('pnref')
257  ->willReturn(false);
258  $paymentMock->expects(static::once())
259  ->method('getParentTransactionId')
260  ->willReturn(false);
261 
262  $paymentMock->expects(static::exactly(2))
263  ->method('getOrder')
264  ->willReturn($orderMock);
265 
266  $response = $this->execGatewayRequest();
267  $amount = 23.03;
268  $this->payflowpro->capture($paymentMock, $amount);
269  static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
270  static::assertFalse((bool)$paymentMock->getIsTransactionPending());
271  }
272 
277  {
278  return [
279  [
280  'amount' => 14.13999999999999999999999999999999999999999999999999,
281  'setAmount' => 49.99,
282  'expectedResult' => 14.14
283  ],
284  [
285  'amount' => 14.13199999999999999999999999999999999999999999999999,
286  'setAmount' => 49.99,
287  'expectedResult' => 14.13,
288  ],
289  [
290  'amount' => 14.14,
291  'setAmount' => 49.99,
292  'expectedResult' => 14.14,
293  ],
294  [
295  'amount' => 14.13999999999999999999999999999999999999999999999999,
296  'setAmount' => 14.14,
297  'expectedResult' => 0,
298  ]
299  ];
300  }
301 
308  public function testCaptureAmountRounding($amount, $setAmount, $expectedResult)
309  {
310  $paymentMock = $this->getPaymentMock();
311  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
312  ->disableOriginalConstructor()
313  ->getMock();
314 
315  $infoInstanceMock = $this->getMockForAbstractClass(
316  InfoInterface::class,
317  [],
318  '',
319  false,
320  false,
321  false,
322  ['getAmountAuthorized','hasAmountPaid']
323  );
324 
325  $infoInstanceMock->expects($this->once())
326  ->method('getAmountAuthorized')
327  ->willReturn($setAmount);
328  $infoInstanceMock->expects($this->once())
329  ->method('hasAmountPaid')
330  ->willReturn(true);
331  $this->payflowpro->setData('info_instance', $infoInstanceMock);
332 
333  // test case to build basic request
334  $paymentMock->expects($this->once())
335  ->method('getAdditionalInformation')
336  ->with('pnref')
337  ->willReturn(false);
338  $paymentMock->expects($this->any())
339  ->method('getParentTransactionId')
340  ->willReturn(true);
341 
342  $paymentMock->expects($this->once())
343  ->method('getOrder')
344  ->willReturn($orderMock);
345 
346  $this->initStoreMock();
348  $this->gatewayMock->expects($this->once())
349  ->method('postRequest')
350  ->with(
351  $this->callback(function ($request) use ($expectedResult) {
352  return is_callable([$request, 'getAmt']) && $request->getAmt() == $expectedResult;
353  }),
354  $this->isInstanceOf(PayflowConfig::class)
355  )
356  ->willReturn($response);
357 
358  $this->payflowpro->capture($paymentMock, $amount);
359 
360  $this->assertEquals($response['pnref'], $paymentMock->getTransactionId());
361  $this->assertFalse((bool)$paymentMock->getIsTransactionPending());
362  }
363 
367  public function testAuthorize()
368  {
369  $paymentMock = $this->getPaymentMock();
370  $orderMock = $this->getOrderMock();
371 
372  $paymentMock->expects(static::exactly(2))
373  ->method('getOrder')
374  ->willReturn($orderMock);
375 
376  $response = $this->execGatewayRequest();
377  $amount = 43.20;
378  $this->payflowpro->authorize($paymentMock, $amount);
379  static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
380  static::assertFalse((bool)$paymentMock->getIsTransactionPending());
381  }
382 
386  public function dataProviderForTestIsActive()
387  {
388  return [
389  [
390  'expectsMethods' => [
393  ],
394  'result' => true,
395  ],
396  [
397  'expectsMethods' => [
399  ],
400  'result' => true,
401  ],
402  [
403  'expectsMethods' => [
406  ],
407  'result' => false,
408  ],
409  ];
410  }
411 
415  public function testRefund()
416  {
418  $paymentMock = $this->getPaymentMock();
419 
420  $response = $this->execGatewayRequest();
421 
422  $amount = 213.04;
423  $this->payflowpro->refund($paymentMock, $amount);
424  static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
425  static::assertTrue($paymentMock->getIsTransactionClosed());
426  }
427 
432  protected function initStoreMock()
433  {
434  $storeId = 27;
435  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
436  ->disableOriginalConstructor()
437  ->setMethods(['getId'])
438  ->getMock();
439  $this->storeManagerMock->expects(static::once())
440  ->method('getStore')
441  ->willReturn($storeMock);
442  $storeMock->expects(static::once())
443  ->method('getId')
444  ->willReturn($storeId);
445  }
446 
451  protected function getGatewayResponseObject()
452  {
453  return new \Magento\Framework\DataObject(
454  [
455  'result' => '0',
456  'pnref' => 'V19A3D27B61E',
457  'respmsg' => 'Approved',
458  'authcode' => '510PNI',
459  'hostcode' => 'A',
460  'request_id' => 'f930d3dc6824c1f7230c5529dc37ae5e',
461  'result_code' => '0',
462  ]
463  );
464  }
465 
470  protected function execGatewayRequest()
471  {
472  $this->initStoreMock();
474  $this->gatewayMock->expects(static::once())
475  ->method('postRequest')
476  ->with(
477  $this->isInstanceOf(\Magento\Framework\DataObject::class),
478  $this->isInstanceOf(PayflowConfig::class)
479  )
480  ->willReturn($response);
481  return $response;
482  }
483 
488  protected function getPaymentMock()
489  {
490  $paymentMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
491  ->disableOriginalConstructor()
492  ->setMethods([
493  'getAdditionalInformation', 'getParentTransactionId', 'getOrder',
494  'getCcNumber', 'getCcExpMonth', 'getCcExpYear', 'getCcCid'
495  ])
496  ->getMock();
497 
498  $cardData = [
499  'number' => 4111111111111111,
500  'month' => 12,
501  'year' => 18,
502  'cvv' => 123
503  ];
504  $paymentMock->expects(static::any())
505  ->method('getCcNumber')
506  ->willReturn($cardData['number']);
507  $paymentMock->expects(static::any())
508  ->method('getCcExpMonth')
509  ->willReturn($cardData['month']);
510  $paymentMock->expects(static::any())
511  ->method('getCcExpYear')
512  ->willReturn($cardData['year']);
513  $paymentMock->expects(static::any())
514  ->method('getCcCid')
515  ->willReturn($cardData['cvv']);
516  return $paymentMock;
517  }
518 
523  protected function getOrderMock()
524  {
525  $orderData = [
526  'currency' => 'USD',
527  'id' => 4,
528  'increment_id' => '0000004'
529  ];
530  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
531  ->disableOriginalConstructor()
532  ->setMethods(['getBaseCurrencyCode', 'getIncrementId', 'getId', 'getBillingAddress', 'getShippingAddress'])
533  ->getMock();
534 
535  $orderMock->expects(static::once())
536  ->method('getId')
537  ->willReturn($orderData['id']);
538  $orderMock->expects(static::once())
539  ->method('getBaseCurrencyCode')
540  ->willReturn($orderData['currency']);
541  $orderMock->expects(static::atLeastOnce())
542  ->method('getIncrementId')
543  ->willReturn($orderData['increment_id']);
544  return $orderMock;
545  }
546 
547  public function testPostRequest()
548  {
549  $expectedResult = new DataObject();
550 
551  $request = new DataObject();
552 
554  $config = $this->createMock(ConfigInterface::class);
555 
556  $this->gatewayMock->expects(static::once())
557  ->method('postRequest')
558  ->with($request, $config)
559  ->willReturn($expectedResult);
560 
561  static::assertSame($expectedResult, $this->payflowpro->postRequest($request, $config));
562  }
563 
568  public function testPostRequestException()
569  {
570  $request = new DataObject();
571 
573  $config = $this->createMock(ConfigInterface::class);
574 
575  $this->gatewayMock->expects(static::once())
576  ->method('postRequest')
577  ->with($request, $config)
578  ->willThrowException(new \Zend_Http_Client_Exception());
579 
580  $this->payflowpro->postRequest($request, $config);
581  }
582 
586  public function testAssignData()
587  {
588  $data = [
589  'cc_type' => 'VI',
590  'cc_last_4' => 1111,
591  'cc_exp_month' => 12,
592  'cc_exp_year' => 2023
593  ];
594  $dataObject = new DataObject($data);
595 
596  $infoInstance = $this->getMockForAbstractClass(InfoInterface::class);
597  $this->payflowpro->setData('info_instance', $infoInstance);
598 
599  $this->eventManager->expects(static::exactly(2))
600  ->method('dispatch');
601 
602  $this->payflowpro->assignData($dataObject);
603  }
604 
612  public function testMapGatewayResponse($postData, $expectedResponse)
613  {
614  self::assertEquals(
615  $this->payflowpro->mapGatewayResponse($postData, new DataObject()),
616  $expectedResponse
617  );
618  }
619 
624  {
625  return [
626  [
627  [
628  'BILLTONAME' => 'John Doe',
629  'BILLTOFIRSTNAME' => 'John',
630  'BILLTOLASTNAME' => 'Doe',
631  'BILLTOEMAIL' => '[email protected]',
632  'BILLTOSTREET' => '6161 West Centinela Avenue',
633  'BILLTOCITY' => 'Culver City',
634  'BILLTOSTATE' => 'CA',
635  'BILLTOZIP' => '90230',
636  'BILLTOCOUNTRY' => 'US',
637  'SHIPTOSTREET' => '6161 West Centinela Avenue',
638  'SHIPTOCITY' => 'Culver City',
639  'SHIPTOSTATE' => 'CA',
640  'SHIPTOZIP' => '90230',
641  'SHIPTOCOUNTRY' => 'US',
642  'NAMETOSHIP' => 'John Doe',
643  'ADDRESSTOSHIP' => '6161 West Centinela Avenue',
644  'CITYTOSHIP' => 'Culver City',
645  'STATETOSHIP' => 'CA',
646  'ZIPTOSHIP' => '90230',
647  'COUNTRYTOSHIP' => 'US',
648  'NAME' => 'John Doe',
649  'CVV2MATCH' => 'Y',
650  'CARDTYPE' => '0',
651  'AVSDATA' => 'NNN',
652  'AVSZIP' => 'N',
653  'AVSADDR' => 'N',
654  ],
655  new DataObject([
656  'billtoname' => 'John Doe',
657  'billtofirstname' => 'John',
658  'billtolastname' => 'Doe',
659  'billtoemail' => '[email protected]',
660  'billtostreet' => '6161 West Centinela Avenue',
661  'billtocity' => 'Culver City',
662  'billtostate' => 'CA',
663  'billtozip' => '90230',
664  'billtocountry' => 'US',
665  'shiptostreet' => '6161 West Centinela Avenue',
666  'shiptocity' => 'Culver City',
667  'shiptostate' => 'CA',
668  'shiptozip' => '90230',
669  'shiptocountry' => 'US',
670  'nametoship' => 'John Doe',
671  'addresstoship' => '6161 West Centinela Avenue',
672  'citytoship' => 'Culver City',
673  'statetoship' => 'CA',
674  'ziptoship' => '90230',
675  'countrytoship' => 'US',
676  'name' => 'John Doe',
677  'cvv2match' => 'Y',
678  'cardtype' => '0',
679  'avsdata' => 'NN',
680  'avszip' => 'N',
681  'avsaddr' => 'N',
682  'firstname' => 'John',
683  'lastname' => 'Doe',
684  'address' => '6161 West Centinela Avenue',
685  'city' => 'Culver City',
686  'state' => 'CA',
687  'zip' => '90230',
688  'country' => 'US',
689  'email' => '[email protected]',
690  'cscmatch' => 'Y',
691  'ccavsstatus' => 'NNN',
692  'cc_type' => 'VI',
693  ]),
694  ]
695  ];
696  }
697 }
$response
Definition: 404.php:11
$config
Definition: fraud_order.php:17
testCaptureAmountRounding($amount, $setAmount, $expectedResult)
$message
testSetTransStatus($response, $paymentExpected)
$amount
Definition: order.php:14
$payment
Definition: order.php:17
$method
Definition: info.phtml:13
testIsActive(array $expectsMethods, $result)
testMapGatewayResponse($postData, $expectedResponse)
$i
Definition: gallery.phtml:31