2024-02-21 21:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace NoccyLabs\React\WebSocket;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
use React\Http\Message\ServerRequest;
|
|
|
|
|
|
|
|
#[CoversClass(WebSocketMiddleware::class)]
|
2024-02-23 21:44:39 +00:00
|
|
|
#[CoversClass(WebSocketConnection::class)]
|
2024-02-21 21:26:06 +00:00
|
|
|
class WebSocketMiddlewareTest extends \PHPUnit\Framework\TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testThatRegularRequestsAreIgnored()
|
|
|
|
{
|
|
|
|
|
|
|
|
$mock = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
|
|
|
$mock->expects($this->never())
|
|
|
|
->method('test');
|
|
|
|
|
|
|
|
$next = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
|
|
|
$next->expects($this->once())
|
|
|
|
->method('test');
|
|
|
|
|
|
|
|
$request = new ServerRequest("GET", "/", [], "");
|
|
|
|
|
|
|
|
$middleware = new WebSocketMiddleware();
|
|
|
|
$middleware->on('connection', [$mock,"test"]);
|
|
|
|
$middleware->__invoke($request, [$next,"test"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThatWebSocketRequestsAreHandled()
|
|
|
|
{
|
|
|
|
|
|
|
|
$mock = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
|
|
|
$mock->expects($this->once())
|
|
|
|
->method('test');
|
|
|
|
|
|
|
|
$next = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
|
|
|
$next->expects($this->never())
|
|
|
|
->method('test');
|
|
|
|
|
|
|
|
$request = new ServerRequest("GET", "/", [
|
|
|
|
'upgrade' => [ 'websocket' ]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$middleware = new WebSocketMiddleware();
|
|
|
|
$middleware->on('connection', [$mock,"test"]);
|
|
|
|
$middleware->__invoke($request, [$next,"test"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class InvokableDummy
|
|
|
|
{
|
|
|
|
public function test()
|
|
|
|
{
|
|
|
|
}
|
2024-02-23 21:44:39 +00:00
|
|
|
}
|