@@ -714,7 +714,18 @@ stores in the session of the test client. If you need to define custom
714
714
attributes in this token, you can use the ``tokenAttributes `` argument of the
715
715
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ KernelBrowser::loginUser ` method.
716
716
717
- You can also use :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ InMemoryUser ` but you have to define those in the configuration first::
717
+ You can also use an :ref: `in-memory user <security-memory-user-provider >` in your tests
718
+ by instantiating :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ InMemoryUser ` directly::
719
+
720
+ // tests/Controller/ProfileControllerTest.php
721
+ use Symfony\Component\Security\Core\User\InMemoryUser;
722
+
723
+ $client = static::createClient();
724
+ $testUser = new InMemoryUser('admin', 'password', ['ROLE_ADMIN']);
725
+ $client->loginUser($testUser);
726
+
727
+ Before doing this, you must define the in-memory user in your test environment
728
+ configuration to ensure it exists and can be authenticated::
718
729
719
730
.. code-block :: yaml
720
731
@@ -726,15 +737,6 @@ You can also use :class:`Symfony\\Component\\Security\\Core\\User\\InMemoryUser`
726
737
users :
727
738
admin : { password: password, roles: ROLE_ADMIN }
728
739
729
- and then log the user with::
730
-
731
- // tests/Controller/ProfileControllerTest.php
732
- use Symfony\Component\Security\Core\User\InMemoryUser;
733
-
734
- $client = static::createClient();
735
- $testUser = new InMemoryUser('admin', 'password', ['ROLE_ADMIN']);
736
- $client->loginUser($testUser);
737
-
738
740
To set a specific firewall (``main `` is set by default)::
739
741
740
742
$client->loginUser($testUser, 'my_firewall');
0 commit comments