I want to integrate getContainer() in WebTestCase I want to integrate getContainer() in WebTestCase symfony symfony

I want to integrate getContainer() in WebTestCase


To be able to use the container inside a WebTestCase you need first to boot the kernel with:

static::bootKernel($options);$container = static::$kernel->getContainer();


First of all, take a look at Accessing the Container in the Symfony Testing documentation.

The supplied answers may work, but you do not need to specifically boot your kernel if you are extending the WebTestCase class, as it is booted automatically when you create your client. I see you are using $this->client, which implies that you've defined a global client for the class in your setUp() function. If that's the case you simply need to do:

$container = $this->client->getContainer();$url = $container->get('router')->generate('name_route', array('parameter' => ' '));

If you haven't defined $this->client anywhere, then you'll need to change the above to

// if your class extended Symfony's standard WebTestCase, this would // instead be $client = static::clientClient();$client = static::makeClient();$container = $this->client->getContainer();$url = $container->get('router')->generate('name_route', array('parameter' => ' '));

Note that the Symfony documentation states:

It's highly recommended that a functional test only tests the Response. But under certain very rare circumstances, you might want to access some internal objects to write assertions. In such cases, you can access the Dependency Injection Container:

So according to Symfony you shouldn't really be accessing the container to generate your route there, and if you look at all of their other examples they would prefer you to call the path of the route rather than retrieve it by name, so in your case it would be:

$this->client->request('GET', '/path/for/your/route',    array(),    array(),    array(        'HTTP_parameter_Header' => 'parameterHeader',              ));


Just get AppKernel inside your test case

require_once dirname(__DIR__).'/../../app/AppKernel.php';

and the then get the container and everthing else

$kernel = new \AppKernel('test', true);$kernel->boot();$container = self::$kernel->getContainer();

I'd suggest you put this into a base class and extends it in all your test cases :)