From 737a4bd93bf7915e96c8bf52c73e3cb2cec213c8 Mon Sep 17 00:00:00 2001 From: Shane Date: Fri, 16 May 2025 19:08:00 +0800 Subject: [PATCH] [12.x] Add `assertClientError` method to `TestResponse` --- src/Illuminate/Testing/TestResponse.php | 15 +++++++++++++++ tests/Testing/TestResponseTest.php | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 0e9de3d5bcf6..72f37339238c 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -131,6 +131,21 @@ public function assertSuccessfulPrecognition() return $this; } + /** + * Assert that the response is a client error. + * + * @return $this + */ + public function assertClientError() + { + PHPUnit::withResponse($this)->assertTrue( + $this->isClientError(), + $this->statusMessageWithDetails('>=400, < 500', $this->getStatusCode()) + ); + + return $this; + } + /** * Assert that the response is a server error. * diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 32642d0df55c..f9389a82e516 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -1086,6 +1086,18 @@ public function testAssertUnprocessable() $response->assertUnprocessable(); } + public function testAssertClientError() + { + $statusCode = 400; + + $baseResponse = tap(new Response, function ($response) use ($statusCode) { + $response->setStatusCode($statusCode); + }); + + $response = TestResponse::fromBaseResponse($baseResponse); + $response->assertClientError(); + } + public function testAssertServerError() { $statusCode = 500;