Skip to content

Commit

Permalink
[9.x] Add streamed content test assertion (#45298)
Browse files Browse the repository at this point in the history
* assert streamed content

* add missing ns

* fix test issues
  • Loading branch information
ziadoz committed Dec 14, 2022
1 parent eff4ec3 commit 2181f38
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Expand Up @@ -572,6 +572,19 @@ public function assertContent($value)
return $this;
}

/**
* Assert that the given string matches the streamed response content.
*
* @param $value
* @return $this
*/
public function assertStreamedContent($value)
{
PHPUnit::assertSame($value, $this->streamedContent());

return $this;
}

/**
* Assert that the given string or array of strings are contained within the response.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Testing/TestResponseTest.php
Expand Up @@ -28,6 +28,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\StreamedResponse;

class TestResponseTest extends TestCase
{
Expand Down Expand Up @@ -234,6 +235,34 @@ public function testAssertContent()
}
}

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

$response->assertStreamedContent('expected response data');

try {
$response->assertStreamedContent('expected');
$this->fail('xxxx');
} catch (AssertionFailedError $e) {
$this->assertSame('Failed asserting that two strings are identical.', $e->getMessage());
}

try {
$response->assertStreamedContent('expected response data with extra');
$this->fail('xxxx');
} catch (AssertionFailedError $e) {
$this->assertSame('Failed asserting that two strings are identical.', $e->getMessage());
}
}

public function testAssertSee()
{
$response = $this->makeMockResponse([
Expand Down

0 comments on commit 2181f38

Please sign in to comment.