From 9de80a7cf358a16a60221982cab99b951a066550 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 14 Nov 2025 15:03:11 +0100 Subject: [PATCH] test: read streamed response --- phpstan.neon.dist | 5 ++ tests/Functional/JsonStreamerTest.php | 81 +++++---------------------- 2 files changed, 19 insertions(+), 67 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ef1bbd402a6..de2a3ce47e2 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -178,3 +178,8 @@ parameters: - src/Symfony/Bundle/Test - tests - src # TODO + + - + identifier: method.notFound + paths: + - tests/Functional/JsonStreamerTest.php diff --git a/tests/Functional/JsonStreamerTest.php b/tests/Functional/JsonStreamerTest.php index cb2e34f512f..bc24ad2d14f 100644 --- a/tests/Functional/JsonStreamerTest.php +++ b/tests/Functional/JsonStreamerTest.php @@ -110,16 +110,8 @@ public function testJsonStreamerJsonLd(): void $this->markTestSkipped(); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/ld+json']]); - - ob_get_clean(); - - $res = json_decode($buffer, true); + $r = self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/ld+json']]); + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertIsInt($res['views']); $this->assertIsInt($res['rating']); $this->assertIsBool($res['isFeatured']); @@ -144,11 +136,8 @@ public function testJsonStreamerCollectionJsonLd(): void $buffer .= $chunk; }); - self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/ld+json']]); - - ob_get_clean(); - - $res = json_decode($buffer, true); + $r = self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/ld+json']]); + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertIsArray($res); $this->assertArrayHasKey('@context', $res); @@ -173,16 +162,8 @@ public function testJsonStreamerJson(): void $this->markTestSkipped(); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/json']]); - - ob_get_clean(); - - $res = json_decode($buffer, true); + $r = self::createClient()->request('GET', '/json_stream_resources/1', ['headers' => ['accept' => 'application/json']]); + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertIsInt($res['views']); $this->assertIsInt($res['rating']); $this->assertIsBool($res['isFeatured']); @@ -202,17 +183,8 @@ public function testJsonStreamerCollectionJson(): void $this->markTestSkipped(); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/json']]); - - ob_get_clean(); - - $res = json_decode($buffer, true); - + $r = self::createClient()->request('GET', '/json_stream_resources', ['headers' => ['accept' => 'application/json']]); + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertIsArray($res); $this->assertArrayNotHasKey('@id', $res); $this->assertArrayNotHasKey('@type', $res); @@ -233,12 +205,7 @@ public function testJsonStreamerWriteJsonLd(): void $this->markTestSkipped('PHP version is lower than 8.4'); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('POST', '/json_stream_resources', [ + $r = self::createClient()->request('POST', '/json_stream_resources', [ 'json' => [ 'title' => 'asd', 'views' => 0, @@ -251,10 +218,7 @@ public function testJsonStreamerWriteJsonLd(): void 'headers' => ['content-type' => 'application/ld+json'], ]); - ob_get_clean(); - - $res = json_decode($buffer, true); - + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertResponseIsSuccessful(); $this->assertSame('asd', $res['title']); $this->assertSame(0, $res['views']); @@ -286,12 +250,7 @@ public function testJsonStreamerWriteJson(): void $this->markTestSkipped('PHP version is lower than 8.4'); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('POST', '/json_stream_resources', [ + $r = self::createClient()->request('POST', '/json_stream_resources', [ 'json' => [ 'title' => 'asd', 'views' => 0, @@ -303,11 +262,7 @@ public function testJsonStreamerWriteJson(): void ], 'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'], ]); - - ob_get_clean(); - - $res = json_decode($buffer, true); - + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertResponseIsSuccessful(); $this->assertSame('asd', $res['title']); $this->assertSame(0, $res['views']); @@ -335,16 +290,8 @@ public function testJsonStreamerJsonLdGenIdFalseWithDifferentTypeThenShortname() $this->markTestSkipped(); } - $buffer = ''; - ob_start(function (string $chunk) use (&$buffer): void { - $buffer .= $chunk; - }); - - self::createClient()->request('GET', '/json-stream-products/test', ['headers' => ['accept' => 'application/ld+json']]); - - ob_get_clean(); - - $res = json_decode($buffer, true); + $r = self::createClient()->request('GET', '/json-stream-products/test', ['headers' => ['accept' => 'application/ld+json']]); + $res = json_decode($r->getBrowserKitResponse()->getContent(), true); $this->assertArrayNotHasKey('@id', $res['aggregateRating']); $this->assertEquals('https://schema.org/AggregateRating', $res['aggregateRating']['@type']); $this->assertEquals('https://schema.org/Product', $res['@type']);