Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RiskDataHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Transaction;
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
14 
20 class RiskDataHandlerTest extends \PHPUnit\Framework\TestCase
21 {
25  private $riskDataHandler;
26 
30  private $subjectReaderMock;
31 
35  protected function setUp()
36  {
37  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
38  ->disableOriginalConstructor()
39  ->setMethods(['readPayment', 'readTransaction'])
40  ->getMock();
41 
42  $this->riskDataHandler = new RiskDataHandler($this->subjectReaderMock);
43  }
44 
52  public function testHandle($riskDecision, $isFraud)
53  {
55  $payment = $this->getMockBuilder(Payment::class)
56  ->disableOriginalConstructor()
57  ->setMethods(['setAdditionalInformation', 'setIsFraudDetected'])
58  ->getMock();
60  $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
61  $paymentDO->expects(self::once())
62  ->method('getPayment')
63  ->willReturn($payment);
64 
65  $transaction = Transaction::factory([
66  'riskData' => [
67  'id' => 'test-id',
68  'decision' => $riskDecision
69  ]
70  ]);
71 
72  $response = [
73  'object' => $transaction
74  ];
75  $handlingSubject = [
76  'payment' => $paymentDO,
77  ];
78 
79  $this->subjectReaderMock->expects(static::once())
80  ->method('readPayment')
81  ->with($handlingSubject)
82  ->willReturn($paymentDO);
83  $this->subjectReaderMock->expects(static::once())
84  ->method('readTransaction')
85  ->with($response)
86  ->willReturn($transaction);
87 
88  $payment->expects(static::at(0))
89  ->method('setAdditionalInformation')
90  ->with(RiskDataHandler::RISK_DATA_ID, 'test-id');
91  $payment->expects(static::at(1))
92  ->method('setAdditionalInformation')
93  ->with(RiskDataHandler::RISK_DATA_DECISION, $riskDecision);
94 
95  if (!$isFraud) {
96  $payment->expects(static::never())
97  ->method('setIsFraudDetected');
98  } else {
99  $payment->expects(static::once())
100  ->method('setIsFraudDetected')
101  ->with(true);
102  }
103 
104  $this->riskDataHandler->handle($handlingSubject, $response);
105  }
106 
111  public function riskDataProvider()
112  {
113  return [
114  ['decision' => 'Not Evaluated', 'isFraud' => false],
115  ['decision' => 'Approve', 'isFraud' => false],
116  ['decision' => 'Review', 'isFraud' => true],
117  ];
118  }
119 }
$transaction
$response
Definition: 404.php:11
$payment
Definition: order.php:17