82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace NoccyLabs\React\Protocol;
|
||
|
|
|
||
|
|
use NoccyLabs\React\Protocol\Json\JsonProtocol;
|
||
|
|
use NoccyLabs\React\Protocol\Line\LineProtocol;
|
||
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||
|
|
use React\EventLoop\Loop;
|
||
|
|
use React\Stream\CompositeStream;
|
||
|
|
use React\Stream\ThroughStream;
|
||
|
|
|
||
|
|
#[CoversClass(ProtocolStream::class)]
|
||
|
|
#[CoversClass(LineProtocol::class)]
|
||
|
|
#[CoversClass(JsonProtocol::class)]
|
||
|
|
class ProtocolStreamTest extends \PHPUnit\Framework\TestCase
|
||
|
|
{
|
||
|
|
|
||
|
|
public function testSendingFrames()
|
||
|
|
{
|
||
|
|
$is = new ThroughStream();
|
||
|
|
$os = new ThroughStream();
|
||
|
|
$cs = new CompositeStream($is, $os);
|
||
|
|
|
||
|
|
$proto = new LineProtocol();
|
||
|
|
$stream = new ProtocolStream($cs, $proto);
|
||
|
|
|
||
|
|
$written = null;
|
||
|
|
$os->on("data", function ($v) use (&$written) { $written = $v; });
|
||
|
|
|
||
|
|
$stream->send(["helloworld"]);
|
||
|
|
|
||
|
|
$this->assertEquals("helloworld\n", $written);
|
||
|
|
|
||
|
|
$cs->close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testReceivingFrames()
|
||
|
|
{
|
||
|
|
$is = new ThroughStream();
|
||
|
|
$os = new ThroughStream();
|
||
|
|
$cs = new CompositeStream($is, $os);
|
||
|
|
$ws = new CompositeStream($os, $is);
|
||
|
|
|
||
|
|
$proto = new LineProtocol();
|
||
|
|
$stream = new ProtocolStream($cs, $proto);
|
||
|
|
|
||
|
|
$read = null;
|
||
|
|
$stream->on("message", function ($v) use (&$read) { $read = $v; });
|
||
|
|
|
||
|
|
$ws->write("helloworld\n");
|
||
|
|
|
||
|
|
$this->assertEquals([ "helloworld" ], $read);
|
||
|
|
|
||
|
|
$cs->close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testPartialFrames()
|
||
|
|
{
|
||
|
|
$is = new ThroughStream();
|
||
|
|
$os = new ThroughStream();
|
||
|
|
$cs = new CompositeStream($is, $os);
|
||
|
|
$ws = new CompositeStream($os, $is);
|
||
|
|
|
||
|
|
$proto = new JsonProtocol(
|
||
|
|
frameSeparator: "\n"
|
||
|
|
);
|
||
|
|
$stream = new ProtocolStream($cs, $proto);
|
||
|
|
|
||
|
|
$read = null;
|
||
|
|
$stream->on("message", function ($v) use (&$read) { $read = $v; });
|
||
|
|
|
||
|
|
$ws->write('{"hello":');
|
||
|
|
$ws->write('"world"}');
|
||
|
|
$ws->write("\n");
|
||
|
|
|
||
|
|
$this->assertEquals([ "hello" => "world" ], $read);
|
||
|
|
|
||
|
|
$cs->close();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|