I'm here today because I'm doing my functionnal tests for my symfony2 application and, I have a little problem : I can login the TestUser for my tests (passing server argument during the client creation), but when i just want to access to my User Object (by my tests, in controllers), he is empty.
It is logical because I don't create the object passing by the login page but I didn't find the issue for this problem, can you help me?
(I know I can do an "init function" witch call the login page, submit the login form etc... but i found it preety dirty, because i have to call her before EACH test, so... no)
Thank's ;)
I found my answer HERE
A function like :
private function logIn()
{
$session = $this->client->getContainer()->get('session');
$firewall = 'FIREWALL_NAME'; //set in security_test.yml
$token = new UsernamePasswordToken('PASSWORD', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
Will do the Job (creation of session's cookie)
Use a function witch set the server variable in client Request :
$client->request(
'METHOD',
'URI',
PARAMETERS,
array(),
array('PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'pa$$word')
);
Have Fun :)
If you want to use a real user object you should use TestFixtures like DoctrineTestFixtures .
Fixtures provides test data, in a seperate test database, in my opinion they should used in every functional test
http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.