Files
DomFramework/Tests/SseTest.php

172 lines
5.1 KiB
PHP

<?php
/**
* DomFramework - Tests
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework\Tests;
use Domframework\Sse;
/**
* Test the domframework Server-Sent Events part
*/
class SseTest extends \PHPUnit_Framework_TestCase
{
public function testLoopNOTDEFINED()
{
$this->expectException("Exception");
$sse = new Sse();
$res = $sse->loop();
}
public function testLoopJUSTPING()
{
$this->expectOutputString(str_repeat(": ping\n\n", 5));
$sse = new Sse();
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
$sse->setBackendFiles("/tmp/dfwTestSSE1")
->setPingTime(1);
$sse->loop();
}
public function testLoopSKIPSTART()
{
$this->expectOutputString(str_repeat(": ping\n\n", 5));
$sse = new Sse();
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
file_put_contents("/tmp/dfwTestSSE1", "NOT SEEN");
$sse->setBackendFiles("/tmp/dfwTestSSE1")
->setPingTime(1);
$sse->loop();
}
public function testLoopDATA()
{
$this->expectOutputString(str_repeat(": ping\n\n", 4) .
"data: WILL BE SEEN\n\n: ping\n\n");
if (file_exists("/tmp/dfwTestSSE1")) {
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 testLoopEVENTS()
{
$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");
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
if (file_exists("/tmp/dfwTestSSE2")) {
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 testLoopHandlersEvent()
{
$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");
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
if (file_exists("/tmp/dfwTestSSE2")) {
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 lowerHandlersEvent($val)
{
return strtolower($val);
}
$sse->setBackendFiles(["event1" => "/tmp/dfwTestSSE1",
"event2" => "/tmp/dfwTestSSE2"])
->setHandlersEvent([
"event1" => __NAMESPACE__ . "\\lowerHandlersEvent"])
->setPingTime(1);
$sse->loop();
}
public function testLoopHandlerDataonly()
{
$this->expectOutputString(str_repeat(": ping\n\n", 4) .
"data: will be seen 1\n\n" .
": ping\n\n");
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
$sse = new Sse();
pcntl_signal(SIGALRM, function () {
file_put_contents("/tmp/dfwTestSSE1", "WILL BE SEEN 1\n");
}, false);
pcntl_alarm(3);
function lowerHandlerDataonly($val)
{
return strtolower($val);
}
$sse->setBackendFiles("/tmp/dfwTestSSE1")
->setHandlerDataonly(__NAMESPACE__ . "\\lowerHandlerDataonly")
->setPingTime(1);
$sse->loop();
}
public function testLoopHandlerDataonlyWithParams()
{
$this->expectOutputString(str_repeat(": ping\n\n", 4) .
"data: PREwill be seen 1POST\n\n" .
": ping\n\n");
if (file_exists("/tmp/dfwTestSSE1")) {
unlink("/tmp/dfwTestSSE1");
}
$sse = new Sse();
pcntl_signal(SIGALRM, function () {
file_put_contents("/tmp/dfwTestSSE1", "WILL BE SEEN 1\n");
}, false);
pcntl_alarm(3);
function lowerHandlerDataonlyWithParams($val, $param1, $param2)
{
return $param1 . strtolower($val) . $param2;
}
$sse->setBackendFiles("/tmp/dfwTestSSE1")
->setHandlerDataonly(
__NAMESPACE__ . "\\lowerHandlerDataonlyWithParams",
"PRE",
"POST"
)
->setPingTime(1);
$sse->loop();
}
}