luqo33
luqo33

Reputation: 8361

Symfony2 TokenStorage is always empty in Behat FeatureContext

I've set up Behat with Symfony2Extension in order to start testing the application.

Having set up the SF2Extension, I'm able to inject Symofny services into my Behat Context:

contexts:
    - My\AppBundle\Features\Security\Context\FeatureContext:
        session:    '@session'
        tokenStorage:   '@security.token_storage'

The FeatureContext's constructor:

public function __construct(Session $session, TokenStorage $tokenStorage)

Now the problem that I cannot overcome is that $tokenStorage is always empty when the tests/scenarios are executing (they're executing with Selenium in a normal browser). My scenarios have a user log-in to the SF2 application successfully, so there exists a token and a non-empty TokenStorage.

Although the $tokenStorage object is injected into the Context, it seems to have nothing to do with the actual application being run at the moment - it's always empty.

I'd appreciate your advice. Thanks.

Upvotes: 1

Views: 344

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36201

The service that's injected by Behat into your context is not the same as the one used by your application. Behat boots its own instance of the Symfony service container. Since you haven't executed the authentication process in your Behat context, the authentication token wasn't created.

Since you use the selenium driver, you don't really use the Symfony extension to make requests and you don't have access to the application other then through its UI (html).

Even if you used the Symfony2Extension to make requests (by configuring the symfony2 driver), it boots a new instance of the container on each request. What you'd be able to do is to access the Symfony profiler, which might provide you some of the information you need.

Upvotes: 2

Related Questions