Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/examples/ export-ignore
/phpunit.xml.dist export-ignore
/tests/ export-ignore

*.php diff=php
11 changes: 3 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ jobs:
strategy:
matrix:
php:
- 7.3
- 7.2
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4
- 5.3
- 8.1
- 8.0
- 7.4
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
Expand Down
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/composer.lock
/vendor/
.DS_Store
/vendor
composer.lock
/build
/.phpunit.cache
.phpunit.result.cache
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See the [examples](examples).

The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)

```JSON
```json
{
"require": {
"clue/sse-react": "dev-master"
Expand All @@ -28,14 +28,14 @@ The recommended way to install this library is [through composer](http://getcomp
To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](http://getcomposer.org):

```bash
$ composer install
```sh
composer install
```

To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
```sh
php vendor/bin/phpunit
```

## License
Expand Down
33 changes: 23 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
{
"name": "clue/sse-react",
"name": "rodber/php-sse-react",
"description": "Streaming, async HTML5 Server-Sent Events server (aka. SSE or EventSource)",
"keywords": ["Server-Sent Events", "SSE", "EventSource", "event-driven", "ReactPHP", "async"],
"homepage": "https://github.com/clue/php-sse-react",
"keywords": [
"Server-Sent Events",
"SSE",
"EventSource",
"event-driven",
"ReactPHP",
"async"
],
"homepage": "https://github.com/rodber/php-sse-react",
"license": "MIT",
"authors": [
{
"name": "Christian Lück",
"email": "[email protected]"
},
{
"name": "Rodolfo Berrios",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": { "Clue\\React\\Sse\\": "src/" }
"psr-4": {
"Clue\\React\\Sse\\": "src/"
}
},
"require": {
"php": ">=5.3",
"react/event-loop": "^1.0",
"react/http": "^1.0",
"react/stream": "^1.0"
"php": ">=7.4",
"react/event-loop": "^1.2",
"react/http": "^1.5",
"react/stream": "^1.2"
},
"require-dev": {
"clue/redis-react": "^2.4",
"phpunit/phpunit": "^5.0 || ^4.8"
"clue/redis-react": "^2.5",
"phpunit/phpunit": "^9.5"
}
}
6 changes: 3 additions & 3 deletions examples/01-simple-periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use React\Http\Message\Response;
use React\Stream\ThroughStream;

$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Loop::get();

$channel = new BufferedChannel();

$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
$http = new React\Http\HttpServer($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
if ($request->getUri()->getPath() === '/') {
return new Response(
200,
Expand Down Expand Up @@ -45,7 +45,7 @@
);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\SocketServer(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', [], $loop);
$http->listen($socket);

$loop->addPeriodicTimer(2.0, function () use ($channel) {
Expand Down
6 changes: 3 additions & 3 deletions examples/02-plaintext-chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use React\Socket\TcpConnector;
use React\Stream\ThroughStream;

$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Loop::get();

$channel = new BufferedChannel();

$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
$http = new React\Http\HttpServer($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
if ($request->getUri()->getPath() === '/') {
return new Response(
200,
Expand Down Expand Up @@ -61,7 +61,7 @@
});
}, 'printf');

$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\SocketServer(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', [], $loop);
$http->listen($socket);

echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
Expand Down
6 changes: 3 additions & 3 deletions examples/03-redis-subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use React\Http\Message\Response;
use React\Stream\ThroughStream;

$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Loop::get();

$channel = new BufferedChannel();

$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
$http = new React\Http\HttpServer($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
if ($request->getUri()->getPath() === '/') {
return new Response(
'200',
Expand Down Expand Up @@ -57,7 +57,7 @@
echo 'ERROR: Unable to subscribe to Redis channel: ' . $e;
});

$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\SocketServer(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', [], $loop);
$http->listen($socket);

echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
Expand Down
6 changes: 3 additions & 3 deletions examples/11-chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use React\Http\Message\Response;
use React\Stream\ThroughStream;

$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Loop::get();

$channel = new BufferedChannel();

$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
$http = new React\Http\HttpServer($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
switch ($request->getUri()->getPath()) {
case '/':
return new Response(
Expand Down Expand Up @@ -65,7 +65,7 @@
}
});

$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\SocketServer(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', [], $loop);
$http->listen($socket);

echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
Expand Down
30 changes: 21 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
cacheResultFile=".phpunit.cache/test-results"
failOnRisky="true"
failOnWarning="true"
testdox="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
convertWarningsToExceptions="true">
<testsuites>
<testsuite name="SSE Test Suite">
<directory>./tests/</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
<logging>
<junit outputFile="build/logs/junit.xml"/>
</logging>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/html" lowUpperBound="50" highLowerBound="90"/>
</report>
</coverage>
</phpunit>
24 changes: 9 additions & 15 deletions tests/BufferedChannelTest.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
<?php

use Clue\React\Sse\BufferedChannel;
use React\Stream\WritableStreamInterface;

class BufferedChannelTest extends TestCase
{
public function testNumberOfWritesToStream()
public function testNumberOfWritesToStream(): void
{
$stream = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

$stream = $this
->getMockBuilder('React\Stream\WritableStreamInterface')
->getMock();
$called = 0;
$stream->expects($this->any())->method('write')->will($this->returnCallback(function () use (&$called) {
++$called;
}));

/** @var $stream WritableStreamInterface */
$channel = new BufferedChannel();

// initially nothing written
$channel->connect($stream);
$this->assertEquals(0, $called);

// writing does send
$channel->writeMessage('first');
$this->assertEquals(1, $called);

// writing does send again
$channel->writeMessage('second');
$this->assertEquals(2, $called);

// writing after disconnect does not send
$channel->disconnect($stream);
$channel->writeMessage('third');
$this->assertEquals(2, $called);

// connecting does not send
$channel->connect($stream);
$this->assertEquals(2, $called);

// connecting with offset will send remaining message
$channel->disconnect($stream);
$channel->connect($stream, 2);
Expand All @@ -44,19 +40,17 @@ public function testNumberOfWritesToStream()

public function testResultingStreamBuffer()
{
$stream = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

$stream = $this
->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$buffered = '';
$stream->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use (&$buffered) {
$buffered .= $data;
}));

/** @var $stream WritableStreamInterface */
$channel = new BufferedChannel();

// initially nothing
$channel->writeMessage('hello', 'world');
$this->assertEquals('', $buffered);

// connecting will send messages buffered in channel
$channel->connect($stream, 0);
$this->assertEquals("id: 0\nevent: world\ndata: hello\n\n", $buffered);
Expand Down
22 changes: 13 additions & 9 deletions tests/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,37 @@ class EncoderTest extends TestCase
{
private $encoder;

public function setUp()
public function setUp(): void
{
$this->encoder = new Encoder();
}

public function testData()
public function testData(): void
{
$this->assertEquals("data: test\n", $this->encoder->encodeData('test'));
$this->assertSame("data: test\n", $this->encoder->encodeData('test'));
}

public function testDataMultiLine()
public function testDataMultiLine(): void
{
$this->assertEquals("data: first\ndata: second\n", $this->encoder->encodeData("first\nsecond"));
$this->assertSame("data: first\ndata: second\n", $this->encoder->encodeData("first\nsecond"));
}

public function testDataEmpty()
public function testDataEmpty(): void
{
$this->assertEquals("data: \n", $this->encoder->encodeData(""));
$this->assertSame("data: \n", $this->encoder->encodeData(""));
}

public function testComment()
public function testComment(): void
{
$this->assertEquals(":welcome!\n", $this->encoder->encodeComment('welcome!'));
}

public function testMessage()
public function testMessage(): void
{
$this->assertEquals("id: 123\nevent: demo\ndata: test\n\n", $this->encoder->encodeMessage('test', 'demo', 123));
}

public function testEncodeFieldEmpty(): void {
$this->assertEquals("string\n", $this->encoder->encodeFieldEmpty('string'));
}
}
7 changes: 3 additions & 4 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

PHPUnit_Framework_Error_Deprecated::$enabled = false;
require __DIR__ . '/../vendor/autoload.php';

class TestCase extends PHPUnit_Framework_TestCase
class TestCase extends PHPUnitTestCase
{
protected function expectCallableOnce()
{
$mock = $this->createCallableMock();

$mock
->expects($this->once())
->method('__invoke');
Expand Down