Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 8c1508f

Browse files
isavchuk-magentoYevSent
authored andcommitted
MAGETWO-62837: Api client decomposition
- ApiClient logic moved to subclasses
1 parent b232d3f commit 8c1508f

File tree

1 file changed

+25
-53
lines changed

1 file changed

+25
-53
lines changed

Model/CaseServices/CreationServiceTest.php

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use Magento\Framework\Api\FilterBuilder;
99
use Magento\Framework\Api\SearchCriteriaBuilder;
1010
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\HTTP\ZendClient;
12-
use Magento\Framework\HTTP\ZendClientFactory;
1311
use Magento\Sales\Api\Data\OrderInterface;
1412
use Magento\Sales\Api\OrderRepositoryInterface;
1513
use Magento\Signifyd\Api\CaseRepositoryInterface;
14+
use Magento\Signifyd\Model\SignifydGateway\ApiCallException;
1615
use Magento\Signifyd\Model\SignifydGateway\ApiClient;
16+
use Magento\Signifyd\Model\SignifydGateway\Client\RequestBuilder;
1717
use Magento\Signifyd\Model\SignifydGateway\Gateway;
1818
use Magento\TestFramework\Helper\Bootstrap;
1919
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -37,9 +37,9 @@ class CreationServiceTest extends \PHPUnit_Framework_TestCase
3737
private $order;
3838

3939
/**
40-
* @var ZendClient|MockObject
40+
* @var RequestBuilder|MockObject
4141
*/
42-
private $client;
42+
private $requestBuilder;
4343

4444
/**
4545
* @var LoggerInterface|MockObject
@@ -58,22 +58,14 @@ protected function setUp()
5858
{
5959
$this->objectManager = Bootstrap::getObjectManager();
6060

61-
$this->client = $this->getMockBuilder(ZendClient::class)
61+
$this->requestBuilder = $this->getMockBuilder(RequestBuilder::class)
6262
->disableOriginalConstructor()
63-
->setMethods(['setHeaders', 'setRawData', 'setMethod', 'setUri', 'request', 'getLastRequest'])
63+
->setMethods(['doRequest'])
6464
->getMock();
6565

66-
$clientFactory = $this->getMockBuilder(ZendClientFactory::class)
67-
->disableOriginalConstructor()
68-
->setMethods(['create'])
69-
->getMock();
70-
$clientFactory->expects(static::once())
71-
->method('create')
72-
->willReturn($this->client);
73-
7466
$apiClient = $this->objectManager->create(
7567
ApiClient::class,
76-
['clientFactory' => $clientFactory]
68+
['requestBuilder' => $this->requestBuilder]
7769
);
7870

7971
$gateway = $this->objectManager->create(
@@ -90,7 +82,7 @@ protected function setUp()
9082
CreationService::class,
9183
[
9284
'signifydGateway' => $gateway,
93-
'logger' => $this->logger
85+
'logger' => $this->logger
9486
]
9587
);
9688
}
@@ -102,23 +94,15 @@ protected function setUp()
10294
public function testCreateForOrderWithEmptyResponse()
10395
{
10496
$order = $this->getOrder();
105-
$requestData = [
106-
'purchase' => [
107-
'orderId' => $order->getEntityId()
108-
]
109-
];
97+
$exceptionMessage = 'Response is not valid JSON: Decoding failed: Syntax error';
11098

111-
$response = new \Zend_Http_Response(200, []);
112-
$this->client->expects(static::once())
113-
->method('request')
114-
->willReturn($response);
115-
$this->client->expects(static::atLeastOnce())
116-
->method('getLastRequest')
117-
->willReturn(json_encode($requestData));
99+
$this->requestBuilder->expects(static::once())
100+
->method('doRequest')
101+
->willThrowException(new ApiCallException($exceptionMessage));
118102

119103
$this->logger->expects(static::once())
120104
->method('error')
121-
->with('Response is not valid JSON: Decoding failed: Syntax error');
105+
->with($exceptionMessage);
122106

123107
$result = $this->service->createForOrder($order->getEntityId());
124108
static::assertTrue($result);
@@ -131,31 +115,20 @@ public function testCreateForOrderWithEmptyResponse()
131115
public function testCreateForOrderWithBadResponse()
132116
{
133117
$order = $this->getOrder();
134-
$requestData = [
135-
'purchase' => [
136-
'orderId' => $order->getEntityId()
137-
]
138-
];
139118
$responseData = [
140119
'messages' => [
141120
'Something wrong'
142121
]
143122
];
123+
$exceptionMessage = 'Bad Request - The request could not be parsed. Response: ' . json_encode($responseData);
144124

145-
$response = new \Zend_Http_Response(400, [], json_encode($responseData));
146-
$this->client->expects(static::once())
147-
->method('request')
148-
->willReturn($response);
149-
$this->client->expects(static::atLeastOnce())
150-
->method('getLastRequest')
151-
->willReturn(json_encode($requestData));
125+
$this->requestBuilder->expects(static::once())
126+
->method('doRequest')
127+
->willThrowException(new ApiCallException($exceptionMessage));
152128

153129
$this->logger->expects(static::once())
154130
->method('error')
155-
->with(
156-
'Bad Request - The request could not be parsed. Response: ' .
157-
json_encode($responseData)
158-
);
131+
->with($exceptionMessage);
159132

160133
$result = $this->service->createForOrder($order->getEntityId());
161134
static::assertTrue($result);
@@ -169,10 +142,9 @@ public function testCreateOrderWithEmptyInvestigationId()
169142
{
170143
$order = $this->getOrder();
171144

172-
$response = new \Zend_Http_Response(200, [], '{}');
173-
$this->client->expects(static::once())
174-
->method('request')
175-
->willReturn($response);
145+
$this->requestBuilder->expects(static::once())
146+
->method('doRequest')
147+
->willReturn([]);
176148

177149
$this->logger->expects(static::once())
178150
->method('error')
@@ -190,10 +162,9 @@ public function testCreateForOrder()
190162
{
191163
$order = $this->getOrder();
192164

193-
$response = new \Zend_Http_Response(200, [], json_encode(['investigationId' => 123123]));
194-
$this->client->expects(static::once())
195-
->method('request')
196-
->willReturn($response);
165+
$this->requestBuilder->expects(static::once())
166+
->method('doRequest')
167+
->willReturn(['investigationId' => 123123]);
197168

198169
$this->logger->expects(static::never())
199170
->method('error');
@@ -211,6 +182,7 @@ public function testCreateForOrder()
211182

212183
/**
213184
* Get stored order
185+
*
214186
* @return OrderInterface
215187
*/
216188
private function getOrder()

0 commit comments

Comments
 (0)