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