Tip
Some methods provided by this bundle have been implemented in Symfony. Alternative ways will be shown below.
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:
- Use the
liip_functional_test.authentication
key in theconfig_test.yml
file; - Pass an array with login parameters directly when you call the method;
- Use the method
WebTestCase::loginClient()
;
Tip
Since Symfony 5.1, loginUser()
can be used.
You can set the credentials for your test user in your config_test.yml
file:
liip_functional_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);
To use the method WebTestCase::loginClient()
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::loginClient()
.
$client = $this->makeClient();
$fixtures = $this->loadFixtures(array(
'AppBundle\DataFixtures\ORM\LoadUserData'
))->getReferenceRepository();
$this->loginClient($client, $fixtures->getReference('account-alpha'), 'main');
Remember that WebTestCase::loginClient()
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.
← Command test • Query counter →