*/ /** 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_HandlersEvent () { $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 lowerHandlersEvent ($val) { return strtolower ($val); } $sse->setBackendFiles (["event1" => "/tmp/dfwTestSSE1", "event2" => "/tmp/dfwTestSSE2"]) ->setHandlersEvent (["event1" => "lowerHandlersEvent"]) ->setPingTime(1); $sse->loop (); } public function test_loop_HandlerDataonly () { $this->expectOutputString(str_repeat (": ping\n\n", 4). "data: will be seen 1\n\n". ": ping\n\n"); @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 ("lowerHandlerDataonly") ->setPingTime(1); $sse->loop (); } public function test_loop_HandlerDataonlyWithParams () { $this->expectOutputString(str_repeat (": ping\n\n", 4). "data: PREwill be seen 1POST\n\n". ": ping\n\n"); @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 ("lowerHandlerDataonlyWithParams", "PRE", "POST") ->setPingTime(1); $sse->loop (); } }