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: 李铭昕 <715557344@qq.com>
This commit is contained in:
fsl 2020-09-10 14:34:50 +08:00 committed by GitHub
parent 94fdaddc62
commit b1fc0cd595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 3 deletions

View File

@ -10,6 +10,7 @@
## Fixed
- [#2464](https://github.com/hyperf/hyperf/pull/2464) Fixed method `fill` does not works for camel case model.
- [#2478](https://github.com/hyperf/hyperf/pull/2478) Fixed `Sender::check` does not works when the checked fd not belong to websocket.
## Optimized

View File

@ -295,7 +295,8 @@
"HyperfTest\\Validation\\": "src/validation/tests/",
"HyperfTest\\View\\": "src/view/tests/",
"HyperfTest\\Watcher\\": "src/watcher/tests/",
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/"
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/",
"HyperfTest\\WebSocketServer\\": "src/websocket-server/tests/"
}
},
"config": {

View File

@ -40,6 +40,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\WebSocketServer\\": "tests/"
}
},
"config": {

View File

@ -102,7 +102,7 @@ class Sender
{
$info = $this->getServer()->connection_info($fd);
if ($info && $info['websocket_status'] === WEBSOCKET_STATUS_ACTIVE) {
if (($info['websocket_status'] ?? null) === WEBSOCKET_STATUS_ACTIVE) {
return true;
}

View File

@ -9,14 +9,17 @@ declare(strict_types=1);
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\WebSocketServer;
use Hyperf\Utils\Context as CoContext;
use Hyperf\WebSocketServer\Context;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ContextTest extends \PHPUnit\Framework\TestCase
class ContextTest extends TestCase
{
public function testHas()
{

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\WebSocketServer;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\WebSocketServer\Sender;
use HyperfTest\ModelCache\Stub\StdoutLogger;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @internal
* @coversNothing
*/
class SenderTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
}
public function testSenderCheck()
{
$container = $this->getContainer();
$server = Mockery::mock(\Swoole\Server::class);
$server->shouldReceive('connection_info')->once()->andReturn(false);
$server->shouldReceive('connection_info')->once()->andReturn([]);
$server->shouldReceive('connection_info')->once()->andReturn(['websocket_status' => WEBSOCKET_STATUS_CLOSING]);
$server->shouldReceive('connection_info')->once()->andReturn(['websocket_status' => WEBSOCKET_STATUS_ACTIVE]);
$container->shouldReceive('get')->with(\Swoole\Server::class)->andReturn($server);
$sender = new Sender($container);
$this->assertFalse($sender->check(1));
$this->assertFalse($sender->check(1));
$this->assertFalse($sender->check(1));
$this->assertTrue($sender->check(1));
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([]));
return $container;
}
}