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