Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.4 (progress) #60

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ jobs:
- server
php:
- 7.4
- 7.3
- 7.2
- 7.1
- 7.0
- 5.6
- 8.0
- 8.1
steps:
- uses: actions/checkout@v2
- name: Setup PHP
Expand All @@ -33,11 +30,4 @@ jobs:
- run: sh tests/ab/run_ab_tests.sh
env:
ABTEST: ${{ matrix.env }}
SKIP_DEFLATE: _skip_deflate
if: ${{ matrix.php <= 5.6 }}

- run: sh tests/ab/run_ab_tests.sh
env:
ABTEST: ${{ matrix.env }}
if: ${{ matrix.php >= 7.0 }}
- run: vendor/bin/phpunit --verbose
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
composer.lock
vendor
tests/ab/reports
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
}
},
"require": {
"php": ">=5.4.2",
"php": ">=7.4",
"guzzlehttp/psr7": "^2 || ^1.7"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^9.5",
"react/socket": "^1.3"
},
"scripts": {
Expand Down
19 changes: 7 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
stopOnError="false"
>

<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<exclude>
<directory>test/ab</directory>
</exclude>
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
11 changes: 7 additions & 4 deletions tests/AbResultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Ratchet\RFC6455\Test;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class AbResultsTest extends TestCase {
private function verifyAutobahnResults($fileName) {
private function verifyAutobahnResults(string $fileName): void {
if (!file_exists($fileName)) {
return $this->markTestSkipped('Autobahn TestSuite results not found');
$this->markTestSkipped('Autobahn TestSuite results not found');
}

$resultsJson = file_get_contents($fileName);
Expand All @@ -22,11 +25,11 @@ private function verifyAutobahnResults($fileName) {
}
}

public function testAutobahnClientResults() {
public function testAutobahnClientResults(): void {
$this->verifyAutobahnResults(__DIR__ . '/ab/reports/clients/index.json');
}

public function testAutobahnServerResults() {
public function testAutobahnServerResults(): void {
$this->verifyAutobahnResults(__DIR__ . '/ab/reports/servers/index.json');
}
}
50 changes: 25 additions & 25 deletions tests/ab/clientRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
use GuzzleHttp\Psr7\Uri;
use Ratchet\RFC6455\Handshake\InvalidPermessageDeflateOptionsException;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Handshake\ClientNegotiator;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\MessageInterface;
use React\Promise\Deferred;
use Ratchet\RFC6455\Messaging\Frame;
use React\Promise\PromiseInterface;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;

Expand All @@ -23,23 +25,21 @@

$connector = new Connector($loop);

function echoStreamerFactory($conn, $permessageDeflateOptions = null)
function echoStreamerFactory(ConnectionInterface $conn, ?PermessageDeflateOptions $permessageDeflateOptions = null): MessageBuffer
{
$permessageDeflateOptions = $permessageDeflateOptions ?: PermessageDeflateOptions::createDisabled();

return new \Ratchet\RFC6455\Messaging\MessageBuffer(
new \Ratchet\RFC6455\Messaging\CloseFrameChecker,
function (\Ratchet\RFC6455\Messaging\MessageInterface $msg, MessageBuffer $messageBuffer) use ($conn) {
return new MessageBuffer(
new CloseFrameChecker,
static function (MessageInterface $msg, MessageBuffer $messageBuffer) use ($conn): void {
$messageBuffer->sendMessage($msg->getPayload(), true, $msg->isBinary());
},
function (\Ratchet\RFC6455\Messaging\FrameInterface $frame, MessageBuffer $messageBuffer) use ($conn) {
static function (FrameInterface $frame, MessageBuffer $messageBuffer) use ($conn) {
switch ($frame->getOpcode()) {
case Frame::OP_PING:
return $conn->write((new Frame($frame->getPayload(), true, Frame::OP_PONG))->maskPayload()->getContents());
break;
case Frame::OP_CLOSE:
return $conn->end((new Frame($frame->getPayload(), true, Frame::OP_CLOSE))->maskPayload()->getContents());
break;
}
},
false,
Expand All @@ -51,13 +51,13 @@ function (\Ratchet\RFC6455\Messaging\FrameInterface $frame, MessageBuffer $messa
);
}

function getTestCases() {
function getTestCases(): PromiseInterface {
global $testServer;
global $connector;

$deferred = new Deferred();

$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $testServer) {
$connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $testServer): void {
$cn = new ClientNegotiator();
$cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002/getCaseCount'));

Expand All @@ -67,7 +67,7 @@ function getTestCases() {
/** @var MessageBuffer $ms */
$ms = null;

$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
$connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) {
$rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n");
Expand All @@ -82,7 +82,7 @@ function getTestCases() {
} else {
$ms = new MessageBuffer(
new CloseFrameChecker,
function (MessageInterface $msg) use ($deferred, $connection) {
static function (MessageInterface $msg) use ($deferred, $connection): void {
$deferred->resolve($msg->getPayload());
$connection->close();
},
Expand All @@ -91,7 +91,7 @@ function (MessageInterface $msg) use ($deferred, $connection) {
null,
null,
null,
function () {}
static function (): void {}
);
}
}
Expand All @@ -109,10 +109,10 @@ function () {}
return $deferred->promise();
}

$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator(
$cn = new ClientNegotiator(
PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null);

function runTest($case)
function runTest(int $case)
{
global $connector;
global $testServer;
Expand All @@ -122,7 +122,7 @@ function runTest($case)

$deferred = new Deferred();

$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $casePath, $case, $testServer) {
$connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $casePath, $case, $testServer): void {
$cn = new ClientNegotiator(
PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null);
$cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002' . $casePath));
Expand All @@ -132,7 +132,7 @@ function runTest($case)

$ms = null;

$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
$connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) {
$rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n");
Expand Down Expand Up @@ -165,7 +165,7 @@ function runTest($case)
}
});

$connection->on('close', function () use ($deferred) {
$connection->on('close', static function () use ($deferred): void {
$deferred->resolve();
});

Expand All @@ -175,13 +175,13 @@ function runTest($case)
return $deferred->promise();
}

function createReport() {
function createReport(): PromiseInterface {
global $connector;
global $testServer;

$deferred = new Deferred();

$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $testServer) {
$connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $testServer): void {
// $reportPath = "/updateReports?agent=" . AGENT . "&shutdownOnComplete=true";
// we will stop it using docker now instead of just shutting down
$reportPath = "/updateReports?agent=" . AGENT;
Expand All @@ -194,7 +194,7 @@ function createReport() {
/** @var MessageBuffer $ms */
$ms = null;

$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
$connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) {
$rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n");
Expand All @@ -209,7 +209,7 @@ function createReport() {
} else {
$ms = new MessageBuffer(
new CloseFrameChecker,
function (MessageInterface $msg) use ($deferred, $connection) {
static function (MessageInterface $msg) use ($deferred, $connection): void {
$deferred->resolve($msg->getPayload());
$connection->close();
},
Expand All @@ -218,7 +218,7 @@ function (MessageInterface $msg) use ($deferred, $connection) {
null,
null,
null,
function () {}
static function (): void {}
);
}
}
Expand All @@ -242,7 +242,7 @@ function () {}
getTestCases()->then(function ($count) use ($loop) {
$allDeferred = new Deferred();

$runNextCase = function () use (&$i, &$runNextCase, $count, $allDeferred) {
$runNextCase = static function () use (&$i, &$runNextCase, $count, $allDeferred): void {
$i++;
if ($i > $count) {
$allDeferred->resolve();
Expand All @@ -251,7 +251,7 @@ function () {}
echo "Running test $i/$count...";
$startTime = microtime(true);
runTest($i)
->then(function () use ($startTime) {
->then(static function () use ($startTime): void {
echo " completed " . round((microtime(true) - $startTime) * 1000) . " ms\n";
})
->then($runNextCase);
Expand All @@ -260,7 +260,7 @@ function () {}
$i = 0;
$runNextCase();

$allDeferred->promise()->then(function () {
$allDeferred->promise()->then(static function (): void {
createReport();
});
});
Expand Down
21 changes: 11 additions & 10 deletions tests/ab/startServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\Response;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Handshake\RequestVerifier;
use Ratchet\RFC6455\Handshake\ServerNegotiator;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\RFC6455\Messaging\FrameInterface;
Expand All @@ -14,17 +17,17 @@

$socket = new \React\Socket\Server('0.0.0.0:9001', $loop);

$closeFrameChecker = new \Ratchet\RFC6455\Messaging\CloseFrameChecker;
$negotiator = new \Ratchet\RFC6455\Handshake\ServerNegotiator(new \Ratchet\RFC6455\Handshake\RequestVerifier, PermessageDeflateOptions::permessageDeflateSupported());
$closeFrameChecker = new CloseFrameChecker;
$negotiator = new ServerNegotiator(new RequestVerifier, PermessageDeflateOptions::permessageDeflateSupported());

$uException = new \UnderflowException;


$socket->on('connection', function (React\Socket\ConnectionInterface $connection) use ($negotiator, $closeFrameChecker, $uException, $socket) {
$socket->on('connection', static function (React\Socket\ConnectionInterface $connection) use ($negotiator, $closeFrameChecker, $uException, $socket): void {
$headerComplete = false;
$buffer = '';
$parser = null;
$connection->on('data', function ($data) use ($connection, &$parser, &$headerComplete, &$buffer, $negotiator, $closeFrameChecker, $uException, $socket) {
$connection->on('data', static function ($data) use ($connection, &$parser, &$headerComplete, &$buffer, $negotiator, $closeFrameChecker, $uException, $socket): void {
if ($headerComplete) {
$parser->onData($data);
return;
Expand Down Expand Up @@ -58,10 +61,10 @@
// we support any valid permessage deflate
$deflateOptions = PermessageDeflateOptions::fromRequestOrResponse($psrRequest)[0];

$parser = new \Ratchet\RFC6455\Messaging\MessageBuffer($closeFrameChecker,
function (MessageInterface $message, MessageBuffer $messageBuffer) {
$parser = new MessageBuffer($closeFrameChecker,
static function (MessageInterface $message, MessageBuffer $messageBuffer): void {
$messageBuffer->sendMessage($message->getPayload(), true, $message->isBinary());
}, function (FrameInterface $frame) use ($connection, &$parser) {
}, static function (FrameInterface $frame) use ($connection, &$parser): void {
switch ($frame->getOpCode()) {
case Frame::OP_CLOSE:
$connection->end($frame->getContents());
Expand All @@ -70,9 +73,7 @@ function (MessageInterface $message, MessageBuffer $messageBuffer) {
$connection->write($parser->newFrame($frame->getPayload(), true, Frame::OP_PONG)->getContents());
break;
}
}, true, function () use ($uException) {
return $uException;
},
}, true, static fn (): \Exception => $uException,
null,
null,
[$connection, 'write'],
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/Handshake/PermessageDeflateOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use PHPUnit\Framework\TestCase;

/**
* @covers Ratchet\RFC6455\Handshake\PermessageDeflateOptions
*/
class PermessageDeflateOptionsTest extends TestCase
{
public static function versionSupportProvider() {
public static function versionSupportProvider(): array {
return [
['7.0.17', false],
['7.0.18', true],
Expand All @@ -24,7 +27,7 @@ public static function versionSupportProvider() {
* @requires function deflate_init
* @dataProvider versionSupportProvider
*/
public function testVersionSupport($version, $supported) {
public function testVersionSupport(string $version, bool $supported): void {
$this->assertEquals($supported, PermessageDeflateOptions::permessageDeflateSupported($version));
}
}
}
Loading