title: Symfony unit testing & the container
body: <p>When unit testing in Symfony 2, the controller I'm testing does not receive the service container causing the test to fail with the good old <code>Call to a member function get() on a non-object</code></p>
  
  <p>I can't use $this->forward from the testing controller as it also does not have the service container. </p>
  
  <p>I found this reference but it seems as though I would be using it for the wrong reason, has anyone had any experience with this? </p>
  
  <p><a href="http://symfony.com/doc/current/book/testing.html#accessing-the-container" rel="nofollow">http://symfony.com/doc/current/book/testing.html#accessing-the-container</a></p>
  
  <p>Edit: Here is my test:</p>
  
  <pre><code>&lt;?php
  
  namespace HvH\ClientsBundle\Tests\Controller;
  
  use HvH\ClientsBundle\Controller\ClientsController;
  
  use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\HeaderBag;
  use Symfony\Component\HttpFoundation\Session;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  class ClientsControllerTest extends WebTestCase
  {
  
      public function testGetClientsAction()
      {
  
          $client = static::createClient();
          $container = $client-&gt;getContainer();
          $session = $container-&gt;get('session');
          $session-&gt;set('key', 'value');
          $session-&gt;save();
  
          $request = new Request;
          $request-&gt;create('/clients/123456', 'GET', array(), array(), array(), array(), '');
  
          $headers['X-Requested-With'] = "XMLHttpRequest";
          $request-&gt;headers-&gt;add($headers);
  
          /* This doesn't work */
          /*
          $controller = new Controller;
          $status = $controller-&gt;forward( 'HvHClientsBundle:Clients:getClients', array('request' =&gt; $request) );        
          */
  
          $clients_controller = new ClientsController();
          $status = $clients_controller-&gt;getClientsAction($request);
  
          $this-&gt;assertEquals(200, $status);
      }
  
  }
  </code></pre>
  
  <p><strong>Here is the part of the clients controller where it fails</strong></p>
  
  <pre><code>&lt;?php
  
  namespace HvH\ClientsBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use HvH\APIschemaBundle\Controller\Validation;
  
  
  //FOSRestBundle
  use FOS\RestBundle\View\View;
  
  class ClientsController extends Controller
  {
  
      //Query all clients
      public function getClientsAction(Request $request)
      {
          $request_type = $request-&gt;headers-&gt;get('X-Requested-With');
  
          if($request_type != 'XMLHttpRequest') {
              return $this-&gt;render('HvHDashboardBundle:Dashboard:dashboard.html.twig' );          
          }
  
          //get any query strings
          $query_strings = $request-&gt;query-&gt;all();
          $definition = $this-&gt;get("service.handler")-&gt;definition_handler(__CLASS__, __FUNCTION__);
          //once data has been prepared 
          return $this-&gt;get('fos_rest.view_handler')-&gt;handle($view);
  
      }    
  }
  </code></pre>