@@ -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