|
2 | 2 |
|
3 | 3 | namespace ekstazi\websocket\client\amphp\test;
|
4 | 4 |
|
5 |
| -use Amp\ByteStream\Payload; |
6 | 5 | use Amp\PHPUnit\AsyncTestCase;
|
7 |
| -use Amp\Success; |
| 6 | +use Amp\Websocket\Client; |
8 | 7 | use ekstazi\websocket\client\amphp\Connection;
|
9 |
| -use ekstazi\websocket\common\Connection as BaseConnection; |
| 8 | +use ekstazi\websocket\common\Writer; |
10 | 9 |
|
11 | 10 | class ConnectionTest extends AsyncTestCase
|
12 | 11 | {
|
13 |
| - |
14 |
| - /** |
15 |
| - * @param Payload $data |
16 |
| - * @return BaseConnection |
17 |
| - */ |
18 |
| - private function stubRead(Payload $data = null): BaseConnection |
19 |
| - { |
20 |
| - $connection = $this->createMock(BaseConnection::class); |
21 |
| - $connection |
22 |
| - ->expects(self::once()) |
23 |
| - ->method('read') |
24 |
| - ->willReturn(new Success($data)); |
25 |
| - return $connection; |
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * Test that data readed from websocket client. |
30 |
| - * @return \Generator |
31 |
| - * @throws |
32 |
| - */ |
33 |
| - public function testRead() |
| 12 | + public function testCreate() |
34 | 13 | {
|
35 |
| - $client = $this->createMock(BaseConnection::class); |
36 |
| - $client->expects(self::once()) |
37 |
| - ->method('read') |
38 |
| - ->willReturn(new Success('test')); |
39 |
| - |
40 |
| - $connection = new Connection($client); |
41 |
| - $data = yield $connection->read(); |
42 |
| - self::assertEquals('test', $data); |
43 |
| - } |
44 |
| - |
45 |
| - /** |
46 |
| - * Test write method with data and different modes. |
47 |
| - * @return \Generator |
48 |
| - * @throws |
49 |
| - */ |
50 |
| - public function testWrite() |
51 |
| - { |
52 |
| - $client = $this->createMock(BaseConnection::class); |
53 |
| - $client->expects(self::once()) |
54 |
| - ->method('write') |
55 |
| - ->with('test', Connection::MODE_BINARY) |
56 |
| - ->willReturn(new Success()); |
57 |
| - |
58 |
| - $connection = new Connection($client); |
59 |
| - yield $connection->write('test', Connection::MODE_BINARY); |
| 14 | + $client = $this->createClient(); |
| 15 | + $stream = Connection::create($client, Writer::MODE_BINARY); |
| 16 | + self::assertInstanceOf(Connection::class, $stream); |
| 17 | + self::assertEquals(Writer::MODE_BINARY, $stream->getDefaultMode()); |
60 | 18 | }
|
61 | 19 |
|
62 | 20 | /**
|
63 |
| - * @return \Generator |
64 |
| - * @throws |
| 21 | + * @return Client |
65 | 22 | */
|
66 |
| - public function testEnd() |
| 23 | + private function createClient(): Client |
67 | 24 | {
|
68 |
| - $client = $this->createMock(BaseConnection::class); |
69 |
| - $client->expects(self::once()) |
70 |
| - ->method('end') |
71 |
| - ->with('test', Connection::MODE_BINARY) |
72 |
| - ->willReturn(new Success()); |
73 |
| - |
74 |
| - $connection = new Connection($client); |
75 |
| - yield $connection->end('test', Connection::MODE_BINARY); |
| 25 | + return $this->createStub(Client::class); |
76 | 26 | }
|
77 | 27 |
|
78 |
| - public function testSetDefaultMode() |
79 |
| - { |
80 |
| - $client = $this->createMock(BaseConnection::class); |
81 |
| - $client->expects(self::once()) |
82 |
| - ->method('setDefaultMode') |
83 |
| - ->with(Connection::MODE_BINARY); |
84 |
| - |
85 |
| - $connection = new Connection($client); |
86 |
| - $connection->setDefaultMode(Connection::MODE_BINARY); |
87 |
| - } |
88 |
| - |
89 |
| - public function testGetDefaultMode() |
90 |
| - { |
91 |
| - $client = $this->createMock(BaseConnection::class); |
92 |
| - $client->expects(self::once()) |
93 |
| - ->method('getDefaultMode') |
94 |
| - ->willReturn(Connection::MODE_BINARY); |
95 |
| - |
96 |
| - $connection = new Connection($client); |
97 |
| - self::assertEquals(Connection::MODE_BINARY, $connection->getDefaultMode()); |
98 |
| - } |
99 |
| - |
100 |
| - public function testGetRemoteAddress() |
101 |
| - { |
102 |
| - $client = $this->createMock(BaseConnection::class); |
103 |
| - $client->expects(self::once()) |
104 |
| - ->method('getRemoteAddress') |
105 |
| - ->willReturn('127.0.0.2'); |
106 |
| - |
107 |
| - $connection = new Connection($client); |
108 |
| - self::assertEquals('127.0.0.2', $connection->getRemoteAddress()); |
109 |
| - } |
110 |
| - |
111 |
| - public function testGetId() |
112 |
| - { |
113 |
| - $client = $this->createMock(BaseConnection::class); |
114 |
| - $client->expects(self::once()) |
115 |
| - ->method('getId') |
116 |
| - ->willReturn(1); |
117 |
| - |
118 |
| - $connection = new Connection($client); |
119 |
| - self::assertEquals(1, $connection->getId()); |
120 |
| - } |
121 | 28 | }
|
0 commit comments