Skip to content

Commit 5ac2186

Browse files
TavoNiievezNaktibalda
authored andcommitted
Added logout function
1 parent c1280aa commit 5ac2186

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

documentation.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,15 @@ $I->haveHttpHeader('Client_Id', 'Codeception');
747747
Invalidate previously cached routes.
748748
749749
750+
### logout
751+
752+
Invalidate the current session.
753+
```php
754+
<?php
755+
$I->logout();
756+
```
757+
758+
750759
### makeHtmlSnapshot
751760
752761
Saves current page's HTML into a temprary file.

src/Codeception/Module/Symfony.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,15 +787,14 @@ private function getPossibleKernelClasses()
787787
*/
788788
public function seeNumRecords($expectedNum, $className, $criteria = [])
789789
{
790-
$em = $this->_getEntityManager();
790+
$em = $this->_getEntityManager();
791791
$repository = $em->getRepository($className);
792792

793793
if (empty($criteria)) {
794-
$currentNum = (int) $repository->createQueryBuilder('a')
794+
$currentNum = (int)$repository->createQueryBuilder('a')
795795
->select('count(a.id)')
796796
->getQuery()
797-
->getSingleScalarResult()
798-
;
797+
->getSingleScalarResult();
799798
} else {
800799
$currentNum = $repository->count($criteria);
801800
}
@@ -809,4 +808,43 @@ public function seeNumRecords($expectedNum, $className, $criteria = [])
809808
)
810809
);
811810
}
811+
812+
/**
813+
* Invalidate the current session.
814+
* ```php
815+
* <?php
816+
* $I->logout();
817+
* ```
818+
*/
819+
public function logout()
820+
{
821+
$container = $this->_getContainer();
822+
823+
if ($container->has('security.token_storage')) {
824+
$tokenStorage = $this->grabService('security.token_storage');
825+
$tokenStorage->setToken(null);
826+
}
827+
828+
if (!$container->has('session')) {
829+
$this->fail("Symfony container doesn't have 'session' service");
830+
return;
831+
}
832+
$session = $this->grabService('session');
833+
834+
$sessionName = $session->getName();
835+
$session->invalidate();
836+
837+
$cookieJar = $this->client->getCookieJar();
838+
foreach ($cookieJar->all() as $cookie) {
839+
$cookieName = $cookie->getName();
840+
if ($cookieName === 'MOCKSESSID' ||
841+
$cookieName === 'REMEMBERME' ||
842+
$cookieName === $sessionName
843+
) {
844+
$cookieJar->expire($cookieName);
845+
}
846+
}
847+
$cookieJar->flushExpiredCookies();
848+
849+
}
812850
}

0 commit comments

Comments
 (0)