|
6 | 6 |
|
7 | 7 | use Tempest\Http\GenericResponse; |
8 | 8 | use Tempest\Http\Responses\Download; |
| 9 | +use Tempest\Http\Responses\EventStream; |
9 | 10 | use Tempest\Http\Responses\File; |
10 | 11 | use Tempest\Http\Responses\Ok; |
| 12 | +use Tempest\Http\ServerSentEvent; |
11 | 13 | use Tempest\Http\Status; |
12 | 14 | use Tempest\Router\GenericResponseSender; |
13 | 15 | use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
@@ -110,4 +112,39 @@ public function test_view_body(): void |
110 | 112 |
|
111 | 113 | $this->assertStringContainsString('Hello Brent!', $output); |
112 | 114 | } |
| 115 | + |
| 116 | + public function test_stream(): void |
| 117 | + { |
| 118 | + ob_start(); |
| 119 | + $response = new EventStream(fn () => yield 'hello'); |
| 120 | + $responseSender = $this->container->get(GenericResponseSender::class); |
| 121 | + $responseSender->send($response); |
| 122 | + $output = ob_get_clean(); |
| 123 | + |
| 124 | + // restore phpunit's output buffer |
| 125 | + ob_start(); |
| 126 | + |
| 127 | + $this->assertStringContainsString('event: message', $output); |
| 128 | + $this->assertStringContainsString('data: "hello"', $output); |
| 129 | + } |
| 130 | + |
| 131 | + public function test_stream_with_custom_event(): void |
| 132 | + { |
| 133 | + ob_start(); |
| 134 | + $response = new EventStream(function () { |
| 135 | + yield new ServerSentEvent(data: 'hello', event: 'first'); |
| 136 | + yield new ServerSentEvent(data: 'goodbye', event: 'last'); |
| 137 | + }); |
| 138 | + $responseSender = $this->container->get(GenericResponseSender::class); |
| 139 | + $responseSender->send($response); |
| 140 | + $output = ob_get_clean(); |
| 141 | + |
| 142 | + // restore phpunit's output buffer |
| 143 | + ob_start(); |
| 144 | + |
| 145 | + $this->assertStringContainsString('event: first', $output); |
| 146 | + $this->assertStringContainsString('data: "hello"', $output); |
| 147 | + $this->assertStringContainsString('event: last', $output); |
| 148 | + $this->assertStringContainsString('data: "goodbye"', $output); |
| 149 | + } |
113 | 150 | } |
0 commit comments