2

I'm doing integration testing of Symfony2 controllers, inheriting my test classes from this:

class InsecureWebTestCase extends WebTestCase {

    protected $client = null;

    public function setUp() {
        $this->client = static::createClient();
        $session = $this->client->getContainer()->get('session');
        $firewall = 'default';
        $token = new UsernamePasswordToken(
            'norbert.scrunge@gmail.com',
            null, 
            $firewall, 
            array('ROLE_USER', 'ROLE_ADMIN')
        );
        // $this->client->getContainer()->get('security.context')->setToken($token);
        $session->set("_security_$firewall", serialize($token));
        $session->save();
        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }

}

If I use the controller as part of the app: $this->container->get('security.token_storage')->getToken()->getUser() and $this->getUser() are instances of my Doctrine "user" entity.

But when running integration tests: $this->container->get('security.token_storage')->getToken()->getUser() is a string containing the user name and $this->getUser() is NULL.

What do I need to do to make behaviour consistent in my app and functional tests?

Andy Preston
  • 779
  • 4
  • 9
  • 23
  • Try this way when performing your request: `$client->request('GET', '/post/12', array(), array(), array( 'PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'pa$$word', ));` – smarber May 19 '15 at 16:04

1 Answers1

5

Look to UsernamePasswordToken source:

class UsernamePasswordToken extends AbstractToken
{

    /**
     * Constructor.
     *
     * @param string|object            $user        The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method.
     * @param string                   $credentials This usually is the password of the user
     * @param string                   $providerKey The provider key
     * @param RoleInterface[]|string[] $roles       An array of roles
     *
     * @throws \InvalidArgumentException
     */
    public function __construct($user, $credentials, $providerKey, array $roles = array())

especially to $user param description

@param string|object $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object

So with the app usage you are passing an user entity there as $user param, but you are passing the email string at your test.

So first way is create new user object and fill it with some test data or get certain user from its repository like:

$user = $client->getContainer()->get('doctrine')->getManager()->getRepository('MyAppUserBundle:User')->findOneByEmail('norbert.scrunge@gmail.com');
$token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_USER', 'ROLE_ADMIN'));
Evgeniy Kuzmin
  • 2,384
  • 1
  • 19
  • 24
  • Simple answer then... just pass a mock or real user object to the `UsernamePasswordToken` constructor instead of just a user name (as is suggested in the Symfony2 docs) – Andy Preston May 20 '15 at 10:06
  • 1
    I tried the same solution here I am accessing logged in user inside eventSubscriber getUser() giving null any suggestion – somesh Aug 06 '19 at 06:16