title: How to simulate authenticated users in functional tests in Silex php framework
body: <p>I am trying to functional test a page protected by authentication form.</p>
  
  <p>My code is based on this link: <a href="http://symfony.com/doc/current/testing/simulating_authentication.html" rel="nofollow">http://symfony.com/doc/current/testing/simulating_authentication.html</a></p>
  
  <p>But it still does not work for me.</p>
  
  <p>Here is thye error message that I get:</p>
  
  <pre><code>PHP Fatal error:  Call to undefined method Symfony\Component\HttpKernel\Client::getContainer()
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>class ClientTest extends WebTestCase {
  
      public function createApplication()
      {
          return require __DIR__.'/../app.php';
      }
  
      /**
       * @test
       */
      public function index_when_no_clients()
      {
          $this-&gt;client = $this-&gt;createClient();
          $crawler = $this-&gt;client-&gt;request('GET', '/');
  
          $this-&gt;assertTrue($this-&gt;client-&gt;getResponse()-&gt;isOk());
          $this-&gt;assertCount(1, $crawler-&gt;filter('h1:contains("Clients")'));
          $this-&gt;assertCount(1, $crawler-&gt;filter('span[class="glyphicon glyphicon-plus"]'));
      }
  
      /**
       * @test
       */
      public function add_get_route_is_valid()
      {
          $client = $this-&gt;createClient();
          $session = $client-&gt;getContainer()-&gt;get('session');
  
          // the firewall context (defaults to the firewall name)
          $firewall = 'secured_area';
  
          $token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
          $session-&gt;set('_security_'.$firewall, serialize($token));
          $session-&gt;save();
  
          $cookie = new Cookie($session-&gt;getName(), $session-&gt;getId());
          $client-&gt;getCookieJar()-&gt;set($cookie);
          $crawler = $client-&gt;request('GET', '/add'); 
  
          $this-&gt;assertTrue($client-&gt;getResponse()-&gt;isOk());
      }   
  }
  </code></pre>
  
  <p>Here is how I am setting authentication in my index.php:</p>
  
  <pre><code>$app-&gt;register(new \Silex\Provider\SessionServiceProvider(), [
      'session.test' =&gt; 'test' === $app['env'],
  ]);
  $app-&gt;register(new Silex\Provider\SecurityServiceProvider(), array(
      'security.firewalls' =&gt; array(
          'admin' =&gt; array(
              'pattern' =&gt; '^/add',
              'form' =&gt; array('login_path' =&gt; '/login', 'check_path' =&gt; '/add/login_check'),
              'users' =&gt; $app-&gt;share(function() use ($app) {
                  $config = Configuration::getInstance();
                  $pdoQueries = new PdoQueries(PdoConnection::getInstance($config));
                  return new UserProvider($pdoQueries);
              }),
          ),
      )
  ));
  
  $app-&gt;get('/login', function(Request $request) use ($app, $arrConfig) {
      return $app['twig']-&gt;render('auth/form.twig', array(
              'error'         =&gt; $app['security.last_error']($request),
              'last_username' =&gt; $app['session']-&gt;get('_security.last_username'),
              'title'         =&gt; 'Login',
              'assetsUrl'             =&gt; $arrConfig['assets_url']
      ));
  });
  </code></pre>
  
  <p>And here is the content of the app.php called in createApplication:</p>
  
  <pre><code>&lt;?php
  require __DIR__ ."/../www/index.php";
  unset($app['exception_handler']);
  return $app;
  </code></pre>
  
  <p>Thanks</p>