Add Server-Sent Events

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6062 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2020-08-28 12:44:03 +00:00
parent 0ff91d1353
commit 6e846514c6
2 changed files with 384 additions and 0 deletions

98
Tests/sseTest.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
/** DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
*/
/** Test the domframework Server-Sent Events part */
class sseTest extends PHPUnit_Framework_TestCase
{
public function test_loop_NOTDEFINED ()
{
$this->expectException ("Exception");
$sse = new sse ();
$res = $sse->loop ();
}
public function test_loop_JUSTPING ()
{
$this->expectOutputString(str_repeat (": ping\n\n", 5));
$sse = new sse ();
@unlink ("/tmp/dfwTestSSE1");
$sse->setBackendFiles ("/tmp/dfwTestSSE1")
->setPingTime(1);
$sse->loop ();
}
public function test_loop_SKIP_START ()
{
$this->expectOutputString(str_repeat (": ping\n\n", 5));
$sse = new sse ();
@unlink ("/tmp/dfwTestSSE1");
file_put_contents ("/tmp/dfwTestSSE1", "NOT SEEN");
$sse->setBackendFiles ("/tmp/dfwTestSSE1")
->setPingTime(1);
$sse->loop ();
}
public function test_loop_DATA ()
{
$this->expectOutputString(str_repeat (": ping\n\n", 4).
"data: WILL BE SEEN\n\n: ping\n\n");
@unlink ("/tmp/dfwTestSSE1");
$sse = new sse ();
pcntl_signal(SIGALRM, function () {
file_put_contents ("/tmp/dfwTestSSE1", "WILL BE SEEN\n");
}, false);
pcntl_alarm(3);
$sse->setBackendFiles ("/tmp/dfwTestSSE1")
->setPingTime(1);
$sse->loop ();
}
public function test_loop_EVENTS ()
{
$this->expectOutputString(str_repeat (": ping\n\n", 4).
"event: event1\ndata: WILL BE SEEN 1\n\n".
"event: event2\ndata: WILL BE SEEN 2\n\n".
": ping\n\n");
@unlink ("/tmp/dfwTestSSE1");
@unlink ("/tmp/dfwTestSSE2");
$sse = new sse ();
pcntl_signal(SIGALRM, function () {
file_put_contents ("/tmp/dfwTestSSE1", "WILL BE SEEN 1\n");
file_put_contents ("/tmp/dfwTestSSE2", "WILL BE SEEN 2\n");
}, false);
pcntl_alarm(3);
$sse->setBackendFiles (["event1" => "/tmp/dfwTestSSE1",
"event2" => "/tmp/dfwTestSSE2"])
->setPingTime(1);
$sse->loop ();
}
public function test_loop_Handler ()
{
$this->expectOutputString(str_repeat (": ping\n\n", 4).
"event: event1\ndata: will be seen 1\n\n".
"event: event2\ndata: WILL BE SEEN 2\n\n".
": ping\n\n");
@unlink ("/tmp/dfwTestSSE1");
@unlink ("/tmp/dfwTestSSE2");
$sse = new sse ();
pcntl_signal(SIGALRM, function () {
file_put_contents ("/tmp/dfwTestSSE1", "will be seen 1\n");
file_put_contents ("/tmp/dfwTestSSE2", "WILL BE SEEN 2\n");
}, false);
pcntl_alarm(3);
function lower ($val) {
return strtolower ($val);
}
$sse->setBackendFiles (["event1" => "/tmp/dfwTestSSE1",
"event2" => "/tmp/dfwTestSSE2"])
->setHandlers (["event1" => "lower"])
->setPingTime(1);
$sse->loop ();
}
}