|
10 | 10 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
11 | 11 | use Symfony\Component\Security\Core\User\UserInterface; |
12 | 12 | use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; |
| 13 | +use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator; |
13 | 14 | use function is_int; |
14 | 15 | use function serialize; |
15 | 16 |
|
@@ -81,14 +82,41 @@ public function dontSeeInSession(string $attribute, $value = null): void |
81 | 82 | } |
82 | 83 |
|
83 | 84 | /** |
84 | | - * Invalidate the current session. |
| 85 | + * Go to the configured logout url (by default: `/logout`). |
| 86 | + * This method includes redirection to the destination page configured after logout. |
| 87 | + * |
| 88 | + * See the Symfony documentation on ['Logging Out'](https://symfony.com/doc/current/security.html#logging-out). |
| 89 | + */ |
| 90 | + public function goToLogoutPath(): void |
| 91 | + { |
| 92 | + $logoutUrlGenerator = $this->getLogoutUrlGenerator(); |
| 93 | + $logoutPath = $logoutUrlGenerator->getLogoutPath(); |
| 94 | + $this->amOnPage($logoutPath); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Alias method for [`logoutProgrammatically()`](https://codeception.com/docs/modules/Symfony#logoutProgrammatically) |
85 | 99 | * |
86 | 100 | * ```php |
87 | 101 | * <?php |
88 | 102 | * $I->logout(); |
89 | 103 | * ``` |
90 | 104 | */ |
91 | 105 | public function logout(): void |
| 106 | + { |
| 107 | + $this->logoutProgrammatically(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Invalidates the current user's session and expires the session cookies. |
| 112 | + * This method does not include any redirects after logging out. |
| 113 | + * |
| 114 | + * ```php |
| 115 | + * <?php |
| 116 | + * $I->logoutProgrammatically(); |
| 117 | + * ``` |
| 118 | + */ |
| 119 | + public function logoutProgrammatically(): void |
92 | 120 | { |
93 | 121 | if ($tokenStorage = $this->getTokenStorage()) { |
94 | 122 | $tokenStorage->setToken(); |
@@ -164,6 +192,11 @@ protected function getTokenStorage(): ?TokenStorageInterface |
164 | 192 | return $this->getService('security.token_storage'); |
165 | 193 | } |
166 | 194 |
|
| 195 | + protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator |
| 196 | + { |
| 197 | + return $this->getService('security.logout_url_generator'); |
| 198 | + } |
| 199 | + |
167 | 200 | protected function getCurrentSession(): SessionInterface |
168 | 201 | { |
169 | 202 | return $this->grabService('session'); |
|
0 commit comments