Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add support for streamed JSON Response #49873

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
9 changes: 8 additions & 1 deletion src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -531,6 +532,11 @@ public function assertStreamedContent($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.
*
Expand Down Expand Up @@ -1564,7 +1570,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.');
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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([
Expand Down
Loading