Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionVoidTest.php
Go to the documentation of this file.
1 <?php
7 
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
14 use Psr\Log\LoggerInterface;
15 
19 class TransactionVoidTest extends \PHPUnit\Framework\TestCase
20 {
24  private $client;
25 
29  private $loggerMock;
30 
34  private $adapterMock;
35 
39  private $transactionId = 'px4kpev5';
40 
44  protected function setUp()
45  {
47  $criticalLoggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
48  $this->loggerMock = $this->getMockBuilder(Logger::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
52  ->disableOriginalConstructor()
53  ->getMock();
55  $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58  $adapterFactoryMock->expects(self::once())
59  ->method('create')
60  ->willReturn($this->adapterMock);
61 
62  $this->client = new TransactionVoid($criticalLoggerMock, $this->loggerMock, $adapterFactoryMock);
63  }
64 
71  public function testPlaceRequestException()
72  {
73  $this->loggerMock->expects($this->once())
74  ->method('debug')
75  ->with(
76  [
77  'request' => $this->getTransferData(),
78  'client' => TransactionVoid::class,
79  'response' => [],
80  ]
81  );
82 
83  $this->adapterMock->expects($this->once())
84  ->method('void')
85  ->with($this->transactionId)
86  ->willThrowException(new \Exception('Test messages'));
87 
89  $transferObjectMock = $this->getTransferObjectMock();
90 
91  $this->client->placeRequest($transferObjectMock);
92  }
93 
97  public function testPlaceRequestSuccess()
98  {
99  $response = new \stdClass;
100  $response->success = true;
101  $this->adapterMock->expects($this->once())
102  ->method('void')
103  ->with($this->transactionId)
104  ->willReturn($response);
105 
106  $this->loggerMock->expects($this->once())
107  ->method('debug')
108  ->with(
109  [
110  'request' => $this->getTransferData(),
111  'client' => TransactionVoid::class,
112  'response' => ['success' => 1],
113  ]
114  );
115 
116  $actualResult = $this->client->placeRequest($this->getTransferObjectMock());
117 
118  $this->assertTrue(is_object($actualResult['object']));
119  $this->assertEquals(['object' => $response], $actualResult);
120  }
121 
127  private function getTransferObjectMock()
128  {
129  $transferObjectMock = $this->createMock(TransferInterface::class);
130  $transferObjectMock->expects($this->once())
131  ->method('getBody')
132  ->willReturn($this->getTransferData());
133 
134  return $transferObjectMock;
135  }
136 
142  private function getTransferData()
143  {
144  return [
145  'transaction_id' => $this->transactionId,
146  ];
147  }
148 }
$response
Definition: 404.php:11