Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentDetailsHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Transaction;
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
14 
18 class PaymentDetailsHandlerTest extends \PHPUnit\Framework\TestCase
19 {
20  const TRANSACTION_ID = '432erwwe';
21 
25  private $paymentHandler;
26 
30  private $paymentMock;
31 
35  private $subjectReaderMock;
36 
37  protected function setUp()
38  {
39  $this->paymentMock = $this->getMockBuilder(Payment::class)
40  ->disableOriginalConstructor()
41  ->setMethods([
42  'setCcTransId',
43  'setLastTransId',
44  'setAdditionalInformation',
45  ])
46  ->getMock();
47  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->paymentMock->expects(static::once())
52  ->method('setCcTransId');
53  $this->paymentMock->expects(static::once())
54  ->method('setLastTransId');
55  $this->paymentMock->expects(static::any())
56  ->method('setAdditionalInformation');
57 
58  $this->paymentHandler = new PaymentDetailsHandler($this->subjectReaderMock);
59  }
60 
64  public function testHandle()
65  {
66  $paymentDataMock = $this->getPaymentDataObjectMock();
67  $transaction = $this->getBraintreeTransaction();
68 
69  $subject = ['payment' => $paymentDataMock];
70  $response = ['object' => $transaction];
71 
72  $this->subjectReaderMock->expects(self::once())
73  ->method('readPayment')
74  ->with($subject)
75  ->willReturn($paymentDataMock);
76  $this->subjectReaderMock->expects(self::once())
77  ->method('readTransaction')
78  ->with($response)
79  ->willReturn($transaction);
80 
81  $this->paymentHandler->handle($subject, $response);
82  }
83 
88  private function getPaymentDataObjectMock()
89  {
90  $mock = $this->getMockBuilder(PaymentDataObject::class)
91  ->setMethods(['getPayment'])
92  ->disableOriginalConstructor()
93  ->getMock();
94 
95  $mock->expects(static::once())
96  ->method('getPayment')
97  ->willReturn($this->paymentMock);
98 
99  return $mock;
100  }
101 
106  private function getBraintreeTransaction()
107  {
108  $attributes = [
109  'id' => self::TRANSACTION_ID,
110  'avsPostalCodeResponseCode' => 'M',
111  'avsStreetAddressResponseCode' => 'M',
112  'cvvResponseCode' => 'M',
113  'processorAuthorizationCode' => 'W1V8XK',
114  'processorResponseCode' => '1000',
115  'processorResponseText' => 'Approved',
116  ];
117 
118  return Transaction::factory($attributes);
119  }
120 }
$transaction
$response
Definition: 404.php:11
$attributes
Definition: matrix.phtml:13