Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Illuminate/Testing/TestResponseAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\ViewErrorBag;
use PHPUnit\Framework\ExpectationFailedException;
use ReflectionProperty;

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down