Skip to content

Commit 2181f38

Browse files
authored
[9.x] Add streamed content test assertion (#45298)
* assert streamed content * add missing ns * fix test issues
1 parent eff4ec3 commit 2181f38

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,19 @@ public function assertContent($value)
572572
return $this;
573573
}
574574

575+
/**
576+
* Assert that the given string matches the streamed response content.
577+
*
578+
* @param $value
579+
* @return $this
580+
*/
581+
public function assertStreamedContent($value)
582+
{
583+
PHPUnit::assertSame($value, $this->streamedContent());
584+
585+
return $this;
586+
}
587+
575588
/**
576589
* Assert that the given string or array of strings are contained within the response.
577590
*

tests/Testing/TestResponseTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PHPUnit\Framework\TestCase;
2929
use Symfony\Component\HttpFoundation\BinaryFileResponse;
3030
use Symfony\Component\HttpFoundation\Cookie;
31+
use Symfony\Component\HttpFoundation\StreamedResponse;
3132

3233
class TestResponseTest extends TestCase
3334
{
@@ -234,6 +235,34 @@ public function testAssertContent()
234235
}
235236
}
236237

238+
public function testAssertStreamedContent()
239+
{
240+
$response = TestResponse::fromBaseResponse(
241+
new StreamedResponse(function () {
242+
$stream = fopen('php://memory', 'r+');
243+
fwrite($stream, 'expected response data');
244+
rewind($stream);
245+
fpassthru($stream);
246+
})
247+
);
248+
249+
$response->assertStreamedContent('expected response data');
250+
251+
try {
252+
$response->assertStreamedContent('expected');
253+
$this->fail('xxxx');
254+
} catch (AssertionFailedError $e) {
255+
$this->assertSame('Failed asserting that two strings are identical.', $e->getMessage());
256+
}
257+
258+
try {
259+
$response->assertStreamedContent('expected response data with extra');
260+
$this->fail('xxxx');
261+
} catch (AssertionFailedError $e) {
262+
$this->assertSame('Failed asserting that two strings are identical.', $e->getMessage());
263+
}
264+
}
265+
237266
public function testAssertSee()
238267
{
239268
$response = $this->makeMockResponse([

0 commit comments

Comments
 (0)