title: Logging a User in Programmatically for a Functional Test with FOSUserBundle
body: <p>I am attempting to log a user in programmatically in my functional test on SF 2.7 and FOSUserBundle dev-master. I have already found a good reference to log a user in via SO in this answer - <a href="https://stackoverflow.com/questions/14957807/symfony2-tests-with-fosuserbundle">Symfony2 - Tests with FOSUserBundle</a></p>
  
  <p>The problem is that the second answer, logging the user in programmatically, doesn't work. Here is my code:</p>
  
  <pre><code>&lt;?php
  
  namespace Test\BackEnd\UserBundle\Controller;
  
  use Test\Shared\CoreBundle\Tests\AbstractControllerTest;
  use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
  use Doctrine\Common\DataFixtures\Loader;
  use Doctrine\Common\DataFixtures\Purger\ORMPurger;
  use Doctrine\ORM\Tools\SchemaTool;
  use FA\BackEnd\UserBundle\DataFixtures\ORM\LoadUserData;
  use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  use Symfony\Component\BrowserKit\Cookie;
  use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  
  class DefaultController extends AbstractControllerTest
  {
  
  public function setUp()
  {
      $this-&gt;client = static::createClient();
  
      $container = $this-&gt;client-&gt;getContainer();
  
      $doctrine = $container-&gt;get('doctrine');
      $em = $doctrine-&gt;getManager();
      $schemaTool = new SchemaTool($em);
      $metadata = $em-&gt;getMetaDataFactory()-&gt;getAllMetadata();
  
      // Drop and recreate tables for all entities
      $schemaTool-&gt;dropSchema($metadata);
      $schemaTool-&gt;createSchema($metadata);
  
      $loader = new Loader();
      $user = new LoadUserData();
      $user-&gt;setContainer($container);
      $loader-&gt;addFixture($user);
  
      $purger = new ORMPurger();
      $executor = new ORMExecutor($em, $purger);
      $executor-&gt;execute($loader-&gt;getFixtures());
  
      $session = $container-&gt;get('session');
      $userManager = $container-&gt;get('fos_user.user_manager');
  
      $user = $userManager-&gt;findUserBy(array('username' =&gt; 'test'));
  
      $firewall = 'default';
  
      $token = new UsernamePasswordToken($user, $user-&gt;getPassword(), $firewall, $user-&gt;getRoles());
      self::$kernel-&gt;getContainer()-&gt;get('security.token_storage')-&gt;setToken($token);
      $session-&gt;set('_security_'.$firewall, serialize($token));
      $session-&gt;save();
  
      $cookie = new Cookie($session-&gt;getName(), $session-&gt;getId());
      $this-&gt;client-&gt;getCookieJar()-&gt;set($cookie);
  }
  
  public function testProfile()
  {
      //$this-&gt;createAuthorizedClient();
  
      $token = $this-&gt;client-&gt;getContainer()-&gt;get('security.token_storage')-&gt;getToken();
  
      $this-&gt;client-&gt;request('GET', '/profile/');
  
      $this-&gt;assertEquals(
          200,
          $this-&gt;client-&gt;getResponse()-&gt;getStatusCode(),
          "/profile isn't accessible"
      );
  
  }
  }
  </code></pre>
  
  <p>Whenever I set a break point before the route gets executed, the token is return correctly:
  <a href="https://i.stack.imgur.com/oMvWS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oMvWS.png" alt="PHPStorm Xdebug Output for token"></a></p>
  
  <p>Whenever I get to the function getUser() used by the Controller (<a href="http://api.symfony.com/2.7/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_getUser" rel="nofollow noreferrer">http://api.symfony.com/2.7/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_getUser</a>) PHPStorm returns an empty token as viewed here:</p>
  
  <p><a href="https://i.stack.imgur.com/VeysG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VeysG.png" alt="PHPstorm Xdebug Information null token"></a></p>
  
  <p>So I decided to try the following code to log a user in, and it works.</p>
  
  <pre><code>    $crawler = $this-&gt;client-&gt;request('GET', '/login');
  
      $form = $crawler-&gt;selectButton('_submit')-&gt;form(array(
          '_username'  =&gt; 'test',
          '_password'  =&gt; 'test123',
      ));
  
      $this-&gt;client-&gt;submit($form);
      $this-&gt;client-&gt;followRedirect(); 
  </code></pre>
  
  <p>Am I not doing something properly whenever I log the user in programmatically? Is the session not being set properly?</p>
  
  <p>Thanks!</p>
  
  <p>Rat</p>