title: Faking authentication on Symfony2 tests - get('security.context')->getToken() on Controller returns differnt token than the one set up in the TestCase
body: <p>I'm writing symfony functional tests for a Symfony 2.0 application that uses the FOSFacebookBundle (v2.0). The controllers I'm testing check for authentication like so:</p>
  
  <pre><code>if($this-&gt;get('security.context')-&gt;isGranted('IS_AUTHENTICATED_FULLY')){...}
  </code></pre>
  
  <p>So I need the test's <code>$client</code> (of type <code>Symfony\Bundle\FrameworkBundle\Client</code>) to be authenticated.</p>
  
  <p>Supposedly I need to create a valid token and set it on the <code>$client</code>'s <code>security.context</code>, <code>session</code>, and <code>cookieJar</code> for <code>$client</code> to be authenticated, right?</p>
  
  <p>The issue is that the <code>security.context</code> in the controllers return a different token from the valid one I set up on the test's <code>$client</code>. </p>
  
  <p>Does anyone know why the token gets reset after making the request?</p>
  
  <p>Here's how I'm setting this up in the test case class:</p>
  
  <pre><code>use FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider;
  use FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken;
  use Symfony\Component\BrowserKit\Cookie;
  
  $providerKey = 'public';
  
  $facebookMock = $this-&gt;getMockBuilder('\Facebook')
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
  $facebookMock-&gt;expects($this-&gt;once())
      -&gt;method('getUser')
      -&gt;will($this-&gt;returnValue('DevTestUsername'));
  
  $facebookProvider = new FacebookProvider($providerKey, $facebookMock);
  
  $tokenMock = new FacebookUserToken(
      'public', 
      'DevTestUsername', 
      array('ROLE_MEMBER', 'ROLE_MANAGER', 'ROLE_USER')
  );
  
  $authenticatedToken = $facebookProvider-&gt;authenticate($tokenMock);
  
  $client-&gt;getContainer()-&gt;get('security.context')-&gt;setToken($authenticatedToken);
  $client-&gt;getContainer()-&gt;get('session')-&gt;set('_security_public', serialize($authenticatedToken));
  $client-&gt;getContainer()-&gt;get('session')-&gt;save();
  
  $client-&gt;getCookieJar()-&gt;set(new Cookie(session_name(), $client-&gt;getContainer()-&gt;get('session')-&gt;getId()));
  </code></pre>
  
  <p>After this setup, on the test case this returns <code>true</code>:</p>
  
  <pre><code>if($client-&gt;getContainer()-&gt;get('security.context')-&gt;isGranted('IS_AUTHENTICATED_FULLY')){...}
  </code></pre>
  
  <p>But on the controllers, it returns <code>false</code></p>
  
  <pre><code>if($this-&gt;get('security.context')-&gt;isGranted('IS_AUTHENTICATED_FULLY')){...}
  </code></pre>
  
  <p>Am I setting up things wrong in the security.context or in the session?</p>
  
  <p>Here's the application's security config:</p>
  
  <pre><code>security:
      factories:
          - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
  
      encoders:
          FOS\UserBundle\Model\UserInterface: sha512
  
      role_hierarchy:
          ROLE_ADMIN:       [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
          ROLE_SUPER_ADMIN: [ROLE_ADMIN]
  
      providers:
          chain_provider:
              providers: [fos_userbundle, facebook_provider]
          fos_userbundle:
              id: fos_user.user_manager
          facebook_provider:
              id: facebook.user
  
      firewalls:
          public:
              pattern:   ^/.*
              fos_facebook:
                  provider: facebook_provider
                  app_url: "http://apps.facebook.com/myapp"
                  server_url: "http://myapp.local/"
                  login_path: /login
                  check_path: /login_check
                  default_target_path: /target
              form_login:
                  provider: fos_userbundle
                  csrf_provider: form.csrf_provider
                  login_path: /login
                  check_path: /login_check
              anonymous: true
              switch_user: { role: ROLE_ADMIN }
              logout:
                  target: http://myapp.com
                  handlers: ["fos_facebook.logout_handler"]
              context: primary_auth
  </code></pre>
  
  <p>The facebook.id service is a class that implements the  Symfony\Component\Security\Core\User\UserProviderInterface</p>
  
  <p>This class uses facebook's php api (BaseFacebook class) to make a call to the facebook graph api (BaseFacebook->api('/me')), but this never happens, as I don't get past Symfony's security.</p>
  
  <p>Any comments, pointers or links to resources greatly appreciated. Thanks!</p>