Skip to content

Commit

Permalink
update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Dec 25, 2023
1 parent a9f4a5e commit c743f35
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ jobs:
- 8.1
- 8.0
- 7.4
redis:
- "7"
- "6"
- "5"
steps:
- uses: actions/checkout@v3
- uses: supercharge/[email protected]
with:
redis-version: ${{ matrix.redis }}
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
Expand Down
8 changes: 2 additions & 6 deletions src/HookServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ class HookServer implements ServerInterface
/** @inheritDoc */
public static function getConfig(string $key, $default = null)
{
return Server::isDebug() ?
config('plugin.workbunny.webman-push-server.app.hook-server.' . $key, $default) :
\config('plugin.workbunny.webman-push-server.app.hook-server.' . $key, $default);
return config('plugin.workbunny.webman-push-server.app.hook-server.' . $key, $default);
}

/** @inheritDoc */
public static function getStorage(): \Redis
{
if(!self::$_storage instanceof \Redis){
self::$_storage = Server::isDebug() ?
new MockRedisStream() :
Redis::connection(self::getConfig('redis_channel', 'default'))->client();
self::$_storage = Redis::connection(self::getConfig('redis_channel', 'default'))->client();
}
return self::$_storage;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use RedisException;
use support\Container;
use support\Redis;
use Tests\MockClass\MockRedis;
use Workbunny\WebmanPushServer\Events\AbstractEvent;
use Workbunny\WebmanPushServer\Events\Unsubscribe;
use Workerman\Connection\TcpConnection;
Expand Down Expand Up @@ -184,17 +183,16 @@ public static function getServer(): ?Server
/** @inheritDoc */
public static function getConfig(string $key, $default = null)
{
return self::isDebug() ?
config('plugin.workbunny.webman-push-server.app.push-server.' . $key, $default) :
\config('plugin.workbunny.webman-push-server.app.push-server.' . $key, $default);
return \config(
'plugin.workbunny.webman-push-server.app.push-server.' . $key, $default
);
}

/** @inheritDoc */
public static function getStorage(): \Redis
{
if(!self::$_storage instanceof \Redis){
self::$_storage = self::isDebug() ?
new MockRedis() :
self::$_storage =
Redis::connection(self::getConfig('redis_channel', 'default'))->client();
}
return self::$_storage;
Expand Down
2 changes: 1 addition & 1 deletion src/config/plugin/workbunny/webman-push-server/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
$channels[$channel] = Server::getStorage()->hMGet($key, $fields) ?? [];
}
return response(200, ['channels' => $channels]);
}catch (\Throwable $throwable){
} catch (\Throwable $throwable) {
//TODO log
return response(500, 'Server Error [Channels]');
}
Expand Down
15 changes: 1 addition & 14 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ function response(int $httpStatus, $data, array $header = []): Response
}
}

if (!function_exists('config')){
/**
* @param string|null $key
* @param $default
* @return array|mixed|null
*/
function config(string $key = null, $default = null)
{
Config::load(__DIR__ . '/config', ['route']);
return Config::get($key, $default);
}
}

/**
* 生成UUID
*/
Expand Down Expand Up @@ -80,4 +67,4 @@ function fuuid(): string
$uuid .= substr($chars, 20, 12);
return $uuid;
}
}
}
13 changes: 9 additions & 4 deletions tests/ApiServiceRouteHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class ApiServiceRouteHandlerTest extends BaseTestCase
{

/**
* 测试消息事件处理
* @covers \Workbunny\WebmanPushServer\ApiService::onMessage
Expand All @@ -30,7 +31,6 @@ class ApiServiceRouteHandlerTest extends BaseTestCase
*/
public function testApiServiceChannels(){
$this->setServer(true);

Server::setServer($this->getServer());

// required auth_key
Expand Down Expand Up @@ -82,13 +82,14 @@ public function testApiServiceChannels(){
*/
public function testApiServiceChannel(){
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();
$request = new Http\Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=test HTTP/1.1\r\nConnection: keep-alive\r\n");
// 手动触发 onMessage 回调
Server::getServices(ApiService::class)->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
$this->assertEquals('{"occupied":true,"type":null}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals('{"occupied":true,"type":false}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode());
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type'));

Expand All @@ -97,7 +98,7 @@ public function testApiServiceChannel(){
// 手动触发 onMessage 回调
Server::getServices(ApiService::class)->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
$this->assertEquals('{"occupied":true,"type":null,"subscription_count":null}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals('{"occupied":true,"type":false,"subscription_count":false}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode());
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type'));

Expand All @@ -106,7 +107,7 @@ public function testApiServiceChannel(){
// 手动触发 onMessage 回调
Server::getServices(ApiService::class)->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
$this->assertEquals('{"occupied":true,"type":null,"subscription_count":null,"user_count":null}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals('{"occupied":true,"type":false,"subscription_count":false,"user_count":false}', $mockConnection->getSendBuffer()->rawBody());
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode());
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type'));
}
Expand All @@ -119,6 +120,7 @@ public function testApiServiceChannel(){
*/
public function testApiServiceEvents(){
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();
$request = new Http\Request("POST /apps/1/events?auth_key=workbunny&auth_signature=test HTTP/1.1\r\nConnection: keep-alive\r\n");
Expand All @@ -140,6 +142,7 @@ public function testApiServiceEvents(){
*/
public function testApiServiceBatchEvents(){
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();
$request = new Http\Request("POST /apps/1/batch_events?auth_key=workbunny&auth_signature=test HTTP/1.1\r\nConnection: keep-alive\r\n");
Expand All @@ -161,6 +164,7 @@ public function testApiServiceBatchEvents(){
*/
public function testApiServiceUsers(){
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();
$request = new Http\Request("GET /apps/1/channels/private-test/users?auth_key=workbunny&auth_signature=test HTTP/1.1\r\nConnection: keep-alive\r\n");
Expand All @@ -180,6 +184,7 @@ public function testApiServiceUsers(){
*/
public function testApiServiceTerminateConnections(){
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();
$request = new Http\Request("POST /apps/1/users/abc/terminate_connections?auth_key=workbunny&auth_signature=test HTTP/1.1\r\nConnection: keep-alive\r\n");
Expand Down
5 changes: 4 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Exception;
use PHPUnit\Framework\TestCase;
use support\Redis;
use Webman\Config;
use Workbunny\WebmanPushServer\ApiService;
use Workbunny\WebmanPushServer\Server;

Expand Down Expand Up @@ -64,7 +66,8 @@ protected function setServer(bool $clean = false): void
protected function setUp(): void
{
Server::$debug = true;
require_once dirname(__DIR__) . '/vendor/workerman/webman-framework/src/support/helpers.php';
require_once __DIR__ . '/debug.php';
Config::load(\config_path(), ['route']);
parent::setUp();
}
}
12 changes: 6 additions & 6 deletions tests/MockClass/MockRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public function hGet($key, $hashKey)
return $this->_storage[$key][$hashKey] ?? null;
}

public function del($key1, ...$otherKeys)
public function del($key, ...$otherKeys): \Redis|int|bool
{
if($this->exists($key1)){
unset($this->_storage[$key1]);
if($this->exists($key)){
unset($this->_storage[$key]);
}
foreach ($otherKeys as $key){
if($this->exists($key)){
unset($this->_storage[$key]);
foreach ($otherKeys as $k){
if($this->exists($k)){
unset($this->_storage[$k]);
}
}
}
Expand Down
26 changes: 23 additions & 3 deletions tests/ServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Tests\MockClass\MockTcpConnection;
use Workbunny\WebmanPushServer\ApiService;
use Workbunny\WebmanPushServer\HookServer;
use Workbunny\WebmanPushServer\Server;
use Workbunny\WebmanPushServer\ServerInterface;

class ServerBaseTest extends BaseTestCase
Expand Down Expand Up @@ -46,6 +47,7 @@ public function testServerInit(){
*/
public function testServerOnMessageWithNormalStringData(){
$this->setServer(true);
Server::setServer($this->getServer());

// normal string
$mockConnection = new MockTcpConnection();
Expand Down Expand Up @@ -89,6 +91,7 @@ public function testServerOnMessageWithNormalStringData(){
public function testServerOnConnectSuccessful()
{
$this->setServer(true);
Server::setServer($this->getServer());

$mockConnection = new MockTcpConnection();

Expand All @@ -107,12 +110,15 @@ public function testServerOnConnectSuccessful()
$this->assertEquals(['' => ''], $this->getServer()->_getConnectionProperty($mockConnection, 'channels'));
$this->assertEquals($mockConnection, $this->getServer()->_getConnection($mockConnection, $appKey, ''));

/** @var MockRedisStream $storage */
$storage = HookServer::getStorage();
// 队列新增一条数据
$this->assertCount(1, $queue = $storage->getStreams()['workbunny:webman-push-server:webhook-stream'] ?? []);
$this->assertEquals(1, $storage->exists('workbunny:webman-push-server:webhook-stream'));
// 队列包含一条server_event事件
$this->assertContains('server_event', array_column($queue, 'name'));
$this->assertContains('server_event', array_column($storage->xRead([
'workbunny:webman-push-server:webhook-stream' => '0-0'
], -1, 1)['workbunny:webman-push-server:webhook-stream'] ?? [], 'name'));
// 移除队列
$storage->del('workbunny:webman-push-server:webhook-stream');
}

/**
Expand All @@ -128,6 +134,7 @@ public function testServerOnConnectSuccessful()
public function testServerOnConnectInvalidAppError()
{
$this->setServer(true);
Server::setServer($this->getServer());

// 无效的header
$mockConnection = new MockTcpConnection();
Expand All @@ -140,6 +147,15 @@ public function testServerOnConnectInvalidAppError()
$this->assertEquals(0, $this->getServer()->_getConnectionProperty($mockConnection, 'clientNotSendPingCount'));
$this->assertTrue($mockConnection->isPaused());
$this->assertEquals('{"event":"pusher:error","data":{"code":null,"message":"Invalid app"}}', $mockConnection->getSendBuffer());
$storage = HookServer::getStorage();
// 队列新增一条数据
$this->assertEquals(1, $storage->exists('workbunny:webman-push-server:webhook-stream'));
// 队列包含一条server_event事件
$this->assertContains('server_event', array_column($storage->xRead([
'workbunny:webman-push-server:webhook-stream' => '0-0'
], -1, 1)['workbunny:webman-push-server:webhook-stream'] ?? [], 'name'));
// 移除队列
$storage->del('workbunny:webman-push-server:webhook-stream');
}

/**
Expand All @@ -155,6 +171,7 @@ public function testServerOnConnectInvalidAppError()
public function testServerOnConnectInvalidAppKeyError()
{
$this->setServer(true);
Server::setServer($this->getServer());

// 无效的akk_key
$mockConnection = new MockTcpConnection();
Expand All @@ -178,6 +195,7 @@ public function testServerOnConnectInvalidAppKeyError()
*/
public function testServerOnMessageWithBoolData(){
$this->setServer(true);
Server::setServer($this->getServer());

// bool
$mockConnection = new MockTcpConnection();
Expand All @@ -196,6 +214,7 @@ public function testServerOnMessageWithBoolData(){
*/
public function testServerOnMessageWithArrayData(){
$this->setServer(true);
Server::setServer($this->getServer());

// array
$mockConnection = new MockTcpConnection();
Expand All @@ -214,6 +233,7 @@ public function testServerOnMessageWithArrayData(){
*/
public function testServerOnMessageWithObjectData(){
$this->setServer(true);
Server::setServer($this->getServer());

// object
$mockConnection = new MockTcpConnection();
Expand Down
Loading

0 comments on commit c743f35

Please sign in to comment.