Skip to content

Commit 0d733f2

Browse files
committed
add test for http publisher
1 parent 6a1349e commit 0d733f2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Services/HttpPublisher.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function publish($content): void
2525
fwrite($output, $content->getBody());
2626
} elseif (is_scalar($content)) {
2727
fwrite($output, $content);
28+
} elseif ($content === null) {
29+
fwrite($output, '');
2830
} else {
2931
fwrite($output, json_encode($content));
3032
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests\Services;
4+
5+
use Laminas\Diactoros\Response\JsonResponse;
6+
use MiladRahimi\PhpRouter\Router;
7+
use MiladRahimi\PhpRouter\Tests\TestCase;
8+
use Throwable;
9+
10+
class HttpPublisherTest extends TestCase
11+
{
12+
/**
13+
* @throws Throwable
14+
*/
15+
public function test_publish_a_string_response()
16+
{
17+
ob_start();
18+
19+
$router = Router::create();
20+
$router->get('/', function () {
21+
return 'Hello!';
22+
});
23+
$router->dispatch();
24+
25+
$this->assertEquals('Hello!', ob_get_clean());
26+
}
27+
28+
/**
29+
* @throws Throwable
30+
*/
31+
public function test_publish_a_empty_response()
32+
{
33+
ob_start();
34+
35+
$router = Router::create();
36+
$router->get('/', function () {
37+
//
38+
});
39+
$router->dispatch();
40+
41+
$this->assertEmpty(ob_get_clean());
42+
}
43+
44+
/**
45+
* @throws Throwable
46+
*/
47+
public function test_publish_a_array_response()
48+
{
49+
ob_start();
50+
51+
$router = Router::create();
52+
$router->get('/', function () {
53+
return ['a', 'b', 'c'];
54+
});
55+
$router->dispatch();
56+
57+
$this->assertEquals('["a","b","c"]', ob_get_clean());
58+
}
59+
}

0 commit comments

Comments
 (0)