1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Service\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\Service\ServiceHttpTransport\ServiceHttpTransport; |
6
|
|
|
use Mezon\Transport\HttpRequestParams; |
7
|
|
|
use Mezon\Security\MockProvider; |
8
|
|
|
|
9
|
|
|
class ServiceHttpTransportUnitTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
* @see TestCase::setUp() |
16
|
|
|
*/ |
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Getting mock object. |
24
|
|
|
* |
25
|
|
|
* @return object ServiceLogic mocked object. |
26
|
|
|
*/ |
27
|
|
|
protected function getServiceLogicMock() |
28
|
|
|
{ |
29
|
|
|
return $this->getMockBuilder(TestingServiceLogicForHttpTransport::class) |
|
|
|
|
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->setMethods([ |
32
|
|
|
'connect' |
33
|
|
|
]) |
34
|
|
|
->getMock(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Getting mock object. |
39
|
|
|
* |
40
|
|
|
* @return object ServiceRestTransport mocked object. |
41
|
|
|
*/ |
42
|
|
|
protected function getTransportMock() |
43
|
|
|
{ |
44
|
|
|
$mock = $this->getMockBuilder(ServiceHttpTransport::class) |
|
|
|
|
45
|
|
|
->setMethods([ |
46
|
|
|
'header', |
47
|
|
|
'createSession' |
48
|
|
|
]) |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$mock->expects($this->once()) |
52
|
|
|
->method('header'); |
53
|
|
|
|
54
|
|
|
$mock->setParamsFetcher( |
55
|
|
|
$this->getMockBuilder(HttpRequestParams::class) |
|
|
|
|
56
|
|
|
->setMethods([ |
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
|
|
|
* Testing connect method. |
71
|
|
|
*/ |
72
|
|
|
public function testConstructor() |
73
|
|
|
{ |
74
|
|
|
new ServiceHttpTransport(); |
75
|
|
|
|
76
|
|
|
$this->addToAssertionCount(1); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Testing that security provider was set. |
81
|
|
|
*/ |
82
|
|
|
public function testSecurityProviderInitDefault() |
83
|
|
|
{ |
84
|
|
|
$transport = new ServiceHttpTransport(); |
85
|
|
|
$this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Testing that security provider was set. |
90
|
|
|
*/ |
91
|
|
|
public function testSecurityProviderInitString() |
92
|
|
|
{ |
93
|
|
|
$transport = new ServiceHttpTransport(MockProvider::class); |
94
|
|
|
$this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Testing that security provider was set. |
99
|
|
|
*/ |
100
|
|
|
public function testSecurityProviderInitObject() |
101
|
|
|
{ |
102
|
|
|
$transport = new ServiceHttpTransport(new MockProvider()); |
103
|
|
|
$this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Testing that header function is called once for each header. |
108
|
|
|
*/ |
109
|
|
|
public function testSingleHeaderCall() |
110
|
|
|
{ |
111
|
|
|
$mock = $this->getTransportMock(); |
112
|
|
|
|
113
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
114
|
|
|
|
115
|
|
|
$serviceLogic->expects($this->once()) |
116
|
|
|
->method('connect'); |
117
|
|
|
|
118
|
|
|
$mock->callLogic($serviceLogic, 'connect'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Testing that header function is called once for each header. |
123
|
|
|
*/ |
124
|
|
|
public function testSingleHeaderCallPublic() |
125
|
|
|
{ |
126
|
|
|
$mock = $this->getTransportMock(); |
127
|
|
|
|
128
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
129
|
|
|
|
130
|
|
|
$serviceLogic->expects($this->once()) |
131
|
|
|
->method('connect'); |
132
|
|
|
|
133
|
|
|
$mock->callPublicLogic($serviceLogic, 'connect'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Testing expected header values. |
138
|
|
|
*/ |
139
|
|
|
public function testExpectedHeaderValues() |
140
|
|
|
{ |
141
|
|
|
$mock = $this->getTransportMock(); |
142
|
|
|
|
143
|
|
|
$mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8')); |
144
|
|
|
|
145
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
146
|
|
|
|
147
|
|
|
$mock->callLogic($serviceLogic, 'connect'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Testing expected header values. |
152
|
|
|
*/ |
153
|
|
|
public function testExpectedHeaderValuesPublic() |
154
|
|
|
{ |
155
|
|
|
$mock = $this->getTransportMock(); |
156
|
|
|
|
157
|
|
|
$mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8')); |
158
|
|
|
|
159
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
160
|
|
|
|
161
|
|
|
$mock->callPublicLogic($serviceLogic, 'connect'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Getting tricky mock object |
166
|
|
|
*/ |
167
|
|
|
protected function getTransportMockEx(string $mode = 'publicCall') |
168
|
|
|
{ |
169
|
|
|
$mock = $this->getTransportMock(); |
170
|
|
|
|
171
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
172
|
|
|
|
173
|
|
|
$mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8')); |
174
|
|
|
|
175
|
|
|
$mock->addRoute('connect', 'connect', 'GET', $mode, [ |
176
|
|
|
'content_type' => 'text/html; charset=utf-8' |
177
|
|
|
]); |
178
|
|
|
|
179
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
180
|
|
|
$_GET['r'] = 'connect'; |
181
|
|
|
|
182
|
|
|
return $mock; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Testing expected header values. |
187
|
|
|
*/ |
188
|
|
|
public function testExpectedHeaderValuesEx() |
189
|
|
|
{ |
190
|
|
|
$mock = $this->getTransportMockEx('callLogic'); |
191
|
|
|
|
192
|
|
|
$mock->getRouter()->callRoute($_GET['r']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Testing expected header values. |
197
|
|
|
*/ |
198
|
|
|
public function testExpectedHeaderValuesPublicEx() |
199
|
|
|
{ |
200
|
|
|
$mock = $this->getTransportMockEx('publicCall'); |
201
|
|
|
|
202
|
|
|
$mock->getRouter()->callRoute($_GET['r']); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Testing public call without createSession method. |
207
|
|
|
*/ |
208
|
|
|
public function testPublicCall() |
209
|
|
|
{ |
210
|
|
|
$mock = $this->getTransportMock(); |
211
|
|
|
|
212
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
213
|
|
|
|
214
|
|
|
$mock->expects($this->never()) |
215
|
|
|
->method('createSession'); |
216
|
|
|
|
217
|
|
|
$mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call'); |
218
|
|
|
|
219
|
|
|
$mock->getRouter()->callRoute('/public-method/'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Testing private call with createSession method. |
224
|
|
|
*/ |
225
|
|
|
public function testPrivateCallNoException() |
226
|
|
|
{ |
227
|
|
|
// setup |
228
|
|
|
$mock = $this->getTransportMock(); |
229
|
|
|
|
230
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
231
|
|
|
|
232
|
|
|
$mock->expects($this->once()) |
233
|
|
|
->method('createSession'); |
234
|
|
|
|
235
|
|
|
$mock->addRoute('private-method', 'privateMethod', 'GET', 'private_call'); |
236
|
|
|
|
237
|
|
|
// test body and assertions |
238
|
|
|
$mock->getRouter()->callRoute('/private-method/'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Testing creaetSession method |
243
|
|
|
*/ |
244
|
|
|
public function testCreateSession(): void |
245
|
|
|
{ |
246
|
|
|
// setup and assertions |
247
|
|
|
$securityProvider = $this->getMockBuilder(MockProvider::class) |
|
|
|
|
248
|
|
|
->setMethods([ |
249
|
|
|
'createSession' |
250
|
|
|
]) |
251
|
|
|
->disableOriginalConstructor() |
252
|
|
|
->getMock(); |
253
|
|
|
$securityProvider->expects($this->once()) |
254
|
|
|
->method('createSession'); |
255
|
|
|
|
256
|
|
|
$transport = new ServiceHttpTransport($securityProvider); |
257
|
|
|
|
258
|
|
|
// test body |
259
|
|
|
$transport->createSession('some-token'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Testing method |
264
|
|
|
*/ |
265
|
|
|
public function test(): void |
266
|
|
|
{ |
267
|
|
|
// setup |
268
|
|
|
$e = [ |
269
|
|
|
"message" => "msg", |
270
|
|
|
"code" => - 1 |
271
|
|
|
]; |
272
|
|
|
$transport = $this->getTransportMockEx(); |
273
|
|
|
|
274
|
|
|
// test body |
275
|
|
|
ob_start(); |
276
|
|
|
$transport->outputException($e); |
277
|
|
|
$content = ob_get_contents(); |
278
|
|
|
ob_end_clean(); |
279
|
|
|
|
280
|
|
|
// assertions |
281
|
|
|
$this->assertStringContainsString('"msg"', $content); |
282
|
|
|
$this->assertStringContainsString('-1', $content); |
283
|
|
|
$this->assertTrue(is_array(json_decode($content, true))); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
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.