Completed
Push — master ( 2cecc7...7c9e4e )
by Arthur
04:46
created

it_sends_with_headers_via_setters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace WSSCTEST;
4
5
use PHPUnit\Framework\TestCase;
6
use WSSC\Exceptions\BadOpcodeException;
7
use WSSC\WebSocketClient;
8
9
class WebSocketClientTest extends TestCase
10
{
11
12
    const WS_SCHEME = 'ws://';
13
    const WS_HOST   = 'localhost';
14
    const WS_PORT   = ':8000';
15
    const WS_URI    = '/notifications/messanger/vkjsndfvjn23243';
16
17
    private $url;
18
19
    public function setUp()/* The :void return type declaration that should be here would cause a BC issue */
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
        echo 'Running client...' . PHP_EOL;
31
        $recvMsg = '{"user_id" : 123}';
32
        $client = new WebSocketClient($this->url);
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
        $recvMsg = '{"user_id" : 123}';
49
        $client = new WebSocketClient($this->url, [
50
            'timeout'       => 15,
51
            'fragment_size' => 8096,
52
            'headers'       => [
53
                'X-Custom-Header' => 'Foo Bar Baz',
54
            ],
55
        ]);
56
57
        try {
58
            $client->send($recvMsg);
59
        } catch (BadOpcodeException $e) {
60
            echo 'Couldn`t sent: ' . $e->getMessage();
61
        }
62
        $recv = $client->receive();
63
        $this->assertEquals($recv, $recvMsg);
64
    }
65
66
    /**
67
     * @test
68
     * @throws \Exception
69
     */
70
    public function it_sends_with_headers_via_setters()
71
    {
72
        $recvMsg = '{"user_id" : 123}';
73
        $client = new WebSocketClient($this->url);
74
        $client->setFragmentSize(8096)->setTimeout(15)->setHeaders([
75
            'X-Custom-Header' => 'Foo Bar Baz'
76
        ]);
77
78
        try {
79
            $client->send($recvMsg);
80
        } catch (BadOpcodeException $e) {
81
            echo 'Couldn`t sent: ' . $e->getMessage();
82
        }
83
        $recv = $client->receive();
84
        $this->assertEquals($recv, $recvMsg);
85
    }
86
}
87