Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionSaleTest.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 TransactionSaleTest extends \PHPUnit\Framework\TestCase
20 {
24  private $model;
25 
29  private $loggerMock;
30 
34  private $adapterMock;
35 
39  protected function setUp()
40  {
42  $criticalLoggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
43  $this->loggerMock = $this->getMockBuilder(Logger::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
47  ->disableOriginalConstructor()
48  ->getMock();
50  $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $adapterFactoryMock->expects(self::once())
54  ->method('create')
55  ->willReturn($this->adapterMock);
56 
57  $this->model = new TransactionSale($criticalLoggerMock, $this->loggerMock, $adapterFactoryMock);
58  }
59 
68  public function testPlaceRequestException()
69  {
70  $this->loggerMock->expects($this->once())
71  ->method('debug')
72  ->with(
73  [
74  'request' => $this->getTransferData(),
75  'client' => TransactionSale::class,
76  'response' => []
77  ]
78  );
79 
80  $this->adapterMock->expects($this->once())
81  ->method('sale')
82  ->willThrowException(new \Exception('Test messages'));
83 
85  $transferObjectMock = $this->getTransferObjectMock();
86 
87  $this->model->placeRequest($transferObjectMock);
88  }
89 
95  public function testPlaceRequestSuccess()
96  {
97  $response = $this->getResponseObject();
98  $this->adapterMock->expects($this->once())
99  ->method('sale')
100  ->with($this->getTransferData())
101  ->willReturn($response);
102 
103  $this->loggerMock->expects($this->once())
104  ->method('debug')
105  ->with(
106  [
107  'request' => $this->getTransferData(),
108  'client' => TransactionSale::class,
109  'response' => ['success' => 1]
110  ]
111  );
112 
113  $actualResult = $this->model->placeRequest($this->getTransferObjectMock());
114 
115  $this->assertTrue(is_object($actualResult['object']));
116  $this->assertEquals(['object' => $response], $actualResult);
117  }
118 
124  private function getTransferObjectMock()
125  {
126  $transferObjectMock = $this->createMock(TransferInterface::class);
127  $transferObjectMock->expects($this->once())
128  ->method('getBody')
129  ->willReturn($this->getTransferData());
130 
131  return $transferObjectMock;
132  }
133 
139  private function getResponseObject()
140  {
141  $obj = new \stdClass;
142  $obj->success = true;
143 
144  return $obj;
145  }
146 
152  private function getTransferData()
153  {
154  return [
155  'test-data-key' => 'test-data-value'
156  ];
157  }
158 }
$response
Definition: 404.php:11