Skip to content

Commit bac2a9a

Browse files
Fixed Sender::check does not works when the checked fd not belong to websocket. (#2478)
* 修复Hyperf\WebSocketServer\Sender->check($fd)方法如果传入的不是websocket的fd会出现:Notice: Undefined index: websocket_status的问题 * Update Sender.php * Added test cases. * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <[email protected]>
1 parent e986083 commit bac2a9a

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"autoload-dev": {
4242
"psr-4": {
43+
"HyperfTest\\WebSocketServer\\": "tests/"
4344
}
4445
},
4546
"config": {

src/Sender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function check($fd): bool
102102
{
103103
$info = $this->getServer()->connection_info($fd);
104104

105-
if ($info && $info['websocket_status'] === WEBSOCKET_STATUS_ACTIVE) {
105+
if (($info['websocket_status'] ?? null) === WEBSOCKET_STATUS_ACTIVE) {
106106
return true;
107107
}
108108

tests/ContextTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
* @contact [email protected]
1010
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1111
*/
12+
namespace HyperfTest\WebSocketServer;
13+
1214
use Hyperf\Utils\Context as CoContext;
1315
use Hyperf\WebSocketServer\Context;
16+
use PHPUnit\Framework\TestCase;
1417

1518
/**
1619
* @internal
1720
* @coversNothing
1821
*/
19-
class ContextTest extends \PHPUnit\Framework\TestCase
22+
class ContextTest extends TestCase
2023
{
2124
public function testHas()
2225
{

tests/SenderTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\WebSocketServer;
13+
14+
use Hyperf\Config\Config;
15+
use Hyperf\Contract\ConfigInterface;
16+
use Hyperf\Contract\StdoutLoggerInterface;
17+
use Hyperf\Utils\ApplicationContext;
18+
use Hyperf\WebSocketServer\Sender;
19+
use HyperfTest\ModelCache\Stub\StdoutLogger;
20+
use Mockery;
21+
use PHPUnit\Framework\TestCase;
22+
use Psr\Container\ContainerInterface;
23+
24+
/**
25+
* @internal
26+
* @coversNothing
27+
*/
28+
class SenderTest extends TestCase
29+
{
30+
protected function tearDown()
31+
{
32+
Mockery::close();
33+
}
34+
35+
public function testSenderCheck()
36+
{
37+
$container = $this->getContainer();
38+
$server = Mockery::mock(\Swoole\Server::class);
39+
$server->shouldReceive('connection_info')->once()->andReturn(false);
40+
$server->shouldReceive('connection_info')->once()->andReturn([]);
41+
$server->shouldReceive('connection_info')->once()->andReturn(['websocket_status' => WEBSOCKET_STATUS_CLOSING]);
42+
$server->shouldReceive('connection_info')->once()->andReturn(['websocket_status' => WEBSOCKET_STATUS_ACTIVE]);
43+
$container->shouldReceive('get')->with(\Swoole\Server::class)->andReturn($server);
44+
$sender = new Sender($container);
45+
46+
$this->assertFalse($sender->check(1));
47+
$this->assertFalse($sender->check(1));
48+
$this->assertFalse($sender->check(1));
49+
$this->assertTrue($sender->check(1));
50+
}
51+
52+
protected function getContainer()
53+
{
54+
$container = Mockery::mock(ContainerInterface::class);
55+
ApplicationContext::setContainer($container);
56+
57+
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
58+
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([]));
59+
60+
return $container;
61+
}
62+
}

0 commit comments

Comments
 (0)