Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Latest commit

 

History

History
90 lines (67 loc) · 3.08 KB

logged.md

File metadata and controls

90 lines (67 loc) · 3.08 KB

Create an already logged client

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:

  1. Use the alexis_lefebvre_fixtures.authentication key in the config_test.yml file;
  2. Pass an array with login parameters directly when you call the method;
  3. Use the method WebTestCase::loginAs();

Logging in a user from the config_test.yml file

You can set the credentials for your test user in your config_test.yml file:

alexis_lefebvre_fixtures:
    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.

Logging in a user passing the credentials directly in the test method

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);

Logging in a user using 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

Recommendations to use already logged in clients

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: ~

Final notes

For more details, you can check the implementation of WebTestCase in that bundle.