There is another solution posted in the Github discussion that doesn't require rewriting the loginUser
(Taken from https://github.com/symfony/symfony/discussions/46961#discussioncomment-4573371 )
<?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;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
trait SessionHelper
{
public function getSession(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;
}
public function generateCsrfToken(KernelBrowser $client, string $tokenId): string
{
$session = $this->getSession($client);
$container = static::getContainer();
$tokenGenerator = $container->get('security.csrf.token_generator');
$csrfToken = $tokenGenerator->generateToken();
$session->set(SessionTokenStorage::SESSION_NAMESPACE . "/{$tokenId}", $csrfToken);
$session->save();
return $csrfToken;
}
}
Used like this:
<?php
namespace App\Tests\Controller;
use App\Tests\SessionHelper;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SessionControllerTest extends WebTestCase
{
use SessionHelper;
public function testSomething(): void
{
$client = static::createClient();
$client->request('POST', '/something', [
'_csrf_token' => $this->generateCsrfToken($client, 'expected token id'),
]);
// assert something
}
}