1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSSCTEST; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use WSSC\Components\ClientConfig; |
7
|
|
|
use WSSC\Exceptions\BadOpcodeException; |
8
|
|
|
use WSSC\WebSocketClient; |
9
|
|
|
|
10
|
|
|
class WebSocketClientTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private const WS_SCHEME = 'ws://'; |
13
|
|
|
private const WS_HOST = 'localhost'; |
14
|
|
|
private const WS_PORT = ':8000'; |
15
|
|
|
private const WS_URI = '/notifications/messanger/vkjsndfvjn23243'; |
16
|
|
|
|
17
|
|
|
private $url; |
18
|
|
|
|
19
|
|
|
public function setUp(): void |
20
|
|
|
{ |
21
|
|
|
$this->url = self::WS_SCHEME . self::WS_HOST . self::WS_PORT . self::WS_URI; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @test |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
public function is_client_connected() |
29
|
|
|
{ |
30
|
|
|
$recvMsg = '{"user_id" : 123}'; |
31
|
|
|
$client = new WebSocketClient($this->url, new ClientConfig()); |
32
|
|
|
try { |
33
|
|
|
$client->send($recvMsg); |
34
|
|
|
} catch (BadOpcodeException $e) { |
35
|
|
|
echo 'Couldn`t sent: ' . $e->getMessage(); |
36
|
|
|
} |
37
|
|
|
$recv = $client->receive(); |
38
|
|
|
$this->assertEquals($recv, $recvMsg); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function can_send_larg_payload() |
46
|
|
|
{ |
47
|
|
|
$recvMsg = '{ |
48
|
|
|
"squadName": "Super hero squad", |
49
|
|
|
"homeTown": "Metro City", |
50
|
|
|
"formed": 2016, |
51
|
|
|
"secretBase": "Super tower", |
52
|
|
|
"active": true, |
53
|
|
|
"members": [ |
54
|
|
|
{ |
55
|
|
|
"name": "Molecule Man", |
56
|
|
|
"age": 29, |
57
|
|
|
"secretIdentity": "Dan Jukes", |
58
|
|
|
"powers": [ |
59
|
|
|
"Radiation resistance", |
60
|
|
|
"Turning tiny", |
61
|
|
|
"Radiation blast" |
62
|
|
|
] |
63
|
|
|
}, |
64
|
|
|
{ |
65
|
|
|
"name": "Madame Uppercut", |
66
|
|
|
"age": 39, |
67
|
|
|
"secretIdentity": "Jane Wilson", |
68
|
|
|
"powers": [ |
69
|
|
|
"Million tonne punch", |
70
|
|
|
"Damage resistance", |
71
|
|
|
"Superhuman reflexes" |
72
|
|
|
] |
73
|
|
|
}, |
74
|
|
|
{ |
75
|
|
|
"name": "Eternal Flame", |
76
|
|
|
"age": 1000000, |
77
|
|
|
"secretIdentity": "Unknown", |
78
|
|
|
"powers": [ |
79
|
|
|
"Immortality", |
80
|
|
|
"Heat Immunity", |
81
|
|
|
"Inferno", |
82
|
|
|
"Teleportation", |
83
|
|
|
"Interdimensional travel" |
84
|
|
|
] |
85
|
|
|
}, |
86
|
|
|
{ |
87
|
|
|
"name": "Madame Uppercut", |
88
|
|
|
"age": 39, |
89
|
|
|
"secretIdentity": "Jane Wilson", |
90
|
|
|
"powers": [ |
91
|
|
|
"Million tonne punch", |
92
|
|
|
"Damage resistance", |
93
|
|
|
"Superhuman reflexes" |
94
|
|
|
] |
95
|
|
|
}, |
96
|
|
|
{ |
97
|
|
|
"name": "Eternal Flame", |
98
|
|
|
"age": 1000000, |
99
|
|
|
"secretIdentity": "Unknown", |
100
|
|
|
"powers": [ |
101
|
|
|
"Immortality", |
102
|
|
|
"Heat Immunity", |
103
|
|
|
"Inferno", |
104
|
|
|
"Teleportation", |
105
|
|
|
"Interdimensional travel" |
106
|
|
|
] |
107
|
|
|
}, |
108
|
|
|
{ |
109
|
|
|
"name": "Madame Uppercut", |
110
|
|
|
"age": 39, |
111
|
|
|
"secretIdentity": "Jane Wilson", |
112
|
|
|
"powers": [ |
113
|
|
|
"Million tonne punch", |
114
|
|
|
"Damage resistance", |
115
|
|
|
"Superhuman reflexes" |
116
|
|
|
] |
117
|
|
|
}, |
118
|
|
|
{ |
119
|
|
|
"name": "Eternal Flame", |
120
|
|
|
"age": 1000000, |
121
|
|
|
"secretIdentity": "Unknown", |
122
|
|
|
"powers": [ |
123
|
|
|
"Immortality", |
124
|
|
|
"Heat Immunity", |
125
|
|
|
"Inferno", |
126
|
|
|
"Teleportation", |
127
|
|
|
"Interdimensional travel" |
128
|
|
|
] |
129
|
|
|
}, |
130
|
|
|
{ |
131
|
|
|
"name": "Madame Uppercut", |
132
|
|
|
"age": 39, |
133
|
|
|
"secretIdentity": "Jane Wilson", |
134
|
|
|
"powers": [ |
135
|
|
|
"Million tonne punch", |
136
|
|
|
"Damage resistance", |
137
|
|
|
"Superhuman reflexes" |
138
|
|
|
] |
139
|
|
|
}, |
140
|
|
|
{ |
141
|
|
|
"name": "Eternal Flame", |
142
|
|
|
"age": 1000000, |
143
|
|
|
"secretIdentity": "Unknown", |
144
|
|
|
"powers": [ |
145
|
|
|
"Immortality", |
146
|
|
|
"Heat Immunity", |
147
|
|
|
"Inferno", |
148
|
|
|
"Teleportation", |
149
|
|
|
"Interdimensional travel" |
150
|
|
|
] |
151
|
|
|
}, |
152
|
|
|
{ |
153
|
|
|
"name": "Madame Uppercut", |
154
|
|
|
"age": 39, |
155
|
|
|
"secretIdentity": "Jane Wilson", |
156
|
|
|
"powers": [ |
157
|
|
|
"Million tonne punch", |
158
|
|
|
"Damage resistance", |
159
|
|
|
"Superhuman reflexes" |
160
|
|
|
] |
161
|
|
|
}, |
162
|
|
|
{ |
163
|
|
|
"name": "Eternal Flame", |
164
|
|
|
"age": 1000000, |
165
|
|
|
"secretIdentity": "Unknown", |
166
|
|
|
"powers": [ |
167
|
|
|
"Immortality", |
168
|
|
|
"Heat Immunity", |
169
|
|
|
"Inferno", |
170
|
|
|
"Teleportation", |
171
|
|
|
"Interdimensional travel" |
172
|
|
|
] |
173
|
|
|
}, |
174
|
|
|
{ |
175
|
|
|
"name": "Madame Uppercut", |
176
|
|
|
"age": 39, |
177
|
|
|
"secretIdentity": "Jane Wilson", |
178
|
|
|
"powers": [ |
179
|
|
|
"Million tonne punch", |
180
|
|
|
"Damage resistance", |
181
|
|
|
"Superhuman reflexes" |
182
|
|
|
] |
183
|
|
|
}, |
184
|
|
|
{ |
185
|
|
|
"name": "Eternal Flame", |
186
|
|
|
"age": 1000000, |
187
|
|
|
"secretIdentity": "Unknown", |
188
|
|
|
"powers": [ |
189
|
|
|
"Immortality", |
190
|
|
|
"Heat Immunity", |
191
|
|
|
"Inferno", |
192
|
|
|
"Teleportation", |
193
|
|
|
"Interdimensional travel" |
194
|
|
|
] |
195
|
|
|
} |
196
|
|
|
] |
197
|
|
|
}'; |
198
|
|
|
|
199
|
|
|
$client = new WebSocketClient($this->url, new ClientConfig()); |
200
|
|
|
try { |
201
|
|
|
$client->send($recvMsg); |
202
|
|
|
} catch (BadOpcodeException $e) { |
203
|
|
|
echo 'Couldn`t sent: ' . $e->getMessage(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$recv = $client->receive(); |
207
|
|
|
$this->assertEquals($recv, $recvMsg); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @test |
212
|
|
|
* @throws \Exception |
213
|
|
|
*/ |
214
|
|
|
public function it_sends_with_headers_via_constructor() |
215
|
|
|
{ |
216
|
|
|
$config = new ClientConfig(); |
217
|
|
|
$config->setFragmentSize(8096); |
218
|
|
|
$config->setTimeout(15); |
219
|
|
|
$config->setHeaders([ |
220
|
|
|
'X-Custom-Header' => 'Foo Bar Baz', |
221
|
|
|
// 'Origin' => 'example.com' |
222
|
|
|
]); |
223
|
|
|
|
224
|
|
|
$recvMsg = '{"user_id" : 123}'; |
225
|
|
|
$client = new WebSocketClient($this->url, $config); |
226
|
|
|
|
227
|
|
|
try { |
228
|
|
|
$client->send($recvMsg); |
229
|
|
|
} catch (BadOpcodeException $e) { |
230
|
|
|
echo 'Couldn`t sent: ' . $e->getMessage(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$recv = $client->receive(); |
234
|
|
|
$this->assertEquals($recv, $recvMsg); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @test |
239
|
|
|
* @throws \Exception |
240
|
|
|
*/ |
241
|
|
|
public function it_closes_connection() |
242
|
|
|
{ |
243
|
|
|
$client = new WebSocketClient($this->url, new ClientConfig()); |
244
|
|
|
|
245
|
|
|
$closeRecv = $client->close(); |
246
|
|
|
$this->assertEmpty($closeRecv); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|