8000 minor #13362 [Security][Testing] Documented KernelBrowser::loginUser(… · symfony/symfony-docs@8649d07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8649d07

Browse files
committed
minor #13362 [Security][Testing] Documented KernelBrowser::loginUser() (wouterj)
This PR was merged into the master branch. Discussion ---------- [Security][Testing] Documented KernelBrowser::loginUser() Fixes #13329 I think this is common enough to be put in the main article :) Commits ------- 2c6d6d4 Documented KernelBrowser::loginUser()
2 parents a891aa2 + 2c6d6d4 commit 8649d07

File tree

5 files changed

+60
-130
lines changed

5 files changed

+60
-130
lines changed

.doctor-rst.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ whitelist:
8585
- ".. _`Deploying Symfony 4 Apps on Heroku`: https://devcenter.heroku.com/articles/deploying-symfony4"
8686
- "// 224, 165, 141, 224, 164, 164, 224, 165, 135])"
8787
- '.. versionadded:: 0.2' # MercureBundle
88+
- 'provides a ``loginUser()`` method to simulate logging in in your functional'

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,4 @@
480480
/components/translation/custom_formats https://github.com/symfony/translation
481481
/components/translation/custom_message_formatter https://github.com/symfony/translation
482482
/components/notifier https://github.com/symfony/notifier
483+
/testing/http_authentication /testing

security.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ Now that Symfony knows *how* you want to encode the passwords, you can use the
172172
``UserPasswordEncoderInterface`` service to do this before saving your users to
173173
the database.
174174

175+
.. _user-data-fixture:
176+
175177
For example, by using :ref:`DoctrineFixturesBundle <doctrine-fixtures>`, you can
176178
create dummy database users:
177179

testing.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,62 @@ command.
567567
If the information you need to check is available from the profiler, use
568568
it instead.
569569

570+
Logging in Users (Authentication)
571+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
572+
573+
.. versionadded:: 5.1
574+
575+
The ``loginUser()`` method was introduced in Symfony 5.1.
576+
577+
When you want to add functional tests for protected pages, you have to
578+
first "login" as a user. Reproducing the actual steps - such as
579+
submitting a login form - make a test very slow. For this reason, Symfony
580+
provides a ``loginUser()`` method to simulate logging in in your functional
581+
tests.
582+
583+
Instead of login in with real users, it's recommended to create a user only for
584+
tests. You can do that with Doctrine :ref:`data fixtures <user-data-fixture>`,
585+
to load the testing users only in the test database.
586+
587+
After loading users in your database, use your user repository to fetch
588+
this user and use
589+
:method:`$client->loginUser() <Symfony\\Bundle\\FrameworkBundle\\KernelBrowser::loginUser>`
590+
to simulate a login request::
591+
592+
// tests/Controller/ProfileControllerTest.php
593+
namespace App\Tests\Controller;
594+
595+
use App\Repository\UserRepository;
596+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
597+
598+
class ProfileControllerTest extends WebTestCase
599+
{
600+
// ...
601+
602+
public function testVisitingWhileLoggedIn()
603+
{
604+
$client = static::createClient();
605+
$userRepository = static::$container->get(UserRepository::class);
606+
607+
// retrieve the test user
608+
$testUser = $userRepository->findOneByEmail('john.doe@example.com');
609+
610+
// simulate $testUser being logged in
611+
$client->loginUser($testUser);
612+
613+
// test e.g. the profile page
614+
$client->request('GET', '/profile');
615+
$this->assertResponseIsSuccessful();
616+
$this->assertSelectorTextContains('h1', 'Hello John!');
617+
}
618+
}
619+
620+
You can pass any
621+
:class:`Symfony\\Component\\Security\\Core\\User\\UserInterface` instance to
622+
``loginUser()``. This method creates a special
623+
:class:`Symfony\\Bundle\\FrameworkBundle\\Test\\TestBrowserToken` object and
624+
stores in the session of the test client.
625+
570626
Accessing the Profiler Data
571627
~~~~~~~~~~~~~~~~~~~~~~~~~~~
572628

testing/http_authentication.rst

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0