Initial commit
This commit is contained in:
82
tests/ProtocolStreamTest.php
Normal file
82
tests/ProtocolStreamTest.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user