You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm posting this issue from stackoverflow as i've got the same problem and no one seems do understand why this is happening. Maybe you can give us more insights on this one.
Stack overflow issue
In our Symfony applications we have custom code that handles authentication. It's based on top of thephpleague/oauth2-server.
We have 2 types of authentication, both of which end up calling TokenStorage::setToken().
All this works perfectly.
Now we decided to add some Behat tests to our bundle:
Scenario: I am able to login with type 1
When I login with type 1
Then I am successfully logged in as a "type1" member
Scenario: I am able to login with type 2
When I login with type 2
Then I am successfully logged in as a "type2" member
(I changed the actual wording of the scenarios in order not to expose business-specific terms)
In order to test the Then steps, I need access to the TokenStorage in my Contexts because I basically want to test if the user has the correct security role:
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
useBehat\Behat\Context\Context;
class LoginContext implements Context
{
private$tokenStorage;
publicfunction__construct(TokenStorage$tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/** * @Then I am successfully logged in as a :expectedRole member */publicfunctioniAmSuccessfullyLoggedInAsAMember($expectedRole)
{
$found = false;
foreach ($this->tokenStorage->getToken()->getRoles() as$role) {
if ($role->getRole() == $expectedRole) {
$found = true;
break;
}
}
if (! $found) {
thrownew \Exception('Incorrect role');
}
}
}
What happens is:
if I run only the first scenario, it works OK
if I run only the second scenario, it works OK
if I run both of them, then on the 2nd scenario I get the error Call to a member function getRoles() on a non-object
Why does this happen ? How can I make it work correctly ?
My versions are:
behat/behat: 3.1.0
behat/gherkin: 4.4.1
behat/symfony2-extension: 2.1.1
symfony/symfony: 2.6.13
One solution I tried is to have my context implement the Behat\Symfony2Extension\Context\KernelAwareContext interface instead and then I did this:
Hey @gcavana. That's an interesting question, my guess is that the first step produces some side-effects that are conflicting with the 2nd request. That is why we often have to restart the Symfony kernel across requests (it's the default in Symfony's FrameworkBundle Client).
I can't tell you what's wrong with that small amount of details, but 2 things: ensure that your kernel is restarted between your scenarios. If not, using var_dumps or any debugger, have a look if anything is calling setToken with a null value on your SecurityStorage (with an xdebug_print_function_stack() function call if you'll see who is calling it easily). If all of that do not give you the answer, could you put somewhere a bit of code providing this problem and I can have a look.
Hi everyone,
I'm posting this issue from stackoverflow as i've got the same problem and no one seems do understand why this is happening. Maybe you can give us more insights on this one.
Stack overflow issue
In our Symfony applications we have custom code that handles authentication. It's based on top of thephpleague/oauth2-server.
We have 2 types of authentication, both of which end up calling TokenStorage::setToken().
All this works perfectly.
Now we decided to add some Behat tests to our bundle:
In order to test the Then steps, I need access to the TokenStorage in my Contexts because I basically want to test if the user has the correct security role:
My context looks like this:
What happens is:
if I run only the first scenario, it works OK
if I run only the second scenario, it works OK
if I run both of them, then on the 2nd scenario I get the error Call to a member function getRoles() on a non-object
Why does this happen ? How can I make it work correctly ?
My versions are:
My thinking was that, by explicitly retrieving TokenStorage before each scenario, I would get a fresh instance each time and therefore it would work.
However, it behaved exactly the same :( .
What am I missing?
The text was updated successfully, but these errors were encountered: