diff --git a/src/Illuminate/Routing/ResponseFactory.php b/src/Illuminate/Routing/ResponseFactory.php index 35b209584549..84c69225e370 100644 --- a/src/Illuminate/Routing/ResponseFactory.php +++ b/src/Illuminate/Routing/ResponseFactory.php @@ -10,6 +10,7 @@ use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\StreamedJsonResponse; use Symfony\Component\HttpFoundation\StreamedResponse; use Throwable; @@ -129,6 +130,20 @@ public function stream($callback, $status = 200, array $headers = []) return new StreamedResponse($callback, $status, $headers); } + /** + * Create a new streamed response instance. + * + * @param array $data + * @param int $status + * @param array $headers + * @param int $encodingOptions + * @return \Symfony\Component\HttpFoundation\StreamedJsonResponse + */ + public function streamJson($data, $status = 200, $headers = [], $encodingOptions = JsonResponse::DEFAULT_ENCODING_OPTIONS) + { + return new StreamedJsonResponse($data, $status, $headers, $encodingOptions); + } + /** * Create a new streamed response instance as a file download. * diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index dc9b405c0565..ea19db479a03 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -26,6 +26,7 @@ use PHPUnit\Framework\ExpectationFailedException; use ReflectionProperty; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\StreamedJsonResponse; use Symfony\Component\HttpFoundation\StreamedResponse; /** @@ -531,6 +532,17 @@ public function assertStreamedContent($value) return $this; } + /** + * Assert that the given array matches the streamed JSON response content. + * + * @param array $value + * @return $this + */ + public function assertStreamedJsonContent($value) + { + return $this->assertStreamedContent(json_encode($value, JSON_THROW_ON_ERROR)); + } + /** * Assert that the given string or array of strings are contained within the response. * @@ -1564,7 +1576,8 @@ public function streamedContent() return $this->streamedContent; } - if (! $this->baseResponse instanceof StreamedResponse) { + if (! $this->baseResponse instanceof StreamedResponse + && ! $this->baseResponse instanceof StreamedJsonResponse) { PHPUnit::fail('The response is not a streamed response.'); } diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 3934bbbebc13..f4c1ee713a6b 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -28,6 +28,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\StreamedJsonResponse; use Symfony\Component\HttpFoundation\StreamedResponse; class TestResponseTest extends TestCase @@ -285,6 +286,49 @@ public function testAssertStreamedContent() } } + public function testAssertStreamedJsonContent() + { + $response = TestResponse::fromBaseResponse( + new StreamedJsonResponse([ + 'data' => $this->yieldTestModels(), + ]) + ); + + $response->assertStreamedJsonContent([ + 'data' => [ + ['id' => 1], + ['id' => 2], + ['id' => 3], + ], + ]); + + try { + $response->assertStreamedJsonContent([ + 'data' => [ + ['id' => 1], + ['id' => 2], + ], + ]); + $this->fail('xxxx'); + } catch (AssertionFailedError $e) { + $this->assertSame('Failed asserting that two strings are identical.', $e->getMessage()); + } + + try { + $response->assertStreamedContent('not expected response string'); + $this->fail('xxxx'); + } catch (AssertionFailedError $e) { + $this->assertSame('Failed asserting that two strings are identical.', $e->getMessage()); + } + } + + public function yieldTestModels() + { + yield new TestModel(['id' => 1]); + yield new TestModel(['id' => 2]); + yield new TestModel(['id' => 3]); + } + public function testAssertSee() { $response = $this->makeMockResponse([