8
8
use Magento \Framework \Api \FilterBuilder ;
9
9
use Magento \Framework \Api \SearchCriteriaBuilder ;
10
10
use Magento \Framework \App \ObjectManager ;
11
- use Magento \Framework \HTTP \ZendClient ;
12
- use Magento \Framework \HTTP \ZendClientFactory ;
13
11
use Magento \Sales \Api \Data \OrderInterface ;
14
12
use Magento \Sales \Api \OrderRepositoryInterface ;
15
13
use Magento \Signifyd \Api \CaseRepositoryInterface ;
14
+ use Magento \Signifyd \Model \SignifydGateway \ApiCallException ;
16
15
use Magento \Signifyd \Model \SignifydGateway \ApiClient ;
16
+ use Magento \Signifyd \Model \SignifydGateway \Client \RequestBuilder ;
17
17
use Magento \Signifyd \Model \SignifydGateway \Gateway ;
18
18
use Magento \TestFramework \Helper \Bootstrap ;
19
19
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
@@ -37,9 +37,9 @@ class CreationServiceTest extends \PHPUnit_Framework_TestCase
37
37
private $ order ;
38
38
39
39
/**
40
- * @var ZendClient |MockObject
40
+ * @var RequestBuilder |MockObject
41
41
*/
42
- private $ client ;
42
+ private $ requestBuilder ;
43
43
44
44
/**
45
45
* @var LoggerInterface|MockObject
@@ -58,22 +58,14 @@ protected function setUp()
58
58
{
59
59
$ this ->objectManager = Bootstrap::getObjectManager ();
60
60
61
- $ this ->client = $ this ->getMockBuilder (ZendClient ::class)
61
+ $ this ->requestBuilder = $ this ->getMockBuilder (RequestBuilder ::class)
62
62
->disableOriginalConstructor ()
63
- ->setMethods (['setHeaders ' , ' setRawData ' , ' setMethod ' , ' setUri ' , ' request ' , ' getLastRequest ' ])
63
+ ->setMethods (['doRequest ' ])
64
64
->getMock ();
65
65
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
-
74
66
$ apiClient = $ this ->objectManager ->create (
75
67
ApiClient::class,
76
- ['clientFactory ' => $ clientFactory ]
68
+ ['requestBuilder ' => $ this -> requestBuilder ]
77
69
);
78
70
79
71
$ gateway = $ this ->objectManager ->create (
@@ -90,7 +82,7 @@ protected function setUp()
90
82
CreationService::class,
91
83
[
92
84
'signifydGateway ' => $ gateway ,
93
- 'logger ' => $ this ->logger
85
+ 'logger ' => $ this ->logger
94
86
]
95
87
);
96
88
}
@@ -102,23 +94,15 @@ protected function setUp()
102
94
public function testCreateForOrderWithEmptyResponse ()
103
95
{
104
96
$ order = $ this ->getOrder ();
105
- $ requestData = [
106
- 'purchase ' => [
107
- 'orderId ' => $ order ->getEntityId ()
108
- ]
109
- ];
97
+ $ exceptionMessage = 'Response is not valid JSON: Decoding failed: Syntax error ' ;
110
98
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 ));
118
102
119
103
$ this ->logger ->expects (static ::once ())
120
104
->method ('error ' )
121
- ->with (' Response is not valid JSON: Decoding failed: Syntax error ' );
105
+ ->with ($ exceptionMessage );
122
106
123
107
$ result = $ this ->service ->createForOrder ($ order ->getEntityId ());
124
108
static ::assertTrue ($ result );
@@ -131,31 +115,20 @@ public function testCreateForOrderWithEmptyResponse()
131
115
public function testCreateForOrderWithBadResponse ()
132
116
{
133
117
$ order = $ this ->getOrder ();
134
- $ requestData = [
135
- 'purchase ' => [
136
- 'orderId ' => $ order ->getEntityId ()
137
- ]
138
- ];
139
118
$ responseData = [
140
119
'messages ' => [
141
120
'Something wrong '
142
121
]
143
122
];
123
+ $ exceptionMessage = 'Bad Request - The request could not be parsed. Response: ' . json_encode ($ responseData );
144
124
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 ));
152
128
153
129
$ this ->logger ->expects (static ::once ())
154
130
->method ('error ' )
155
- ->with (
156
- 'Bad Request - The request could not be parsed. Response: ' .
157
- json_encode ($ responseData )
158
- );
131
+ ->with ($ exceptionMessage );
159
132
160
133
$ result = $ this ->service ->createForOrder ($ order ->getEntityId ());
161
134
static ::assertTrue ($ result );
@@ -169,10 +142,9 @@ public function testCreateOrderWithEmptyInvestigationId()
169
142
{
170
143
$ order = $ this ->getOrder ();
171
144
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 ([]);
176
148
177
149
$ this ->logger ->expects (static ::once ())
178
150
->method ('error ' )
@@ -190,10 +162,9 @@ public function testCreateForOrder()
190
162
{
191
163
$ order = $ this ->getOrder ();
192
164
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 ]);
197
168
198
169
$ this ->logger ->expects (static ::never ())
199
170
->method ('error ' );
@@ -211,6 +182,7 @@ public function testCreateForOrder()
211
182
212
183
/**
213
184
* Get stored order
185
+ *
214
186
* @return OrderInterface
215
187
*/
216
188
private function getOrder ()
0 commit comments