FriendsOfBehat / SymfonyExtension

:musical_score: Extension integrating Behat with Symfony.
MIT License
472 stars 62 forks source link

getSession returns null when retrieving from requestStack in Symfony 5.3 #172

Closed samuelvi closed 2 years ago

samuelvi commented 2 years ago

Hi, The scenario is as follows:

At this point $this->requestStack->getSession() always returns null. The following error is gotten when trying to retrieve the session from the requestStack instance:

There is currently no session available. (Symfony\Component\HttpFoundation\Exception\SessionNotFoundException)

Thanks!

P.S: Also tried to inject the requestStack via constructor but the same error happens.

samuelvi commented 2 years ago

I just realized that retrieving the session service directly from the container works.

So previous to SF 5.3:

public function __construct(SessionInterface $session) 
{
  $this-session = $session;
}

public function getWebSession() : SessionInterface 
{
  return $this->session;
}

And since SF 5.3:

public function __construct(KernelInterface $kernel) 
{
  $this->kernel = $kernel;
}

public function getWebSession() : SessionInterface 
{
  return $this->kernel->getContainer()->get('session');
}

Sorry about the confusion, thanks!