Skip to content
New issue

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

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

Already on GitHub? # to your account

[11.x] assertStreamed and assertNotStreamed #54566

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,36 @@ public function assertContent($value)
return $this;
}

/**
* Assert that the response was streamed.
*
* @return $this
*/
public function assertStreamed()
{
PHPUnit::withResponse($this)->assertTrue(
$this->baseResponse instanceof StreamedResponse || $this->baseResponse instanceof StreamedJsonResponse,
'Expected the response to be streamed, but it wasn\'t.'
);

return $this;
}

/**
* Assert that the response was not streamed.
*
* @return $this
*/
public function assertNotStreamed()
{
PHPUnit::withResponse($this)->assertTrue(
! $this->baseResponse instanceof StreamedResponse && ! $this->baseResponse instanceof StreamedJsonResponse,
'Response was unexpectedly streamed.'
);

return $this;
}

/**
* Assert that the given string matches the streamed response content.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,39 @@ public function testAssertContent()
}
}

public function testAssertStreamedAndAssertNotStreamed()
{
$notStreamedResponse = $this->makeMockResponse([
'render' => 'expected response data',
]);

$streamedResponse = TestResponse::fromBaseResponse(
new StreamedResponse(function () {
$stream = fopen('php://memory', 'r+');
fwrite($stream, 'expected response data');
rewind($stream);
fpassthru($stream);
})
);

$notStreamedResponse->assertNotStreamed();
$streamedResponse->assertStreamed();

try {
$notStreamedResponse->assertStreamed();
$this->fail('xxxx');
} catch (AssertionFailedError $e) {
$this->assertSame("Expected the response to be streamed, but it wasn't.\nFailed asserting that false is true.", $e->getMessage());
}

try {
$streamedResponse->assertNotStreamed();
$this->fail('xxxx');
} catch (AssertionFailedError $e) {
$this->assertSame("Response was unexpectedly streamed.\nFailed asserting that false is true.", $e->getMessage());
}
}

public function testAssertStreamedContent()
{
$response = TestResponse::fromBaseResponse(
Expand Down
Loading