How to inject a Request object into a Twig template from a CLI unit test?

121 views Asked by At

I'm trying to unit test some Symfony 2.8 code that uses a Twig 1.24 template (I'm not actually testing the template). The template has a hard coded reference to app.request, which doesn't exist under the CLI:

{%
set currentPath = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params'))
%}

I've tried injecting a new Request object into the container from within the test method:

    $container->enterScope('request');
    $container->set('request', new Request(), 'request');

as recommended by other Symfony 2 answers, but I still get the following error:

1) InvoiceServiceTest: Should generate a pdf invoice
 Test  tests\unit\HMRX\CoreBundle\Services\InvoiceServiceTest.php:testShouldGenerateAPdfInvoice

  [Twig_Error_Runtime] Impossible to access an attribute ("attributes") on a null variable in "HMRXAppBundle:Invoice:invoice.html.twig" at line 2

How can I get around this limitation and get a Request object in the template under test? I don't think it needs to be particularly valid, it just needs to exist.

0

There are 0 answers