5
5
namespace Tests \Tempest \Integration \Http ;
6
6
7
7
use JsonSerializable ;
8
+ use Stringable ;
9
+ use Tempest \DateTime \Duration ;
8
10
use Tempest \Http \GenericRequest ;
9
11
use Tempest \Http \GenericResponse ;
10
12
use Tempest \Http \Method ;
14
16
use Tempest \Http \Responses \File ;
15
17
use Tempest \Http \Responses \Ok ;
16
18
use Tempest \Http \ServerSentEvent ;
19
+ use Tempest \Http \ServerSentMessage ;
17
20
use Tempest \Http \Status ;
18
21
use Tempest \Router \GenericResponseSender ;
19
22
use Tempest \View \ViewRenderer ;
@@ -75,17 +78,10 @@ public function test_sending_head_request(): void
75
78
public function test_file_response (): void
76
79
{
77
80
ob_start ();
78
-
79
81
$ path = __DIR__ . '/Fixtures/sample.png ' ;
80
-
81
- $ response = new File (
82
- path: $ path ,
83
- );
84
-
82
+ $ response = new File (path: $ path );
85
83
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
86
-
87
84
$ responseSender ->send ($ response );
88
-
89
85
$ content = ob_get_clean ();
90
86
91
87
$ this ->assertSame (file_get_contents ($ path ), $ content );
@@ -94,17 +90,10 @@ public function test_file_response(): void
94
90
public function test_download_response (): void
95
91
{
96
92
ob_start ();
97
-
98
93
$ path = __DIR__ . '/Fixtures/sample.png ' ;
99
-
100
- $ response = new Download (
101
- path: $ path ,
102
- );
103
-
94
+ $ response = new Download (path: $ path );
104
95
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
105
-
106
96
$ responseSender ->send ($ response );
107
-
108
97
$ content = ob_get_clean ();
109
98
110
99
$ this ->assertSame (file_get_contents ($ path ), $ content );
@@ -113,16 +102,13 @@ public function test_download_response(): void
113
102
public function test_sending_of_array_to_json (): void
114
103
{
115
104
ob_start ();
116
-
117
105
$ response = new GenericResponse (
118
106
status: Status::CREATED ,
119
107
body: ['key ' => 'value ' ],
120
108
);
121
109
122
110
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
123
-
124
111
$ responseSender ->send ($ response );
125
-
126
112
$ output = ob_get_clean ();
127
113
128
114
$ this ->assertSame ('{"key":"value"} ' , $ output );
@@ -131,7 +117,6 @@ public function test_sending_of_array_to_json(): void
131
117
public function test_sending_of_json_serializable_to_json (): void
132
118
{
133
119
ob_start ();
134
-
135
120
$ response = new GenericResponse (
136
121
status: Status::CREATED ,
137
122
body: new class implements JsonSerializable {
@@ -143,9 +128,7 @@ public function jsonSerialize(): mixed
143
128
);
144
129
145
130
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
146
-
147
131
$ responseSender ->send ($ response );
148
-
149
132
$ output = ob_get_clean ();
150
133
151
134
$ this ->assertSame ('{"key":"value"} ' , $ output );
@@ -154,17 +137,14 @@ public function jsonSerialize(): mixed
154
137
public function test_view_body (): void
155
138
{
156
139
ob_start ();
157
-
158
140
$ response = new Ok (
159
141
body: view (__DIR__ . '/../../Fixtures/Views/overview.view.php ' )->data (
160
142
name: 'Brent ' ,
161
143
),
162
144
);
163
145
164
146
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
165
-
166
147
$ responseSender ->send ($ response );
167
-
168
148
$ output = ob_get_clean ();
169
149
170
150
$ this ->assertStringContainsString ('Hello Brent! ' , $ output );
@@ -176,10 +156,7 @@ public function test_stream(): void
176
156
$ response = new EventStream (fn () => yield 'hello ' );
177
157
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
178
158
$ responseSender ->send ($ response );
179
-
180
159
$ output = ob_get_clean ();
181
-
182
- // restore phpunit's output buffer
183
160
ob_start ();
184
161
185
162
$ this ->assertStringContainsString ('event: message ' , $ output );
@@ -190,20 +167,76 @@ public function test_stream_with_custom_event(): void
190
167
{
191
168
ob_start ();
192
169
$ response = new EventStream (function () {
193
- yield new ServerSentEvent (data: 'hello ' , event: 'first ' );
194
- yield new ServerSentEvent (data: 'goodbye ' , event: 'last ' );
170
+ yield new ServerSentMessage (data: 'hello ' , event: 'first ' );
171
+ yield new ServerSentMessage (data: 'goodbye ' , event: 'last ' );
195
172
});
196
173
$ responseSender = $ this ->container ->get (GenericResponseSender::class);
197
174
$ responseSender ->send ($ response );
198
-
199
175
$ output = ob_get_clean ();
200
-
201
- // restore phpunit's output buffer
202
176
ob_start ();
203
177
204
178
$ this ->assertStringContainsString ('event: first ' , $ output );
205
179
$ this ->assertStringContainsString ('data: "hello" ' , $ output );
206
180
$ this ->assertStringContainsString ('event: last ' , $ output );
207
181
$ this ->assertStringContainsString ('data: "goodbye" ' , $ output );
208
182
}
183
+
184
+ public function test_stream_with_custom_id (): void
185
+ {
186
+ ob_start ();
187
+ $ response = new EventStream (function () {
188
+ yield new ServerSentMessage (data: 'hello ' , id: 123 );
189
+ yield new ServerSentMessage (data: 'goodbye ' , id: 456 );
190
+ });
191
+ $ responseSender = $ this ->container ->get (GenericResponseSender::class);
192
+ $ responseSender ->send ($ response );
193
+ $ output = ob_get_clean ();
194
+ ob_start ();
195
+
196
+ $ this ->assertStringContainsString ('id: 123 ' , $ output );
197
+ $ this ->assertStringContainsString ('data: "hello" ' , $ output );
198
+ $ this ->assertStringContainsString ('id: 456 ' , $ output );
199
+ $ this ->assertStringContainsString ('data: "goodbye" ' , $ output );
200
+ }
201
+
202
+ public function test_stream_with_custom_retry (): void
203
+ {
204
+ ob_start ();
205
+ $ response = new EventStream (function () {
206
+ yield new ServerSentMessage (data: 'hello ' , retryAfter: 1000 );
207
+ yield new ServerSentMessage (data: 'goodbye ' , retryAfter: Duration::minute ());
208
+ });
209
+ $ responseSender = $ this ->container ->get (GenericResponseSender::class);
210
+ $ responseSender ->send ($ response );
211
+ $ output = ob_get_clean ();
212
+ ob_start ();
213
+
214
+ $ this ->assertStringContainsString ('retry: 1000 ' , $ output );
215
+ $ this ->assertStringContainsString ('data: "hello" ' , $ output );
216
+ $ this ->assertStringContainsString ('retry: 60000 ' , $ output );
217
+ $ this ->assertStringContainsString ('data: "goodbye" ' , $ output );
218
+ }
219
+
220
+ public function test_stream_with_custom_implementation (): void
221
+ {
222
+ ob_start ();
223
+ $ response = new EventStream (function () {
224
+ yield new class implements ServerSentEvent {
225
+ public ?int $ id = 1 ;
226
+ public null |Duration |int $ retryAfter = null ;
227
+ public ?string $ event = 'custom ' ;
228
+ public JsonSerializable |Stringable |string |iterable $ data = ['foo ' , 'bar ' ];
229
+ };
230
+ });
231
+ $ responseSender = $ this ->container ->get (GenericResponseSender::class);
232
+ $ responseSender ->send ($ response );
233
+ $ output = ob_get_clean ();
234
+ ob_start ();
235
+
236
+ $ this ->assertStringContainsString ('id: 1 ' , $ output );
237
+ $ this ->assertStringNotContainsString ('retry: ' , $ output );
238
+ $ this ->assertStringContainsString ('event: custom ' , $ output );
239
+ $ this ->assertStringContainsString ('data: foo ' , $ output );
240
+ $ this ->assertStringContainsString ('data: bar ' , $ output );
241
+ }
209
242
}
0 commit comments