From e67e634da64d17fda54f13d7e07cce98d6d649e7 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Thu, 10 Sep 2020 11:00:23 -0500 Subject: [PATCH 1/2] Added logout function --- documentation.md | 9 ++++++++ src/Codeception/Module/Symfony.php | 37 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/documentation.md b/documentation.md index a7cbf282..b6a664e1 100644 --- a/documentation.md +++ b/documentation.md @@ -747,6 +747,15 @@ $I->haveHttpHeader('Client_Id', 'Codeception'); Invalidate previously cached routes. +### logout + +Invalidate the current session. +```php +logout(); +``` + + ### makeHtmlSnapshot Saves current page's HTML into a temprary file. diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 39756533..f9b278f4 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -770,4 +770,41 @@ private function getPossibleKernelClasses() return [$this->config['kernel_class']]; } + + /** + * Invalidate the current session. + * ```php + * logout(); + * ``` + */ + public function logout() + { + $container = $this->_getContainer(); + + if ($container->has('security.token_storage')) { + $tokenStorage = $this->grabService('security.token_storage'); + $tokenStorage->setToken(null); + } + + if (!$container->has('session')) { + return; + } + $session = $this->grabService('session'); + + $sessionName = $session->getName(); + $session->invalidate(); + + $cookieJar = $this->client->getCookieJar(); + foreach ($cookieJar->all() as $cookie) { + $cookieName = $cookie->getName(); + if ($cookieName === 'MOCKSESSID' || + $cookieName === 'REMEMBERME' || + $cookieName === $sessionName + ) { + $cookieJar->expire($cookieName); + } + } + $cookieJar->flushExpiredCookies(); + } } From 502bc1cb3f60a9c8bfed577e21d4eb7e8c39f358 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Fri, 11 Sep 2020 21:58:00 -0500 Subject: [PATCH 2/2] fails if you don't have the necessary services --- src/Codeception/Module/Symfony.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index f9b278f4..d5aca7d8 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -788,6 +788,7 @@ public function logout() } if (!$container->has('session')) { + $this->fail("Symfony container doesn't have 'session' service"); return; } $session = $this->grabService('session');