Skip to content

Commit 9084ab6

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor reword [Testing] Indicate how to use InMemoryUser in WebTestCase
2 parents ef297ab + 74a1a5f commit 9084ab6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

testing.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,29 @@ stores in the session of the test client. If you need to define custom
714714
attributes in this token, you can use the ``tokenAttributes`` argument of the
715715
:method:`Symfony\\Bundle\\FrameworkBundle\\KernelBrowser::loginUser` method.
716716

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::
729+
730+
.. code-block:: yaml
731+
732+
# config/packages/security.yaml
733+
when@test:
734+
security:
735+
users_in_memory:
736+
memory:
737+
users:
738+
admin: { password: password, roles: ROLE_ADMIN }
739+
717740
To set a specific firewall (``main`` is set by default)::
718741

719742
$client->loginUser($testUser, 'my_firewall');

0 commit comments

Comments
 (0)