From c703de37d6eee632a3eb661c2866566fda80388e Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 15 Oct 2022 00:34:00 +1100 Subject: [PATCH] add TestResponse::assertContent() (#44580) --- src/Illuminate/Testing/TestResponse.php | 13 +++++++++++++ tests/Testing/TestResponseTest.php | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 72c88c1db55..ced3f3aaa09 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -514,6 +514,19 @@ public function getCookie($cookieName, $decrypt = true, $unserialize = false) } } + /** + * Assert that the given string matches the response content. + * + * @param string $value + * @return $this + */ + public function assertContent($value) + { + PHPUnit::assertSame($value, $this->content()); + + return $this; + } + /** * Assert that the given string or array of strings are contained within the response. * diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index d1cbf0183ba..c4014a185fa 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -211,6 +211,29 @@ public function testAssertViewMissingNested() $response->assertViewMissing('foo.baz'); } + public function testAssertContent() + { + $response = $this->makeMockResponse([ + 'render' => 'expected response data', + ]); + + $response->assertContent('expected response data'); + + try { + $response->assertContent('expected'); + $this->fail('xxxx'); + } catch (AssertionFailedError $e) { + $this->assertSame('Failed asserting that two strings are identical.', $e->getMessage()); + } + + try { + $response->assertContent('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([