4
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

A.L
  • 10,259
  • 10
  • 67
  • 98
ichrak ABID
  • 81
  • 1
  • 4
  • 1
    Your error message implies that $this is not an object which seems strange. Are you sure the error is coming from your posted code? I have not used the Liip bundle. As several other people have suggested I suspect you need to boot the kernel but am not positive. The Liip bundle might take care of that. – Cerad Jan 29 '16 at 13:58
  • Just an FYI you also have a spelling error, `'paramterHeader'` should be `'parameterHeader'` – Jason Roman Jan 29 '16 at 15:37

4 Answers4

4

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();
t j
  • 7,026
  • 12
  • 46
  • 66
Benoît
  • 594
  • 4
  • 11
2

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',          
    )
);
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
1

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 :)

t j
  • 7,026
  • 12
  • 46
  • 66
Omar Alves
  • 763
  • 5
  • 13
  • By the way, don't forget to set the right path for AppKernel.php – Omar Alves Jan 29 '16 at 19:46
  • for some reason this is the only solution that doesn't give me errors. When I setup the repository [this way](http://symfony.com/doc/current/cookbook/testing/doctrine.html) I get the error `Resetting the container is not allowed when a scope is active.` – brietsparks May 12 '16 at 19:33
0
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',          
        ]
    );
}
t j
  • 7,026
  • 12
  • 46
  • 66
geoB
  • 4,578
  • 5
  • 37
  • 70