Skip to content

Commit 102d770

Browse files
[11.x] assertStreamed and assertNotStreamed (#54566)
* assertStreamed and assertNotStreamed * Fix linting issue * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b4f11a9 commit 102d770

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,36 @@ public function assertContent($value)
532532
return $this;
533533
}
534534

535+
/**
536+
* Assert that the response was streamed.
537+
*
538+
* @return $this
539+
*/
540+
public function assertStreamed()
541+
{
542+
PHPUnit::withResponse($this)->assertTrue(
543+
$this->baseResponse instanceof StreamedResponse || $this->baseResponse instanceof StreamedJsonResponse,
544+
'Expected the response to be streamed, but it wasn\'t.'
545+
);
546+
547+
return $this;
548+
}
549+
550+
/**
551+
* Assert that the response was not streamed.
552+
*
553+
* @return $this
554+
*/
555+
public function assertNotStreamed()
556+
{
557+
PHPUnit::withResponse($this)->assertTrue(
558+
! $this->baseResponse instanceof StreamedResponse && ! $this->baseResponse instanceof StreamedJsonResponse,
559+
'Response was unexpectedly streamed.'
560+
);
561+
562+
return $this;
563+
}
564+
535565
/**
536566
* Assert that the given string matches the streamed response content.
537567
*

tests/Testing/TestResponseTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,39 @@ public function testAssertContent()
261261
}
262262
}
263263

264+
public function testAssertStreamedAndAssertNotStreamed()
265+
{
266+
$notStreamedResponse = $this->makeMockResponse([
267+
'render' => 'expected response data',
268+
]);
269+
270+
$streamedResponse = TestResponse::fromBaseResponse(
271+
new StreamedResponse(function () {
272+
$stream = fopen('php://memory', 'r+');
273+
fwrite($stream, 'expected response data');
274+
rewind($stream);
275+
fpassthru($stream);
276+
})
277+
);
278+
279+
$notStreamedResponse->assertNotStreamed();
280+
$streamedResponse->assertStreamed();
281+
282+
try {
283+
$notStreamedResponse->assertStreamed();
284+
$this->fail('xxxx');
285+
} catch (AssertionFailedError $e) {
286+
$this->assertSame("Expected the response to be streamed, but it wasn't.\nFailed asserting that false is true.", $e->getMessage());
287+
}
288+
289+
try {
290+
$streamedResponse->assertNotStreamed();
291+
$this->fail('xxxx');
292+
} catch (AssertionFailedError $e) {
293+
$this->assertSame("Response was unexpectedly streamed.\nFailed asserting that false is true.", $e->getMessage());
294+
}
295+
}
296+
264297
public function testAssertStreamedContent()
265298
{
266299
$response = TestResponse::fromBaseResponse(

0 commit comments

Comments
 (0)