Reputation: 81
use Liip\FunctionalTestBundle\Test\WebTestCase;
class ControllerTest extends WebTestCase
{
public function testSuccess()
{
$url = $this->getContainer()->get('router')
->generate('name_route', array('parameter' => ' '));
$this->client->request('GET', $url,
array(),
array(),
array(
'HTTP_parameter_Header' => 'paramterHeader',
)
);
}
}
After I launched the test, I have an error:
Call to a member function getContainer() on a non-object
Upvotes: 4
Views: 5628
Reputation: 1
You can do it like this:
$router = $this->getContainer()->get('router');
$this->client->request(Request::METHOD_GET,$router->generate('homepage'));
Upvotes: -1
Reputation: 602
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();
Upvotes: 4
Reputation: 763
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 :)
Upvotes: 1
Reputation: 4704
public function testSuccess()
{
self::bootKernel();
$this->router = static::$kernel->getContainer()
->get('router');
$url = $this->router->generate(
'name_route',
['parameter' => ' ']
);
$this->client->request(
'GET',
$url,
[],
[],
[
'HTTP_parameter_Header' => 'parameterHeader',
]
);
}
Upvotes: 0
Reputation: 8276
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',
)
);
Upvotes: 2