Added websocket client test.

This commit is contained in:
李铭昕 2019-06-26 11:09:47 +08:00
parent 9be7cc316b
commit c52216ef17
5 changed files with 92 additions and 16 deletions

View File

@ -2,7 +2,7 @@
## Added
- Nothing.
- Added WebSocket Client.
## Changed

View File

@ -83,7 +83,9 @@
"hyperf/redis": "self.version",
"hyperf/server": "self.version",
"hyperf/tracer": "self.version",
"hyperf/utils": "self.version"
"hyperf/utils": "self.version",
"hyperf/websocket-client": "self.version",
"hyperf/websocket-server": "self.version"
},
"suggest": {
},
@ -128,20 +130,24 @@
"Hyperf\\Redis\\": "src/redis/src/",
"Hyperf\\Server\\": "src/server/src/",
"Hyperf\\Tracer\\": "src/tracer/src/",
"Hyperf\\Utils\\": "src/utils/src/"
"Hyperf\\Utils\\": "src/utils/src/",
"Hyperf\\WebSocketClient\\": "src/websocket-client/src/",
"Hyperf\\WebSocketServer\\": "src/websocket-server/src/"
}
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\ConfigApollo\\": "./src/config-apollo/tests",
"HyperfTest\\Consul\\": "./src/consul/tests",
"HyperfTest\\Di\\": "./src/di/tests",
"HyperfTest\\Dispatcher\\": "./src/dispatcher/test",
"HyperfTest\\Database\\": "./src/database/tests",
"HyperfTest\\DbConnection\\": "./src/db-connection/tests",
"HyperfTest\\Event\\": "./src/event/tests",
"HyperfTest\\Redis\\": "./src/redis/tests",
"HyperfTest\\Utils\\": "./src/utils/tests"
"HyperfTest\\ConfigApollo\\": "./src/config-apollo/tests/",
"HyperfTest\\Consul\\": "./src/consul/tests/",
"HyperfTest\\Di\\": "./src/di/tests/",
"HyperfTest\\Dispatcher\\": "./src/dispatcher/test/",
"HyperfTest\\Database\\": "./src/database/tests/",
"HyperfTest\\DbConnection\\": "./src/db-connection/tests/",
"HyperfTest\\Event\\": "./src/event/tests/",
"HyperfTest\\Redis\\": "./src/redis/tests/",
"HyperfTest\\Utils\\": "./src/utils/tests/",
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/",
"HyperfTest\\WebSocketServer\\": "src/websocket-server/tests/"
}
},
"config": {
@ -178,7 +184,9 @@
"Hyperf\\Redis\\ConfigProvider",
"Hyperf\\Server\\ConfigProvider",
"Hyperf\\Tracer\\ConfigProvider",
"Hyperf\\Utils\\ConfigProvider"
"Hyperf\\Utils\\ConfigProvider",
"Hyperf\\WebSocketClient\\ConfigProvider",
"Hyperf\\WebSocketServer\\ConfigProvider"
]
}
},

View File

@ -15,6 +15,7 @@ namespace Hyperf\WebSocketClient;
use Hyperf\WebSocketClient\Exception\ConnectException;
use Psr\Http\Message\UriInterface;
use Swoole\Coroutine;
use Swoole\WebSocket\Frame as SwFrame;
class Client
{
@ -59,8 +60,11 @@ class Client
public function recv(float $timeout = -1)
{
$ret = $this->client->recv($timeout);
var_dump($ret instanceof \Swoole\WebSocket\Frame);
return $ret ? new Frame($ret) : $ret;
if ($ret instanceof SwFrame) {
return new Frame($ret);
}
return $ret;
}
public function push(string $data, int $opcode = WEBSOCKET_OPCODE_TEXT, bool $finish = true): bool

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\WebSocketClient;
use Hyperf\HttpMessage\Uri\Uri;
use Hyperf\WebSocketClient\Client;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ClientTest extends TestCase
{
/**
* @expectedException \Hyperf\WebSocketClient\Exception\ConnectException
*/
public function testClientConnectFailed()
{
new Client(new Uri('ws://172.168.1.1:9522'));
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\WebSocketClient;
use Hyperf\WebSocketClient\Frame;
use Mockery;
use PHPUnit\Framework\TestCase;
use Swoole\WebSocket\Frame as SwFrame;
/**
* @internal
* @coversNothing
*/
class FrameTest extends TestCase
{
public function testFrame()
{
$frame = new Frame(Mockery::mock(SwFrame::class));
$this->assertSame($frame->data, (string) $frame);
}
}