Twig is_granted fails in Behat scenario Twig is_granted fails in Behat scenario symfony symfony

Twig is_granted fails in Behat scenario


My guess is that you're using a different instance of the container in your behat step and in your template.

AFAIR, the symfony2 driver uses BrowserKit under the hood to navigate through your website. The container which will be used in your web page will then be instanciated by the PHP Engine of your Web server (and not by Behat). If so, it is absolutely impossible to operate modifications in the container at runtime in a step and expect that the web server will be aware of them.

Easy solution would be to actually log in in the behat step (through the web interface) instead of setting the token manually.

Another harder way, if you absolutely want to login programatically, would be to serialize the created token on HDD and register some kind of logic (a kernel.request listener for example) that will check if this file is available and inject the unserialized token in the security context. If you do so, MAKE SURE that you enable this logic in TEST environment only, as it potentially is a security breach.


The problem is you have running 2 instances of Symfony:

  • One core for Behat, that was initialized.
  • Second, initialized by apache/nginx that was triggered by Mink connection to the server.

Solution

For that, we had a solution in another project (with Zend).We created service, that created an additional configuration to authorization:

  • if a file exists and the project was in DEV mode, then it was loaded in the initialization step.

Then in hook/step we could call service that generates a file like that and after scenario, delete it. This way, you could have any logged user in your project.

Another way is to call steps that will log you into your project via a standard form.