1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Service\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\Service\ServiceRestTransport\ServiceRestTransport; |
6
|
|
|
use Mezon\Transport\HttpRequestParams; |
7
|
|
|
use Mezon\Security\MockProvider; |
8
|
|
|
use Mezon\Rest; |
9
|
|
|
use Mezon\Transport\Tests\Headers; |
10
|
|
|
|
11
|
|
|
if (defined('MEZON_DEBUG') === false) { |
12
|
|
|
define('MEZON_DEBUG', true); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
18
|
|
|
*/ |
19
|
|
|
class ServiceRestTransportUnitTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
* @see TestCase::setUp() |
26
|
|
|
*/ |
27
|
|
|
protected function setUp(): void |
28
|
|
|
{ |
29
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Getting mock object |
34
|
|
|
* |
35
|
|
|
* @return object ServiceRestTransport mocked object |
36
|
|
|
*/ |
37
|
|
|
protected function getTransportMock() |
38
|
|
|
{ |
39
|
|
|
$mock = $this->getMockBuilder(ServiceRestTransport::class) |
40
|
|
|
->setConstructorArgs([new MockProvider()]) |
41
|
|
|
->onlyMethods([ |
42
|
|
|
'header', |
43
|
|
|
'createSession', |
44
|
|
|
'errorResponse', |
45
|
|
|
'parentErrorResponse' |
46
|
|
|
]) |
47
|
|
|
->getMock(); |
48
|
|
|
|
49
|
|
|
$mock->expects($this->once()) |
50
|
|
|
->method('header'); |
51
|
|
|
$mock->method('errorResponse')->willThrowException(new Rest\Exception('Msg', 0, 1, 1)); |
52
|
|
|
$mock->method('parentErrorResponse')->willThrowException(new \Exception('Msg', 0)); |
53
|
|
|
|
54
|
|
|
$mock->setParamsFetcher( |
55
|
|
|
$this->getMockBuilder(HttpRequestParams::class) |
56
|
|
|
->onlyMethods([ |
57
|
|
|
'getParam' |
58
|
|
|
]) |
59
|
|
|
->disableOriginalConstructor() |
60
|
|
|
->getMock()); |
61
|
|
|
|
62
|
|
|
$mock->getParamsFetcher() |
63
|
|
|
->method('getParam') |
64
|
|
|
->willReturn('token'); |
65
|
|
|
|
66
|
|
|
return $mock; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Getting mock object. |
71
|
|
|
* |
72
|
|
|
* @return object ServiceLogic mocked object |
73
|
|
|
*/ |
74
|
|
|
protected function getServiceLogicMock() |
75
|
|
|
{ |
76
|
|
|
return $this->getMockBuilder(TestingServiceLogicForRestTransport::class) |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->onlyMethods([ |
79
|
|
|
'connect' |
80
|
|
|
]) |
81
|
|
|
->getMock(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Testing that security provider was set |
86
|
|
|
*/ |
87
|
|
|
public function testSecurityProviderInitObject() |
88
|
|
|
{ |
89
|
|
|
$transport = new ServiceRestTransport(new MockProvider()); |
90
|
|
|
$this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Testing that header function is called once for each header |
95
|
|
|
*/ |
96
|
|
|
public function testSingleHeaderCallPublic() |
97
|
|
|
{ |
98
|
|
|
$mock = $this->getTransportMock(); |
99
|
|
|
|
100
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
101
|
|
|
|
102
|
|
|
$serviceLogic->expects($this->once()) |
103
|
|
|
->method('connect'); |
104
|
|
|
|
105
|
|
|
$mock->callPublicLogic($serviceLogic, 'connect'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Setup method call |
110
|
|
|
* |
111
|
|
|
* @param string $methodName |
112
|
|
|
* Method name |
113
|
|
|
* @return object Mock object |
114
|
|
|
*/ |
115
|
|
|
protected function setupMethod(string $methodName): object |
116
|
|
|
{ |
117
|
|
|
$mock = $this->getTransportMock(); |
118
|
|
|
|
119
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
120
|
|
|
|
121
|
|
|
$mock->expects($this->never()) |
122
|
|
|
->method('createSession'); |
123
|
|
|
|
124
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
125
|
|
|
$mock->addRoute('public-method', $methodName, 'GET', 'public_call'); |
126
|
|
|
|
127
|
|
|
return $mock; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Testing public call without createSession method. |
132
|
|
|
*/ |
133
|
|
|
public function testPublicCall() |
134
|
|
|
{ |
135
|
|
|
// setup |
136
|
|
|
$mock = $this->setupMethod('publicMethod'); |
137
|
|
|
|
138
|
|
|
// test body and assertions |
139
|
|
|
$mock->getRouter()->callRoute('/public-method/'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Setup method call |
144
|
|
|
* |
145
|
|
|
* @param string $methodName |
146
|
|
|
* Method name |
147
|
|
|
* @return object Mock object |
148
|
|
|
*/ |
149
|
|
|
protected function setupPrivateMethod(string $methodName): object |
150
|
|
|
{ |
151
|
|
|
$mock = $this->getTransportMock(); |
152
|
|
|
|
153
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
154
|
|
|
|
155
|
|
|
$mock->expects($this->once()) |
156
|
|
|
->method('createSession'); |
157
|
|
|
|
158
|
|
|
$mock->addRoute('private-method', $methodName, 'GET', 'private_call'); |
159
|
|
|
|
160
|
|
|
return $mock; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Testing private call with createSession method. |
165
|
|
|
*/ |
166
|
|
|
public function testPrivateCall() |
167
|
|
|
{ |
168
|
|
|
// setup |
169
|
|
|
$mock = $this->setupPrivateMethod('privateMethod'); |
170
|
|
|
|
171
|
|
|
// test body and assertions |
172
|
|
|
$mock->getRouter()->callRoute('/private-method/'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Testing public call with exception throwing |
177
|
|
|
*/ |
178
|
|
|
public function testPublicCallException() |
179
|
|
|
{ |
180
|
|
|
// setup |
181
|
|
|
$mock = $this->setupMethod('methodException'); |
182
|
|
|
|
183
|
|
|
$this->expectException(\Exception::class); |
184
|
|
|
|
185
|
|
|
// test body and assertions |
186
|
|
|
$mock->getRouter()->callRoute('/public-method/'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Testing public call with exception throwing |
191
|
|
|
*/ |
192
|
|
|
public function testPublicCallRestException() |
193
|
|
|
{ |
194
|
|
|
// setup |
195
|
|
|
$mock = $this->setupMethod('methodRestException'); |
196
|
|
|
|
197
|
|
|
$this->expectException(\Exception::class); |
198
|
|
|
// test body and assertions |
199
|
|
|
$mock->getRouter()->callRoute('/public-method/'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Testing private call with exception throwing |
204
|
|
|
*/ |
205
|
|
|
public function testPrivateCallException() |
206
|
|
|
{ |
207
|
|
|
// setup |
208
|
|
|
$mock = $this->setupPrivateMethod('methodException'); |
209
|
|
|
|
210
|
|
|
$this->expectException(\Exception::class); |
211
|
|
|
|
212
|
|
|
// test body and assertions |
213
|
|
|
$mock->getRouter()->callRoute('/private-method/'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Testing private call with exception throwing |
218
|
|
|
*/ |
219
|
|
|
public function testPrivateCallRestException() |
220
|
|
|
{ |
221
|
|
|
// setup |
222
|
|
|
$mock = $this->setupPrivateMethod('methodRestException'); |
223
|
|
|
|
224
|
|
|
$this->expectException(\Exception::class); |
225
|
|
|
|
226
|
|
|
// test body and assertions |
227
|
|
|
$mock->getRouter()->callRoute('/private-method/'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Testing errorResponse method |
232
|
|
|
*/ |
233
|
|
|
public function testErrorResponseException(): void |
234
|
|
|
{ |
235
|
|
|
// setup |
236
|
|
|
$_SERVER['HTTP_HOST'] = 'http://service'; |
237
|
|
|
$e = new \Exception('msg', 1); |
238
|
|
|
$Transport = new ServiceRestTransport(new MockProvider()); |
239
|
|
|
|
240
|
|
|
// test body |
241
|
|
|
$result = $Transport->errorResponse($e); |
242
|
|
|
|
243
|
|
|
// assertions |
244
|
|
|
$this->assertEquals('msg', $result['message']); |
245
|
|
|
$this->assertEquals(1, $result['code']); |
246
|
|
|
$this->assertEquals('http://service', $result['service']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Testing errorResponse method |
251
|
|
|
*/ |
252
|
|
|
public function testErrorResponseRestException(): void |
253
|
|
|
{ |
254
|
|
|
// setup |
255
|
|
|
$_SERVER['HTTP_HOST'] = 'http://rest-service'; |
256
|
|
|
$e = new Rest\Exception('msg', 1, 200, 'body'); |
257
|
|
|
$Transport = new ServiceRestTransport(new MockProvider()); |
258
|
|
|
|
259
|
|
|
// test body |
260
|
|
|
$result = $Transport->errorResponse($e); |
261
|
|
|
|
262
|
|
|
// assertions |
263
|
|
|
$this->assertEquals('msg', $result['message']); |
264
|
|
|
$this->assertEquals(1, $result['code']); |
265
|
|
|
$this->assertEquals('http://rest-service', $result['service']); |
266
|
|
|
$this->assertEquals(200, $result['http_code']); |
267
|
|
|
$this->assertEquals('body', $result['http_body']); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Testing parentErrorResponse method |
272
|
|
|
*/ |
273
|
|
|
public function testParentErrorResponseRestException(): void |
274
|
|
|
{ |
275
|
|
|
// setup |
276
|
|
|
$e = new \Exception('msg', 1); |
277
|
|
|
$Transport = new ServiceRestTransport(new MockProvider()); |
278
|
|
|
|
279
|
|
|
// test body |
280
|
|
|
$result = $Transport->parentErrorResponse($e); |
281
|
|
|
|
282
|
|
|
// assertions |
283
|
|
|
$this->assertEquals('msg', $result['message']); |
284
|
|
|
$this->assertEquals(1, $result['code']); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Getting tricky mock object |
289
|
|
|
*/ |
290
|
|
|
protected function getTransportMockEx(string $mode = 'publicCall') |
291
|
|
|
{ |
292
|
|
|
$mock = $this->getTransportMock(); |
293
|
|
|
|
294
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
295
|
|
|
|
296
|
|
|
$mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('application/json')); |
297
|
|
|
|
298
|
|
|
$mock->addRoute('connect', 'connect', 'GET', $mode, [ |
299
|
|
|
'content_type' => 'application/json' |
300
|
|
|
]); |
301
|
|
|
|
302
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
303
|
|
|
$_GET['r'] = 'connect'; |
304
|
|
|
|
305
|
|
|
return $mock; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Testing method outputException |
310
|
|
|
*/ |
311
|
|
|
public function testOutputException(): void |
312
|
|
|
{ |
313
|
|
|
// setup |
314
|
|
|
$e = [ |
315
|
|
|
"message" => "msg", |
316
|
|
|
"code" => - 1 |
317
|
|
|
]; |
318
|
|
|
$transport = $this->getTransportMockEx(); |
319
|
|
|
|
320
|
|
|
// test body |
321
|
|
|
ob_start(); |
322
|
|
|
$transport->outputException($e); |
323
|
|
|
$content = ob_get_contents(); |
324
|
|
|
ob_end_clean(); |
325
|
|
|
|
326
|
|
|
// assertions |
327
|
|
|
$this->assertStringContainsString('"msg"', $content); |
328
|
|
|
$this->assertStringContainsString('-1', $content); |
329
|
|
|
$this->assertTrue(is_array(json_decode($content, true))); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Testing that route will be called |
334
|
|
|
*/ |
335
|
|
|
public function testCallRoute(): void |
336
|
|
|
{ |
337
|
|
|
// setup |
338
|
|
|
$transport = new ServiceRestTransport(new MockProvider()); |
339
|
|
|
$transport->setServiceLogic(new TestingServiceLogicForRestTransport()); |
340
|
|
|
$transport->addRoute('/ok/', 'ok', 'GET', 'public_call'); |
341
|
|
|
$_GET['r'] = 'ok'; |
342
|
|
|
|
343
|
|
|
// test body |
344
|
|
|
ob_start(); |
345
|
|
|
$transport->run(); |
346
|
|
|
$content = ob_get_contents(); |
347
|
|
|
ob_end_clean(); |
348
|
|
|
|
349
|
|
|
// assertions |
350
|
|
|
$this->assertEquals('"ok"', $content); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Testing that header function is called once for each header. |
355
|
|
|
*/ |
356
|
|
|
public function testSingleHeaderCall() |
357
|
|
|
{ |
358
|
|
|
// setup |
359
|
|
|
Headers::setAllHeaders([ |
360
|
|
|
'Authentication' => 'Basic token' |
361
|
|
|
]); |
362
|
|
|
$mock = $this->getTransportMock(); |
363
|
|
|
|
364
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
365
|
|
|
|
366
|
|
|
// assertions |
367
|
|
|
$serviceLogic->expects($this->once()) |
368
|
|
|
->method('connect'); |
369
|
|
|
|
370
|
|
|
// test body |
371
|
|
|
$mock->callLogic($serviceLogic, 'connect'); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|