17
17
use PHPUnit \Framework \MockObject \MockObject ;
18
18
use PHPUnit \Framework \TestCase ;
19
19
use Psr \Log \LoggerInterface ;
20
+ use ReflectionMethod ;
21
+ use Zend_Http_Client_Exception ;
22
+ use Zend_Http_Response ;
20
23
21
24
/**
22
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
26
*/
24
27
class GatewayTest extends TestCase
25
28
{
26
- /** @var Gateway|MockObject */
27
- protected $ object ;
28
-
29
- /** @var ZendClientFactory|MockObject */
30
- protected $ httpClientFactoryMock ;
31
-
32
- /** @var Random|MockObject */
33
- protected $ mathRandomMock ;
34
-
35
- /** @var Logger|MockObject */
36
- protected $ loggerMock ;
37
-
38
- /** @var ZendClient|MockObject */
39
- protected $ zendClientMock ;
40
-
29
+ /**
30
+ * @var Gateway|MockObject
31
+ */
32
+ private $ object ;
33
+
34
+ /**
35
+ * @var ZendClientFactory|MockObject
36
+ */
37
+ private $ httpClientFactoryMock ;
38
+
39
+ /**
40
+ * @var Random|MockObject
41
+ */
42
+ private $ mathRandomMock ;
43
+
44
+ /**
45
+ * @var Logger|MockObject
46
+ */
47
+ private $ loggerMock ;
48
+
49
+ /**
50
+ * @var ZendClient|MockObject
51
+ */
52
+ private $ zendClientMock ;
53
+
54
+ /**
55
+ * @inheritdoc
56
+ */
41
57
protected function setUp (): void
42
58
{
43
59
$ this ->httpClientFactoryMock = $ this ->getMockBuilder (ZendClientFactory::class)
@@ -66,24 +82,28 @@ protected function setUp(): void
66
82
);
67
83
}
68
84
69
- public function testPostRequestOk ()
85
+ /**
86
+ * @param string $nvpResponse
87
+ * @param array $expectedResult
88
+ * @dataProvider postRequestOkDataProvider
89
+ */
90
+ public function testPostRequestOk (string $ nvpResponse , array $ expectedResult ): void
70
91
{
71
92
$ configMap = [
72
93
['getDebugReplacePrivateDataKeys ' , null , ['masked ' ]],
73
94
['debug ' , null , true ]
74
95
];
75
- $ expectedResponse = 'RESULT=0&RESPMSG=Approved&SECURETOKEN=8ZIaw2&SECURETOKENID=2481d53 ' ;
76
96
77
97
/** @var ConfigInterface|MockObject $configInterfaceMock */
78
98
$ configInterfaceMock = $ this ->getMockBuilder (ConfigInterface::class)
79
99
->getMockForAbstractClass ();
80
- $ zendResponseMock = $ this ->getMockBuilder (\ Zend_Http_Response::class)
100
+ $ zendResponseMock = $ this ->getMockBuilder (Zend_Http_Response::class)
81
101
->setMethods (['getBody ' ])
82
102
->disableOriginalConstructor ()
83
103
->getMock ();
84
104
$ zendResponseMock ->expects (static ::once ())
85
105
->method ('getBody ' )
86
- ->willReturn ($ expectedResponse );
106
+ ->willReturn ($ nvpResponse );
87
107
$ this ->zendClientMock ->expects (static ::once ())
88
108
->method ('request ' )
89
109
->willReturn ($ zendResponseMock );
@@ -98,8 +118,119 @@ public function testPostRequestOk()
98
118
99
119
$ result = $ this ->object ->postRequest ($ object , $ configInterfaceMock );
100
120
101
- static ::assertInstanceOf (DataObject::class, $ result );
102
- static ::assertArrayHasKey ('result_code ' , $ result ->getData ());
121
+ static ::assertEquals ($ expectedResult , $ result ->toArray ());
122
+ }
123
+
124
+ /**
125
+ * @return array[]
126
+ */
127
+ public function postRequestOkDataProvider (): array
128
+ {
129
+ return [
130
+ [
131
+ 'RESULT=0&RESPMSG=Approved&SECURETOKEN=9tl4MmP46NUadl9pwCKFgfQjA '
132
+ . '&SECURETOKENID=vVWBMSNb9j0SLlYw4AbqBnKmuogtzNNC ' ,
133
+ [
134
+ 'result ' => '0 ' ,
135
+ 'securetoken ' => '9tl4MmP46NUadl9pwCKFgfQjA ' ,
136
+ 'securetokenid ' => 'vVWBMSNb9j0SLlYw4AbqBnKmuogtzNNC ' ,
137
+ 'respmsg ' => 'Approved ' ,
138
+ 'result_code ' => '0 ' ,
139
+ ]
140
+ ],
141
+ [
142
+ 'RESULT=0&PNREF=A30A3A958244&RESPMSG=Approved&AUTHCODE=028PNI&AVSADDR=N&AVSZIP=N&HOSTCODE=A '
143
+ . '&PROCAVS=N&VISACARDLEVEL=12&TRANSTIME=2020-12-16 14:43:57&FIRSTNAME[4]=Joé '
144
+ . '&LASTNAME=O \'Reilly&COMPANYNAME[14]=Ruff & Johnson&COMMENT1[7]=Level=5 '
145
+ . '&AMT=30.00&ACCT=1111&EXPDATE=1224&CARDTYPE=0&IAVS=N ' ,
146
+ [
147
+ 'result ' => '0 ' ,
148
+ 'pnref ' => 'A30A3A958244 ' ,
149
+ 'respmsg ' => 'Approved ' ,
150
+ 'authcode ' => '028PNI ' ,
151
+ 'avsaddr ' => 'N ' ,
152
+ 'avszip ' => 'N ' ,
153
+ 'hostcode ' => 'A ' ,
154
+ 'procavs ' => 'N ' ,
155
+ 'visacardlevel ' => '12 ' ,
156
+ 'transtime ' => '2020-12-16 14:43:57 ' ,
157
+ 'firstname ' => 'Joé ' ,
158
+ 'lastname ' => 'O \'Reilly ' ,
159
+ 'companyname ' => 'Ruff & Johnson ' ,
160
+ 'comment1 ' => 'Level=5 ' ,
161
+ 'amt ' => '30.00 ' ,
162
+ 'acct ' => '1111 ' ,
163
+ 'expdate ' => '1224 ' ,
164
+ 'cardtype ' => '0 ' ,
165
+ 'iavs ' => 'N ' ,
166
+ 'result_code ' => '0 ' ,
167
+ ]
168
+ ],
169
+ ];
170
+ }
171
+
172
+ /**
173
+ * @param array $requestData
174
+ * @param string $requestBody
175
+ * @dataProvider requestBodyDataProvider
176
+ */
177
+ public function testRequestBody (array $ requestData , string $ requestBody ): void
178
+ {
179
+ $ configMap = [
180
+ ['getDebugReplacePrivateDataKeys ' , null , ['masked ' ]],
181
+ ['debug ' , null , true ]
182
+ ];
183
+
184
+ /** @var ConfigInterface|MockObject $configInterfaceMock */
185
+ $ configInterfaceMock = $ this ->getMockBuilder (ConfigInterface::class)
186
+ ->getMockForAbstractClass ();
187
+ $ zendResponseMock = $ this ->getMockBuilder (Zend_Http_Response::class)
188
+ ->setMethods (['getBody ' ])
189
+ ->disableOriginalConstructor ()
190
+ ->getMock ();
191
+ $ zendResponseMock ->expects (static ::once ())
192
+ ->method ('getBody ' )
193
+ ->willReturn ('RESULT=0&RESPMSG=Approved ' );
194
+ $ this ->zendClientMock ->expects (static ::once ())
195
+ ->method ('request ' )
196
+ ->willReturn ($ zendResponseMock );
197
+
198
+ $ configInterfaceMock ->expects (static ::any ())
199
+ ->method ('getValue ' )
200
+ ->willReturnMap ($ configMap );
201
+ $ this ->loggerMock ->expects (static ::once ())
202
+ ->method ('debug ' );
203
+
204
+ $ request = new DataObject ($ requestData );
205
+ $ this ->object ->postRequest ($ request , $ configInterfaceMock );
206
+ $ method = new ReflectionMethod ($ this ->zendClientMock , '_prepareBody ' );
207
+ $ method ->setAccessible (true );
208
+ $ this ->assertEquals ($ requestBody , $ method ->invoke ($ this ->zendClientMock ));
209
+ }
210
+
211
+ /**
212
+ * @return array[]
213
+ */
214
+ public function requestBodyDataProvider (): array
215
+ {
216
+ return [
217
+ [
218
+ [
219
+ 'companyname ' => 'Ruff & Johnson ' ,
220
+ 'comment1 ' => 'Level=5 ' ,
221
+ 'shiptofirstname ' => 'Joé ' ,
222
+ 'shiptolastname ' => 'O \'Reilly ' ,
223
+ 'shiptostreet ' => '4659 Rainbow Road ' ,
224
+ 'shiptocity ' => 'Los Angeles ' ,
225
+ 'shiptostate ' => 'CA ' ,
226
+ 'shiptozip ' => '90017 ' ,
227
+ 'shiptocountry ' => 'US ' ,
228
+ ],
229
+ 'companyname[14]=Ruff & Johnson&comment1[7]=Level=5&shiptofirstname=Joé&shiptolastname=O \'Reilly '
230
+ . '&shiptostreet=4659 Rainbow Road&shiptocity=Los Angeles&shiptostate=CA&shiptozip=90017 '
231
+ . '&shiptocountry=US '
232
+ ]
233
+ ];
103
234
}
104
235
105
236
public function testPostRequestFail ()
@@ -108,15 +239,15 @@ public function testPostRequestFail()
108
239
/** @var ConfigInterface|MockObject $configInterfaceMock */
109
240
$ configInterfaceMock = $ this ->getMockBuilder (ConfigInterface::class)
110
241
->getMockForAbstractClass ();
111
- $ zendResponseMock = $ this ->getMockBuilder (\ Zend_Http_Response::class)
242
+ $ zendResponseMock = $ this ->getMockBuilder (Zend_Http_Response::class)
112
243
->setMethods (['getBody ' ])
113
244
->disableOriginalConstructor ()
114
245
->getMock ();
115
246
$ zendResponseMock ->expects (static ::never ())
116
247
->method ('getBody ' );
117
248
$ this ->zendClientMock ->expects (static ::once ())
118
249
->method ('request ' )
119
- ->willThrowException (new \ Zend_Http_Client_Exception ());
250
+ ->willThrowException (new Zend_Http_Client_Exception ());
120
251
121
252
$ object = new DataObject ();
122
253
$ this ->object ->postRequest ($ object , $ configInterfaceMock );
0 commit comments