Skip to content

Commit c28c624

Browse files
authored
test: read streamed response (#7538)
1 parent 1f33ee6 commit c28c624

File tree

2 files changed

+19
-67
lines changed

2 files changed

+19
-67
lines changed

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,8 @@ parameters:
178178
- src/Symfony/Bundle/Test
179179
- tests
180180
- src # TODO
181+
182+
-
183+
identifier: method.notFound
184+
paths:
185+
- tests/Functional/JsonStreamerTest.php

tests/Functional/JsonStreamerTest.php

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,8 @@ public function testJsonStreamerJsonLd(): void
110110
$this->markTestSkipped();
111111
}
112112

113-
$buffer = '';
114-
ob_start(function (string $chunk) use (&$buffer): void {
115-
$buffer .= $chunk;
116-
});
117-
118-
self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/ld+json']]);
119-
120-
ob_get_clean();
121-
122-
$res = json_decode($buffer, true);
113+
$r = self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/ld+json']]);
114+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
123115
$this->assertIsInt($res['views']);
124116
$this->assertIsInt($res['rating']);
125117
$this->assertIsBool($res['isFeatured']);
@@ -144,11 +136,8 @@ public function testJsonStreamerCollectionJsonLd(): void
144136
$buffer .= $chunk;
145137
});
146138

147-
self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/ld+json']]);
148-
149-
ob_get_clean();
150-
151-
$res = json_decode($buffer, true);
139+
$r = self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/ld+json']]);
140+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
152141

153142
$this->assertIsArray($res);
154143
$this->assertArrayHasKey('@context', $res);
@@ -173,16 +162,8 @@ public function testJsonStreamerJson(): void
173162
$this->markTestSkipped();
174163
}
175164

176-
$buffer = '';
177-
ob_start(function (string $chunk) use (&$buffer): void {
178-
$buffer .= $chunk;
179-
});
180-
181-
self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/json']]);
182-
183-
ob_get_clean();
184-
185-
$res = json_decode($buffer, true);
165+
$r = self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/json']]);
166+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
186167
$this->assertIsInt($res['views']);
187168
$this->assertIsInt($res['rating']);
188169
$this->assertIsBool($res['isFeatured']);
@@ -202,17 +183,8 @@ public function testJsonStreamerCollectionJson(): void
202183
$this->markTestSkipped();
203184
}
204185

205-
$buffer = '';
206-
ob_start(function (string $chunk) use (&$buffer): void {
207-
$buffer .= $chunk;
208-
});
209-
210-
self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/json']]);
211-
212-
ob_get_clean();
213-
214-
$res = json_decode($buffer, true);
215-
186+
$r = self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/json']]);
187+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
216188
$this->assertIsArray($res);
217189
$this->assertArrayNotHasKey('@id', $res);
218190
$this->assertArrayNotHasKey('@type', $res);
@@ -233,12 +205,7 @@ public function testJsonStreamerWriteJsonLd(): void
233205
$this->markTestSkipped('PHP version is lower than 8.4');
234206
}
235207

236-
$buffer = '';
237-
ob_start(function (string $chunk) use (&$buffer): void {
238-
$buffer .= $chunk;
239-
});
240-
241-
self::createClient()->request('POST', '/json_stream_resources', [
208+
$r = self::createClient()->request('POST', '/json_stream_resources', [
242209
'json' => [
243210
'title' => 'asd',
244211
'views' => 0,
@@ -251,10 +218,7 @@ public function testJsonStreamerWriteJsonLd(): void
251218
'headers' => ['content-type' => 'application/ld+json'],
252219
]);
253220

254-
ob_get_clean();
255-
256-
$res = json_decode($buffer, true);
257-
221+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
258222
$this->assertResponseIsSuccessful();
259223
$this->assertSame('asd', $res['title']);
260224
$this->assertSame(0, $res['views']);
@@ -286,12 +250,7 @@ public function testJsonStreamerWriteJson(): void
286250
$this->markTestSkipped('PHP version is lower than 8.4');
287251
}
288252

289-
$buffer = '';
290-
ob_start(function (string $chunk) use (&$buffer): void {
291-
$buffer .= $chunk;
292-
});
293-
294-
self::createClient()->request('POST', '/json_stream_resources', [
253+
$r = self::createClient()->request('POST', '/json_stream_resources', [
295254
'json' => [
296255
'title' => 'asd',
297256
'views' => 0,
@@ -303,11 +262,7 @@ public function testJsonStreamerWriteJson(): void
303262
],
304263
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'],
305264
]);
306-
307-
ob_get_clean();
308-
309-
$res = json_decode($buffer, true);
310-
265+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
311266
$this->assertResponseIsSuccessful();
312267
$this->assertSame('asd', $res['title']);
313268
$this->assertSame(0, $res['views']);
@@ -335,16 +290,8 @@ public function testJsonStreamerJsonLdGenIdFalseWithDifferentTypeThenShortname()
335290
$this->markTestSkipped();
336291
}
337292

338-
$buffer = '';
339-
ob_start(function (string $chunk) use (&$buffer): void {
340-
$buffer .= $chunk;
341-
});
342-
343-
self::createClient()->request('GET', '/json-stream-products/test', ['headers' => ['accept' => 'application/ld+json']]);
344-
345-
ob_get_clean();
346-
347-
$res = json_decode($buffer, true);
293+
$r = self::createClient()->request('GET', '/json-stream-products/test', ['headers' => ['accept' => 'application/ld+json']]);
294+
$res = json_decode($r->getBrowserKitResponse()->getContent(), true);
348295
$this->assertArrayNotHasKey('@id', $res['aggregateRating']);
349296
$this->assertEquals('https://schema.org/AggregateRating', $res['aggregateRating']['@type']);
350297
$this->assertEquals('https://schema.org/Product', $res['@type']);

0 commit comments

Comments
 (0)