Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionRefundTest.php
Go to the documentation of this file.
1 <?php
7 
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
14 use Psr\Log\LoggerInterface;
16 
20 class TransactionRefundTest extends \PHPUnit\Framework\TestCase
21 {
25  private $client;
26 
30  private $loggerMock;
31 
35  private $adapterMock;
36 
40  private $transactionId = 'px4kpev5';
41 
45  private $paymentAmount = '100.00';
46 
50  protected function setUp()
51  {
53  $criticalLoggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
54  $this->loggerMock = $this->getMockBuilder(Logger::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
58  ->disableOriginalConstructor()
59  ->getMock();
61  $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64  $adapterFactoryMock->expects(self::once())
65  ->method('create')
66  ->willReturn($this->adapterMock);
67 
68  $this->client = new TransactionRefund($criticalLoggerMock, $this->loggerMock, $adapterFactoryMock);
69  }
70 
77  public function testPlaceRequestException()
78  {
79  $this->loggerMock->expects($this->once())
80  ->method('debug')
81  ->with(
82  [
83  'request' => $this->getTransferData(),
84  'client' => TransactionRefund::class,
85  'response' => [],
86  ]
87  );
88 
89  $this->adapterMock->expects($this->once())
90  ->method('refund')
91  ->with($this->transactionId, $this->paymentAmount)
92  ->willThrowException(new \Exception('Test messages'));
93 
95  $transferObjectMock = $this->getTransferObjectMock();
96 
97  $this->client->placeRequest($transferObjectMock);
98  }
99 
103  public function testPlaceRequestSuccess()
104  {
105  $response = new \stdClass;
106  $response->success = true;
107  $this->adapterMock->expects($this->once())
108  ->method('refund')
109  ->with($this->transactionId, $this->paymentAmount)
110  ->willReturn($response);
111 
112  $this->loggerMock->expects($this->once())
113  ->method('debug')
114  ->with(
115  [
116  'request' => $this->getTransferData(),
117  'client' => TransactionRefund::class,
118  'response' => ['success' => 1],
119  ]
120  );
121 
122  $actualResult = $this->client->placeRequest($this->getTransferObjectMock());
123 
124  $this->assertTrue(is_object($actualResult['object']));
125  $this->assertEquals(['object' => $response], $actualResult);
126  }
127 
133  private function getTransferObjectMock()
134  {
135  $transferObjectMock = $this->createMock(TransferInterface::class);
136  $transferObjectMock->expects($this->once())
137  ->method('getBody')
138  ->willReturn($this->getTransferData());
139 
140  return $transferObjectMock;
141  }
142 
148  private function getTransferData()
149  {
150  return [
151  'transaction_id' => $this->transactionId,
152  PaymentDataBuilder::AMOUNT => $this->paymentAmount,
153  ];
154  }
155 }
$response
Definition: 404.php:11