Laravel - HTTP Tests - Session And Authentication
Laravel provides several helpers for interacting with the session during HTTP testing. First, you may set the session data to a given array using the withSession
method. This is useful for loading the session with data before issuing a request to your application:
withSession(['banned' => false])->get('/');
}
}
Laravel's session is typically used to maintain state for the currently authenticated user. Therefore, the actingAs
helper method provides a simple way to authenticate a given user as the current user. For example, we may use a model factory to generate and authenticate a user:
create();
$response = $this->actingAs($user)
->withSession(['banned' => false])
->get('/');
}
}
You may also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the actingAs
method:
$this->actingAs($user, 'web')