Initial commit

This commit is contained in:
Christopher Vagnetoft
2026-04-02 22:29:35 +02:00
commit 62a1167055
31 changed files with 3759 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace NoccyLabs\React\Protocol\Binary;
use NoccyLabs\React\Protocol\ProtocolException;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(BinaryProtocol::class)]
class BinaryProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testPackingFrames()
{
$proto = new BinaryProtocol(
frameSeparator: "\0"
);
$res1 = $proto->packFrame([ 'payload' => 'foobar' ]);
$this->assertEquals("foobar\0", $res1);
$proto = new BinaryProtocol(
frameSeparator: "",
prependSizeBytes: 2,
);
$res1 = $proto->packFrame([ 'payload' => 'foobar' ]);
$this->assertEquals("\x06\x00foobar", $res1);
}
public function testUnpackingFrames()
{
$proto = new BinaryProtocol(
frameSeparator: "\0"
);
$res1 = $proto->unpackFrame("foobar\0");
$this->assertEquals([ 'payload' => 'foobar' ], $res1);
$proto = new BinaryProtocol(
frameSeparator: "",
prependSizeBytes: 2,
);
$res1 = $proto->unpackFrame("\x06\x00foobar");
$this->assertEquals([ 'payload' => 'foobar' ], $res1);
}
public function testUnpackingIncompleteFrames()
{
$proto = new BinaryProtocol(
frameSeparator: "\0"
);
$res1 = $proto->unpackFrame("foobar\0");
$this->assertEquals([ 'payload' => 'foobar' ], $res1);
$proto = new BinaryProtocol(
frameSeparator: "",
prependSizeBytes: 2,
);
$this->expectException(ProtocolException::class);
$res0 = $proto->unpackFrame("\x06");
$this->expectException(ProtocolException::class);
$res1 = $proto->unpackFrame("\x00foo");
$res2 = $proto->unpackFrame("bar");
$this->assertNull($res1);
$this->assertEquals([ 'payload' => 'foobar' ], $res2);
}
public function testConsumingFrames()
{
$proto = new BinaryProtocol(
frameSeparator: "\0"
);
$res1 = $proto->unpackFrame("foobar\0");
$this->assertEquals([ 'payload' => 'foobar' ], $res1);
$proto = new BinaryProtocol(
frameSeparator: "",
prependSizeBytes: 2,
);
$buffer = "\x06\x00foobar\x06\x00foobaz";
$res1 = $proto->consumeFrame($buffer);
$res2 = $proto->consumeFrame($buffer);
$this->assertEquals([ 'payload' => 'foobar' ], $res1);
$this->assertEquals([ 'payload' => 'foobaz' ], $res2);
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace NoccyLabs\React\Protocol\Json;
use PHPUnit\Framework\Attributes\CoversClass;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
#[CoversClass(JsonProtocol::class)]
class JsonProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testFrameSeparators()
{
$proto = new JsonProtocol("\0");
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("{\"foo\":\"bar\"}\0", $packed);
$proto = new JsonProtocol("\n");
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("{\"foo\":\"bar\"}\n", $packed);
}
public function testFrameSizePrefixes()
{
$proto = new JsonProtocol(prependSizeBytes: 1);
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("\x0E{\"foo\":\"bar\"}\0", $packed);
$proto = new JsonProtocol(prependSizeBytes: 2, prependSizeEndian: 'l');
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("\x0E\x00{\"foo\":\"bar\"}\0", $packed);
$proto = new JsonProtocol(prependSizeBytes: 2, prependSizeEndian: 'b');
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("\x00\x0E{\"foo\":\"bar\"}\0", $packed);
$proto = new JsonProtocol(prependSizeBytes: 4, prependSizeEndian: 'l');
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("\x0E\x00\x00\x00{\"foo\":\"bar\"}\0", $packed);
$proto = new JsonProtocol(prependSizeBytes: 4, prependSizeEndian: 'b');
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("\x00\x00\x00\x0E{\"foo\":\"bar\"}\0", $packed);
}
public function testPackingCallbacks()
{
$proto = new JsonProtocol(
prependSizeBytes: 0,
beforePackCb: function (array $data) {
$data['a'] = true;
return $data;
},
afterPackCb: function (string $data) {
return $data."a";
},
);
$packed = $proto->packFrame(['foo' => 'bar']);
$this->assertEquals("{\"foo\":\"bar\",\"a\":true}\0a", $packed);
}
public function testUnpackingCallbacks()
{
$proto = new JsonProtocol(
prependSizeBytes: 0,
beforeUnpackCb: function (string $data) {
return strtolower($data);
},
afterUnpackCb: function (array $data) {
unset($data['a']);
return $data;
},
);
$unpacked = $proto->unpackFrame("{\"FOO\":\"bar\",\"a\":true}\0");
$expect = ['foo' => 'bar'];
$this->assertEquals($expect, $unpacked);
}
public function testConsumingFrames()
{
$proto = new JsonProtocol(
frameSeparator: "\n"
);
$buffer = '{"foo":true}'."\n".'{"bar":false}'."\n";
$msg1 = $proto->consumeFrame($buffer);
$msg2 = $proto->consumeFrame($buffer);
$this->assertEquals(['foo'=>true], $msg1);
$this->assertEquals(['bar'=>false], $msg2);
$proto = new JsonProtocol(
frameSeparator: "\n",
prependSizeBytes: 4,
prependSizeEndian: 'l',
);
$buffer = "\x0c\x00\x00\x00".'{"foo":true}'."\n"."\x0d\x00\x00\x00".'{"bar":false}'."\n";
$msg1 = $proto->consumeFrame($buffer);
$msg2 = $proto->consumeFrame($buffer);
$this->assertEquals(['foo'=>true], $msg1);
$this->assertEquals(['bar'=>false], $msg2);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace NoccyLabs\React\Protocol\Json;
use PHPUnit\Framework\Attributes\CoversClass;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
#[CoversClass(JsonProtocol::class)]
#[CoversClass(JsonRpcProtocol::class)]
class JsonRpcProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testProcessingCallbacks()
{
$proto = new JsonRpcProtocol(
frameSeparator: "\n"
);
$packed = $proto->packFrame([ 'method' => 'foo', 'params' => [ 'bar' => true ] ]);
$this->assertEquals('{"method":"foo","params":{"bar":true},"jsonrpc":"2.0"}'."\n", $packed);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace NoccyLabs\React\Protocol\Json;
use PHPUnit\Framework\Attributes\CoversClass;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
#[CoversClass(JsonProtocol::class)]
#[CoversClass(NativeMessagingProtocol::class)]
class NativeMessagingProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testFramePacking()
{
$proto = new NativeMessagingProtocol();
$packed = $proto->packFrame([ 'method' => 'foo', 'params' => [ 'bar' => true ] ]);
$this->assertEquals("\x26\x00\x00\x00".'{"method":"foo","params":{"bar":true}}', $packed);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace NoccyLabs\React\Protocol\Line;
use PHPUnit\Framework\Attributes\CoversClass;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
#[CoversClass(HttpLikeProtocol::class)]
class HttpLikeProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testPackingFrames()
{
$proto = new HttpLikeProtocol(
lineSeparator: "\n"
);
$packed = $proto->packFrame(['query' => 'GET / HTTP/1.1', 'headers' => [ 'foo' => [ 'bar' ]]]);
$this->assertEquals("GET / HTTP/1.1\nfoo: bar\n\n", $packed);
}
public function testUnpackingFrames()
{
$proto = new HttpLikeProtocol(
lineSeparator: "\n"
);
$msg1 = $proto->unpackFrame("GET / HTTP/1.1\nfoo: bar\n\n");
$this->assertEquals(['query' => 'GET / HTTP/1.1', 'headers' => [ 'foo' => [ 'bar' ]]], $msg1);
}
public function testConsumingFrames()
{
$proto = new HttpLikeProtocol(
lineSeparator: "\n"
);
$buffer = "GET / HTTP/1.1\nfoo: bar\n\n";
$msg1 = $proto->consumeFrame($buffer);
// $msg2 = $proto->consumeFrame($buffer);
$this->assertEquals(['query' => 'GET / HTTP/1.1', 'headers' => [ 'foo' => [ 'bar' ]]], $msg1);
// $this->assertEquals(['bar','false'], $msg2);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace NoccyLabs\React\Protocol\Line;
use PHPUnit\Framework\Attributes\CoversClass;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
#[CoversClass(LineProtocol::class)]
class LineProtocolTest extends \PHPUnit\Framework\TestCase
{
public function testFrameSeparators()
{
$proto = new LineProtocol("\n");
$packed = $proto->packFrame(['foo', 'bar']);
$this->assertEquals("foo bar\n", $packed);
}
public function testConsumingFrames()
{
$proto = new LineProtocol(
lineBreak: "\n"
);
$buffer = "foo true\nbar false\n";
$msg1 = $proto->consumeFrame($buffer);
$msg2 = $proto->consumeFrame($buffer);
$this->assertEquals(['foo','true'], $msg1);
$this->assertEquals(['bar','false'], $msg2);
}
}

View 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();
}
}