8000 [Security][Testing] Documented KernelBrowser::loginUser() by wouterj · Pull Request #13362 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Security][Testing] Documented KernelBrowser::loginUser() #13362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .doctor-rst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ whitelist:
- ".. _`Deploying Symfony 4 Apps on Heroku`: https://devcenter.heroku.com/articles/deploying-symfony4"
- "// 224, 165, 141, 224, 164, 164, 224, 165, 135])"
- '.. versionadded:: 0.2' # MercureBundle
- 'provides a ``loginUser()`` method to simulate logging in in your functional'
1 change: 1 addition & 0 deletions _build/redirection_map
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,4 @@
/components/translation/custom_formats https://github.com/symfony/translation
/components/translation/custom_message_formatter https://github.com/symfony/translation
/components/notifier https://github.com/symfony/notifier
/testing/http_authentication /testing
2 changes: 2 additions & 0 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Now that Symfony knows *how* you want to encode the passwords, you can use the
``UserPasswordEncoderInterface`` service to do this before saving your users to
the database.

.. _user-data-fixture:

For example, by using :ref:`DoctrineFixturesBundle <doctrine-fixtures>`, you can
create dummy database users:

Expand Down
56 changes: 56 additions & 0 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,62 @@ command.
If the information you need to check is available from the profiler, use
it instead.

Logging in Users (Authentication)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 5.1

The ``loginUser()`` method was introduced in Symfony 5.1.

When you want to add functional tests for protected pages, you have to
first "login" as a user. Reproducing the actual steps - such as
submitting a login form - make a test very slow. For this reason, Symfony
provides a ``loginUser()`` method to simulate logging in in your functional
tests.

Instead of login in with real users, it's recommended to create a user only for
tests. You can do that with Doctrine :ref:`data fixtures <user-data-fixture>`,
to load the testing users only in the test database.

After loading users in your database, use your user repository to fetch
this user and use
:method:`$client->loginUser() <Symfony\\Bundle\\FrameworkBundle\\KernelBrowser::loginUser>`
to simulate a login request::

// tests/Controller/ProfileControllerTest.php
namespace App\Tests\Controller;

use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ProfileControllerTest extends WebTestCase
{
// ...

public function testVisitingWhileLoggedIn()
{
$client = static::createClient();
$userRepository = static::$container->get(UserRepository::class);

// retrieve the test user
$testUser = $userRepository->findOneByEmail('john.doe@example.com');

// simulate $testUser being logged in
$client->loginUser($testUser);

// test e.g. the profile page
$client->request('GET', '/profile');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello John!');
}
}

You can pass any
:class:`Symfony\\Component\\Security\\Core\\User\\UserInterface` instance to
``loginUser()``. This method creates a special
:class:`Symfony\\Bundle\\FrameworkBundle\\Test\\TestBrowserToken` object and
stores in the session of the test client.

Accessing the Profiler Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
130 changes: 0 additions & 130 deletions testing/http_authentication.rst

This file was deleted.

0