diff --git a/testing.rst b/testing.rst index 993e7878d46..583d2acfcbf 100644 --- a/testing.rst +++ b/testing.rst @@ -750,6 +750,30 @@ To set a specific firewall (``main`` is set by default):: By design, the ``loginUser()`` method doesn't work when using stateless firewalls. Instead, add the appropriate token/header in each ``request()`` call. +.. _testing_setup_the_session: + +Setup the session +................. + +The client provides a ``getSession()`` method, which allows you to setup the session before performing the request:: + + // tests/Controller/FormControllerTest.php + use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + + class FormControllerTest extends WebTestCase + { + public function testSetupCsrfTokenBeforeFormSubmit(): void + { + $client = self::createClient(); + + $session = $client->getSession(); + $session->set('_csrf/form', 'fhr8d5sha3a69tpv24s5'); + $session->save(); + + $client->request('POST', '/form', ['form' => ['_token' => 'fhr8d5sha3a69tpv24s5']]); + } + } + Making AJAX Requests ....................