1

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

Thiryn
  • 199
  • 1
  • 2
  • 12
  • Have you read [this](http://stackoverflow.com/questions/6535873/how-to-use-an-authenticated-user-in-a-symfony2-functional-test)? – Bartek Jul 03 '14 at 13:06
  • Yes, the problem isn't the authentication, I can login (overcoating[?] the security.yml configuration), but some informations isn't setted... for example if i have an action doing $this->getUser()->getRoles() phpunit says i cant do getRoles() method on non-object – Thiryn Jul 03 '14 at 15:13

2 Answers2

1

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

Thiryn
  • 199
  • 1
  • 2
  • 12
0

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

Nextar
  • 1,088
  • 2
  • 12
  • 26
  • Mmh... Why not... but how can I say to phpunit "Use this user object", in an action when I do $this->getUser(), $this is an instance of \Controller... How can i set the user...? – Thiryn Jul 03 '14 at 15:42
  • $this->getUser() uses the security.context which is stored in the Dependency injection container, instead of overriding something like register an own security.context you should provide a test database with a user fixture, if you login in a functional test this user will be aviable after the login in you Token which is stored in the security.context for more information please read the doku about the Security Component :) – Nextar Jul 03 '14 at 18:36
  • I see! I have set my security context Token, I can't create a test database, but I created a TestUser. I can log in, but now I have a little problem, I haven't any Authentication Cookie, and my listener (I don't use GC, I'm using my own listener who check the PHPSESSID cookie) redirect me directly to the login page (He do his work...). Is it a way to bypass it? – Thiryn Jul 04 '14 at 12:30