Skip to content

Commit

Permalink
[10.x] Add support for streamed JSON Response (#49873)
Browse files Browse the repository at this point in the history
* Add support for streamed JSON Response

* Add test helper and tests for streamed JSON Responses

* Remove unused imports

* Remove unused method

* Remove unused imports

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
pelmered and taylorotwell authored Jan 29, 2024
1 parent 334a217 commit e9c67db
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
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
15 changes: 14 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,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.
*
Expand Down Expand Up @@ -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.');
}

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

0 comments on commit e9c67db

Please sign in to comment.