The WebTestCase provides a conveniency method to create an already logged in client using the first parameter of
WebTestCase::makeClient().
You have three alternatives to create an already logged in client:
alexis_lefebvre_test.authentication key in the config_test.yml file;WebTestCase::loginAs();config_test.yml fileYou can set the credentials for your test user in your config_test.yml file:
alexis_lefebvre_test:
authentication:
username: "a valid username"
password: "the password of that user"
This way using $client = $this->makeClient(true); your client will be automatically logged in.
You can log in a user directly from your test method by simply passing an array as the first parameter of
WebTestCase::makeClient():
$credentials = array(
'username' => 'a valid username',
'password' => 'a valid password'
);
$client = $this->makeClient($credentials);
WebTestCase::loginAs()To use the method WebTestCase::loginAs() you have to return the repository containing all references set in the
fixtures using the method getReferenceRepository() and pass the reference of the User
object to the method WebTestCase::loginAs().
$fixtures = $this->loadFixtures(array(
'AppBundle\DataFixtures\ORM\LoadUserData'
))->getReferenceRepository();
$this->loginAs($fixtures->getReference('account-alpha'), 'main');
$client = $this->makeClient();
Remember that WebTestCase::loginAs() accepts objects that implement the interface Symfony\Component\Security\Core\User\UserInterface.
If you get the error message "Missing session.storage.options#name", you have to simply add to your
config_test.yml
file the key name:
framework:
...
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
storage_id: session.storage.mock_file
name: MOCKSESSID
As recommended by the Symfony Cookbook in
the chapter about Testing, it is a good idea to to use HTTP Basic Auth for you tests. You can configure the
authentication method in your config_test.yml:
# The best practice in symfony is to put a HTTP basic auth
# for the firewall in test env, so that not to have to
# make a request to the login form every single time.
# http://symfony.com/doc/current/cookbook/testing/http_authentication.html
security:
firewalls:
NAME_OF_YOUR_FIREWALL:
http_basic: ~
For more details, you can check the implementation of WebTestCase in that bundle.
How can I help you explore Laravel packages today?