1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Service\Tests; |
3
|
|
|
|
4
|
|
|
use Mezon\Functional\Fetcher; |
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ServiceTests |
9
|
|
|
* |
10
|
|
|
* @package Service |
11
|
|
|
* @subpackage ServiceTests |
12
|
|
|
* @author Dodonov A.A. |
13
|
|
|
* @version v.1.0 (2019/08/17) |
14
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Predefined set of tests for service |
19
|
|
|
* |
20
|
|
|
* @author Dodonov A.A. |
21
|
|
|
*/ |
22
|
|
|
abstract class ServiceTests extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Session id |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $sessionId = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Server path |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected static $serverPath = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Headers |
41
|
|
|
* |
42
|
|
|
* @var ?array |
43
|
|
|
*/ |
44
|
|
|
protected $headers = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Method asserts for errors and warnings in the html code |
48
|
|
|
* |
49
|
|
|
* @param string $content |
50
|
|
|
* Asserting content |
51
|
|
|
* @param string $message |
52
|
|
|
* Message to be displayed in case of error |
53
|
|
|
*/ |
54
|
|
|
protected function assertErrors($content, $message): void |
55
|
|
|
{ |
56
|
|
|
if (strpos($content, 'Warning') !== false || strpos($content, 'Error') !== false || |
57
|
|
|
strpos($content, 'Fatal error') !== false || strpos($content, 'Access denied') !== false || |
58
|
|
|
strpos($content, "doesn't exist in statement") !== false) { |
59
|
|
|
throw (new \Exception($message . "\r\n" . $content)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->assertTrue(true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Method asserts JSON |
67
|
|
|
* |
68
|
|
|
* @param mixed $jsonResult |
69
|
|
|
* Result of the call |
70
|
|
|
* @param string $result |
71
|
|
|
* Raw result of the call |
72
|
|
|
*/ |
73
|
|
|
protected function assertJsonResponse($jsonResult, string $result): void |
74
|
|
|
{ |
75
|
|
|
if ($jsonResult === null && $result !== '') { |
76
|
|
|
$this->fail("JSON result is invalid because of:\r\n$result"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (isset($jsonResult->message)) { |
80
|
|
|
print("message : " . $jsonResult->message . "\r\n"); |
81
|
|
|
print("code : " . ($jsonResult->code ?? '') . "\r\n"); |
82
|
|
|
print("call_stack : "); |
83
|
|
|
print(json_encode($jsonResult->call_stack ?? 'not provided')); |
84
|
|
|
$this->fail(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Method sends post request |
90
|
|
|
* |
91
|
|
|
* @param array $data |
92
|
|
|
* Request data |
93
|
|
|
* @param string $url |
94
|
|
|
* Requesting endpoint |
95
|
|
|
* @return mixed Request result |
96
|
|
|
*/ |
97
|
|
|
protected function postHttpRequest(array $data, string $url) |
98
|
|
|
{ |
99
|
|
|
$options = [ |
100
|
|
|
'http' => [ |
101
|
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . |
102
|
|
|
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n" . |
103
|
|
|
($this->sessionId !== '' ? "Cgi-Authorization: Basic " . $this->sessionId . "\r\n" : '') . |
104
|
|
|
($this->headers !== null ? implode("\r\n", $this->headers) . "\r\n" : ''), |
105
|
|
|
'method' => 'POST', |
106
|
|
|
'content' => http_build_query($data) |
107
|
|
|
] |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$context = stream_context_create($options); |
111
|
|
|
$result = file_get_contents($url, false, $context); |
112
|
|
|
|
113
|
|
|
$this->assertErrors($result, 'Request have returned warnings/errors'); |
114
|
|
|
|
115
|
|
|
return json_decode($result); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Method prepares GET request options |
120
|
|
|
* |
121
|
|
|
* @return array GET request options |
122
|
|
|
*/ |
123
|
|
|
protected function prepareGetOptions(): array |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
|
|
'http' => [ |
127
|
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . |
128
|
|
|
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n" . |
129
|
|
|
($this->sessionId !== '' ? "Cgi-Authorization: Basic " . $this->sessionId . "\r\n" : '') . |
130
|
|
|
($this->headers !== null ? implode("\r\n", $this->headers) . "\r\n" : ''), |
131
|
|
|
'method' => 'GET' |
132
|
|
|
] |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Method sends GET request |
138
|
|
|
* |
139
|
|
|
* @param string $url |
140
|
|
|
* Requesting URL |
141
|
|
|
* @return mixed Result off the request |
142
|
|
|
*/ |
143
|
|
|
protected function getHttpRequest(string $url) |
144
|
|
|
{ |
145
|
|
|
$options = $this->prepareGetOptions(); |
146
|
|
|
|
147
|
|
|
$context = stream_context_create($options); |
148
|
|
|
$result = file_get_contents($url, false, $context); |
149
|
|
|
|
150
|
|
|
$this->assertErrors($result, 'Request have returned warnings/errors'); |
151
|
|
|
|
152
|
|
|
return json_decode($result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Method returns array ['login' => '' , 'password' => ''], for connecting to service |
157
|
|
|
* |
158
|
|
|
* @return array Test data |
159
|
|
|
*/ |
160
|
|
|
protected abstract function getUserData(): array; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Method performs valid connect |
164
|
|
|
* |
165
|
|
|
* @return mixed Result of the connection |
166
|
|
|
*/ |
167
|
|
|
protected function validConnect() |
168
|
|
|
{ |
169
|
|
|
$data = $this->getUserData(); |
170
|
|
|
|
171
|
|
|
$url = self::$serverPath . '/connect/'; |
172
|
|
|
|
173
|
|
|
$result = $this->postHttpRequest($data, $url); |
174
|
|
|
|
175
|
|
|
if (isset($result->session_id) !== false) { |
176
|
|
|
$this->sessionId = $result->session_id; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Testing API connection |
184
|
|
|
*/ |
185
|
|
|
public function testValidConnect(): void |
186
|
|
|
{ |
187
|
|
|
// setup and test body |
188
|
|
|
$result = $this->validConnect(); |
189
|
|
|
|
190
|
|
|
// assertions |
191
|
|
|
$this->assertNotEquals($result, null, 'Connection failed'); |
192
|
|
|
|
193
|
|
|
if (isset($result->session_id) === false) { |
194
|
|
|
$this->assertEquals(true, false, 'Field "session_id" was not set'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->sessionId = $result->session_id; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Testing API invalid connection |
202
|
|
|
*/ |
203
|
|
|
public function testInvalidConnect(): void |
204
|
|
|
{ |
205
|
|
|
// setup |
206
|
|
|
$data = $this->getUserData(); |
207
|
|
|
$data['password'] = '1234'; |
208
|
|
|
$url = self::$serverPath . '/connect/'; |
209
|
|
|
|
210
|
|
|
// test body |
211
|
|
|
$result = $this->postHttpRequest($data, $url); |
212
|
|
|
|
213
|
|
|
// assertions |
214
|
|
|
$this->assertTrue(isset($result->message)); |
215
|
|
|
$this->assertTrue(isset($result->code)); |
216
|
|
|
$this->assertTrue($result->code == - 1 || $result->code == 4); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Testing setting valid token |
221
|
|
|
*/ |
222
|
|
|
public function testSetValidToken(): void |
223
|
|
|
{ |
224
|
|
|
// setup |
225
|
|
|
$this->testValidConnect(); |
226
|
|
|
|
227
|
|
|
$data = [ |
228
|
|
|
'token' => $this->sessionId |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
$url = self::$serverPath . '/token/' . $this->sessionId . '/'; |
232
|
|
|
|
233
|
|
|
// test body |
234
|
|
|
$result = $this->postHttpRequest($data, $url); |
235
|
|
|
|
236
|
|
|
// assertions |
237
|
|
|
$this->assertEquals(isset($result->session_id), true, 'Connection failed'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Testing setting invalid token |
242
|
|
|
*/ |
243
|
|
|
public function testSetInvalidToken(): void |
244
|
|
|
{ |
245
|
|
|
// setup |
246
|
|
|
$this->testValidConnect(); |
247
|
|
|
|
248
|
|
|
$data = [ |
249
|
|
|
'token' => '' |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$url = self::$serverPath . '/token/unexisting/'; |
253
|
|
|
|
254
|
|
|
// test body |
255
|
|
|
$result = $this->postHttpRequest($data, $url); |
256
|
|
|
|
257
|
|
|
// assertions |
258
|
|
|
$this->assertObjectHasAttribute('message', $result); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Testing login under another user |
263
|
|
|
*/ |
264
|
|
|
public function testLoginAs(): void |
265
|
|
|
{ |
266
|
|
|
// setup |
267
|
|
|
$this->testValidConnect(); |
268
|
|
|
|
269
|
|
|
$data = [ |
270
|
|
|
'login' => '[email protected]' |
271
|
|
|
]; |
272
|
|
|
$url = self::$serverPath . '/login-as/'; |
273
|
|
|
$this->postHttpRequest($data, $url); |
274
|
|
|
|
275
|
|
|
// test body |
276
|
|
|
$url = self::$serverPath . '/self/login/'; |
277
|
|
|
$result = $this->getHttpRequest($url); |
278
|
|
|
|
279
|
|
|
// assertions |
280
|
|
|
$this->assertEquals('[email protected]', Fetcher::getField($result, 'login')); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|