Laravel - Testing what happens after a redirect

You can get PHPUnit to follow redirects with:

Laravel >= 5.5.19:

$this->followingRedirects();

Laravel < 5.4.12:

$this->followRedirects();

Usage:

$response = $this->followingRedirects()
    ->post('/login', ['email' => '[email protected]'])
    ->assertStatus(200);

Note: This needs to be set explicitly for each request.


For versions between these two:

See https://github.com/laravel/framework/issues/18016#issuecomment-322401713 for a workaround.


You can tell crawler to follow a redirect this way:

$crawler = $this->client->followRedirect();

so in your case that would be something like:

public function testMessageSucceeds() {
    $this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);

    $this->assertResponseStatus(302);
    $this->assertRedirectedToRoute('home');

    $crawler = $this->client->followRedirect();

    $message = $crawler->filter('.success-message');

    $this->assertCount(1, $message);
}