Skip to content

Commit 40d06db

Browse files
committed
Updated to new version
1 parent 2b06ccc commit 40d06db

File tree

4 files changed

+28
-157
lines changed

4 files changed

+28
-157
lines changed

src/Connection.php

+15-43
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,26 @@
22

33
namespace ekstazi\websocket\client\amphp;
44

5-
use Amp\Promise;
5+
use Amp\Websocket\Client;
66
use ekstazi\websocket\client\Connection as ConnectionInterface;
7-
use ekstazi\websocket\common\Connection as BaseConnection;
7+
use ekstazi\websocket\common\amphp\Reader;
8+
use ekstazi\websocket\common\amphp\Writer;
9+
use ekstazi\websocket\common\internal\Connection as BaseConnection;
810

9-
final class Connection implements ConnectionInterface
11+
final class Connection extends BaseConnection implements ConnectionInterface
1012
{
13+
1114
/**
12-
* @var BaseConnection
15+
* Create stream from client.
16+
* @param Client $client
17+
* @param string $defaultMode
18+
* @return Connection
1319
*/
14-
private $connection;
15-
16-
public function __construct(BaseConnection $connection)
17-
{
18-
$this->connection = $connection;
19-
}
20-
21-
public function getId(): int
22-
{
23-
return $this->connection->getId();
24-
}
25-
26-
public function getRemoteAddress(): string
27-
{
28-
return $this->connection->getRemoteAddress();
29-
}
30-
31-
public function read(): Promise
32-
{
33-
return $this->connection->read();
34-
}
35-
36-
public function setDefaultMode(string $defaultMode): void
37-
{
38-
$this->connection->setDefaultMode($defaultMode);
39-
}
40-
41-
public function getDefaultMode(): string
42-
{
43-
return $this->connection->getDefaultMode();
44-
}
45-
46-
public function write(string $data, string $mode = null): Promise
47-
{
48-
return $this->connection->write($data, $mode);
49-
}
50-
51-
public function end(string $finalData = "", string $mode = null): Promise
20+
public static function create(Client $client, string $defaultMode = Writer::MODE_BINARY): self
5221
{
53-
return $this->connection->end($finalData, $mode);
22+
return new static(
23+
new Reader($client),
24+
new Writer($client, $defaultMode)
25+
);
5426
}
5527
}

src/Connector.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public function connect(RequestInterface $request, string $defaultMode = Connect
3737
$options = $options ?? $this->defaultOptions;
3838
$handshake = new Handshake($request->getUri(), $options);
3939
$client = yield $this->connector->connect($handshake->withHeaders($request->getHeaders()));
40-
$adapter = BaseConnection::create($client, $defaultMode);
41-
return new Connection($adapter);
40+
return Connection::create($client, $defaultMode);
4241
});
4342
}
4443
}

test/ConnectionTest.php

+10-103
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,27 @@
22

33
namespace ekstazi\websocket\client\amphp\test;
44

5-
use Amp\ByteStream\Payload;
65
use Amp\PHPUnit\AsyncTestCase;
7-
use Amp\Success;
6+
use Amp\Websocket\Client;
87
use ekstazi\websocket\client\amphp\Connection;
9-
use ekstazi\websocket\common\Connection as BaseConnection;
8+
use ekstazi\websocket\common\Writer;
109

1110
class ConnectionTest extends AsyncTestCase
1211
{
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()
3413
{
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());
6018
}
6119

6220
/**
63-
* @return \Generator
64-
* @throws
21+
* @return Client
6522
*/
66-
public function testEnd()
23+
private function createClient(): Client
6724
{
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);
7626
}
7727

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-
}
12128
}

test/ConnectorTest.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,9 @@ private function stubRequest(): RequestInterface
3737
return $request;
3838
}
3939

40-
private function stubClient($asDefault = true): Client
40+
private function stubClient(): Client
4141
{
42-
$client = $this->createMock(Client::class);
43-
$client->expects($asDefault ? self::once() : self::never())
44-
->method('getRemoteAddress')
45-
->willReturn(new SocketAddress('127.0.0.2', 8000));
46-
$client->expects($asDefault ? self::once() : self::never())
47-
->method('getId')
48-
->willReturn(1);
49-
return $client;
42+
return $this->createMock(Client::class);
5043
}
5144

5245

0 commit comments

Comments
 (0)