Using Sessions in WebTestCase not working #45662
-
SummaryYes, I know that
thrown by this: $session = $this->client->getContainer()->get('session'); Detailsclass BookingControllerTest extends WebTestCase {
// ...
public function testToAddItemToCart()
{
$this->client = static::createClient();
self::ensureKernelShutdown();
$item = (ProductFactory::random())->object();
// make REQUEST (which adds stuff into the session)
$this->client->request('GET', "/cart/add/{$item->getSlug()}");
// assert RESPONSE
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertTrue($this->client->getResponse()->isRedirect('/cart/show'));
// get cart from SESSION
$session = $this->client->getContainer()->get('session');
$cart = $session->get('cart');
// assert content of cart
// ...
}
} What I didI found this article Symfony Session testing, but it does not explain how to uses these objects. // tried to replace this line
$session = $this->client->getContainer()->get('session');
// by this and ...
$session = $this->client->getContainer()->get('request_stack')->getSession();
// by this and ...
$session = $this->client->getContainer()->get(RequestStack::class)->getSession();
// by that
$session = new Session(new MockFileSessionStorage()); QuestionI have no idea how to solve this deprecation nor found any similar example. What would be the correct approach here? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
Container get method is deprecated. Use dependency injection: public function someControllerAction(RequestStack $requestStack)
{
$session = $requestStack->getSession();
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Hey there, I found a way to access the controller session in my tests. It is heavily based on insights from @chyshyya and from your own code @DigitalTimK, thanks to both of you 🙏 I have to tell you that I'm using Symfony 6.1 and my solution may not totally fix your case as you're using Symfony 5.4. I hope it will! There were two missing things in your previous attempts:
I extracted my code in a trait so it can be easily reused in my tests. <?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
trait SessionHelper
{
public function createSession(KernelBrowser $client): Session
{
$cookie = $client->getCookieJar()->get('MOCKSESSID');
// create a new session object
$container = static::getContainer();
$session = $container->get('session.factory')->createSession();
if ($cookie) {
// get the session id from the session cookie if it exists
$session->setId($cookie->getValue());
$session->start();
} else {
// or create a new session id and a session cookie
$session->start();
$session->save();
$sessionCookie = new Cookie(
$session->getName(),
$session->getId(),
null,
null,
'localhost',
);
$client->getCookieJar()->set($sessionCookie);
}
return $session;
}
} Then it can be used this way: <?php
namespace App\Tests\Controller;
use App\Tests\SessionHelper;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SessionControllerTest extends WebTestCase
{
use SessionHelper;
public function testUpdateLocaleSavesTheLocaleInTheSession(): void
{
$client = static::createClient();
$session = $this->createSession($client);
$client->request('POST', '/session/locale', [
'locale' => 'fr_FR',
]);
$this->assertSame('fr_FR', $session->get('_locale'));
}
} I hope my solution will help you! |
Beta Was this translation helpful? Give feedback.
Hey there, I found a way to access the controller session in my tests. It is heavily based on insights from @chyshyya and from your own code @DigitalTimK, thanks to both of you 🙏
I have to tell you that I'm using Symfony 6.1 and my solution may not totally fix your case as you're using Symfony 5.4. I hope it will!
There were two missing things in your previous attempts:
localhost
)new MockFileSessionStorage()
didn't create the session file in the same directory as the one of the controller session (it took me a while to figure it out), which is defined by thesession.save_path
configuration keyI extracted my code in a trait so it can be easily reus…