Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jul 2, 2024
1 parent 2e1219d commit e771f86
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions tests/PushServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

use Exception;
use Tests\MockClass\MockTcpConnection;
use Workbunny\WebmanPushServer\Events\Ping;
use Workbunny\WebmanPushServer\PushServer;
use const Workbunny\WebmanPushServer\EVENT_CONNECTION_ESTABLISHED;
use const Workbunny\WebmanPushServer\EVENT_PONG;

class PushServerBaseTest extends BaseTestCase
{
/**
* 测试server初始化
* @throws Exception
* @return void
*/
public function testPushServerOnConnect()
{
Expand Down Expand Up @@ -55,6 +57,7 @@ public function testPushServerOnConnect()
$this->assertTrue(
is_callable(PushServer::getConnectionProperty($connection, 'onWebSocketConnect', 'has-not'))
);
$this->assertTrue(!$connection->getSendBuffer());
// 模拟调用$connection->onWebSocketConnect
call_user_func(PushServer::getConnectionProperty($connection, 'onWebSocketConnect'), $connection, $this->getWebsocketHeader());
// 断言判定
Expand All @@ -73,7 +76,43 @@ public function testPushServerOnConnect()
$this->assertEquals(
[], PushServer::getConnectionProperty($connection, 'channels', 'has-not')
);
// EVENT_CONNECTION_ESTABLISHED事件回复
$this->assertEquals(EVENT_CONNECTION_ESTABLISHED, @json_decode($connection->getSendBuffer(), true)['event'] ?? null);
}

/**
* @return void
*/
public function testPushServerOnMessage()
{
// 初始化一个mock tcp连接
$connection = new MockTcpConnection();
// 模拟onConnect
$this->getPushServer()->onConnect($connection);
// 断言检测心跳计数为0
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
);
// 模拟未心跳,累计计数
PushServer::setConnectionProperty($connection, 'clientNotSendPingCount', 1);
// 断言检测心跳累计为1
$this->assertEquals(
1, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
);
// 断言检测事件初始为null
$this->assertNull($this->getPushServer()->getLastEvent());
// 断言检测初始buffer为空
$this->assertTrue(!$connection->getSendBuffer());
// 模拟心跳
$this->getPushServer()->onMessage($connection, '{"event":"pusher:ping"}');
// 断言检测心跳累计为0
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
);
// 断言检测事件为ping
$this->assertTrue(($this->getPushServer()->getLastEvent()) instanceof Ping);
// 断言检测回执buffer为pong事件
$this->assertEquals(EVENT_PONG, @json_decode($connection->getSendBuffer(), true)['event'] ?? null);
}

}

0 comments on commit e771f86

Please sign in to comment.