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
|
|
|
|
13
|
|
|
const WS_SCHEME = 'ws://'; |
14
|
|
|
const WS_HOST = 'localhost'; |
15
|
|
|
const WS_PORT = ':8000'; |
16
|
|
|
const WS_URI = '/notifications/messanger/vkjsndfvjn23243'; |
17
|
|
|
|
18
|
|
|
private $url; |
19
|
|
|
|
20
|
|
|
public function setUp()/* The :void return type declaration that should be here would cause a BC issue */ |
21
|
|
|
{ |
22
|
|
|
$this->url = self::WS_SCHEME . self::WS_HOST . self::WS_PORT . self::WS_URI; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
* @throws \Exception |
28
|
|
|
*/ |
29
|
|
|
public function is_client_connected() |
30
|
|
|
{ |
31
|
|
|
$recvMsg = '{"user_id" : 123}'; |
32
|
|
|
$client = new WebSocketClient($this->url, new ClientConfig()); |
33
|
|
|
try { |
34
|
|
|
$client->send($recvMsg); |
35
|
|
|
} catch (BadOpcodeException $e) { |
36
|
|
|
echo 'Couldn`t sent: ' . $e->getMessage(); |
37
|
|
|
} |
38
|
|
|
$recv = $client->receive(); |
39
|
|
|
$this->assertEquals($recv, $recvMsg); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
public function it_sends_with_headers_via_constructor() |
47
|
|
|
{ |
48
|
|
|
$config = new ClientConfig(); |
49
|
|
|
$config->setFragmentSize(8096); |
50
|
|
|
$config->setTimeout(15); |
51
|
|
|
$config->setHeaders([ |
52
|
|
|
'X-Custom-Header' => 'Foo Bar Baz', |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$recvMsg = '{"user_id" : 123}'; |
56
|
|
|
$client = new WebSocketClient($this->url, $config); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$client->send($recvMsg); |
60
|
|
|
} catch (BadOpcodeException $e) { |
61
|
|
|
echo 'Couldn`t sent: ' . $e->getMessage(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$recv = $client->receive(); |
65
|
|
|
$this->assertEquals($recv, $recvMsg); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
|
|
public function it_closes_connection() |
73
|
|
|
{ |
74
|
|
|
$client = new WebSocketClient($this->url, new ClientConfig()); |
75
|
|
|
|
76
|
|
|
$closeRecv = $client->close(); |
77
|
|
|
$this->assertEmpty($closeRecv); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|