From 25a26c8e25d47921242fffb5db7572775e6f3cf8 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sat, 15 Aug 2026 13:13:57 +0500 Subject: [PATCH] Keep the assertion failure message when session errors are JSON serialized --- src/Illuminate/Testing/TestResponseAssert.php | 13 ++++++- tests/Testing/TestResponseTest.php | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Testing/TestResponseAssert.php b/src/Illuminate/Testing/TestResponseAssert.php index 25dfc289f128..769fef2a2442 100644 --- a/src/Illuminate/Testing/TestResponseAssert.php +++ b/src/Illuminate/Testing/TestResponseAssert.php @@ -4,6 +4,7 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Support\Arr; +use Illuminate\Support\ViewErrorBag; use PHPUnit\Framework\ExpectationFailedException; use ReflectionProperty; @@ -78,7 +79,17 @@ protected function injectResponseContext($exception) $session = $this->response->baseResponse->getSession(); if (! is_null($session) && $session->has('errors')) { - return $this->appendErrorsToException($session->get('errors')->all(), $exception); + $errors = $session->get('errors'); + + if (! $errors instanceof ViewErrorBag && ! $session->isStarted()) { + $session->start(); + + $errors = $session->get('errors'); + } + + if ($errors instanceof ViewErrorBag) { + return $this->appendErrorsToException($errors->all(), $exception); + } } } diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 2454259420ba..d3cbd8f3ca49 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -3177,6 +3177,44 @@ public function testValidationErrorsAreIncludedInAssertionFailure(): void $response->assertStatus(200); } + public function testValidationErrorsAreIncludedInAssertionFailureWhenSessionIsJsonSerialized(): void + { + $session = new Store('test-session', new NullSessionHandler(), null, 'json'); + + $response = TestResponse::fromBaseResponse( + tap(new RedirectResponse('/')) + ->setSession($session) + ->withErrors([ + 'first_name' => 'The first name field is required.', + 'last_name' => 'The last name field is required.', + ]) + ); + + $session->save(); // Required to serialize error bag to JSON + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessageMatches('/Expected response status code \[200\] but received 302.*The first name field is required.*The last name field is required/s'); + + $response->assertStatus(200); + } + + public function testValidationErrorsAreIncludedInAssertionFailureWhenJsonSerializedSessionIsNotSaved(): void + { + $response = TestResponse::fromBaseResponse( + tap(new RedirectResponse('/')) + ->setSession(new Store('test-session', new NullSessionHandler(), null, 'json')) + ->withErrors([ + 'first_name' => 'The first name field is required.', + 'last_name' => 'The last name field is required.', + ]) + ); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessageMatches('/Expected response status code \[200\] but received 302.*The first name field is required.*The last name field is required/s'); + + $response->assertStatus(200); + } + public function testJsonErrorsAreIncludedInAssertionFailure(): void { $response = TestResponse::fromBaseResponse(new JsonResponse([