44
55use Codeception \Configuration ;
66use Codeception \Exception \ModuleException ;
7- use Codeception \Lib \Framework ;
87use Codeception \Exception \ModuleRequireException ;
98use Codeception \Lib \Connector \Symfony as SymfonyConnector ;
9+ use Codeception \Lib \Framework ;
1010use Codeception \Lib \Interfaces \DoctrineProvider ;
1111use Codeception \Lib \Interfaces \PartedModule ;
12+ use Symfony \Bundle \FrameworkBundle \Console \Application ;
13+ use Symfony \Component \BrowserKit \Cookie ;
1214use Symfony \Component \Console \Tester \CommandTester ;
13- use Symfony \Component \Finder \Finder ;
1415use Symfony \Component \DependencyInjection \ContainerInterface ;
15- use Symfony \Component \Finder \SplFileInfo ;
16+ use Symfony \Component \Finder \Finder ;
1617use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
18+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
19+ use Symfony \Component \Security \Core \User \UserInterface ;
20+ use Symfony \Component \Security \Guard \Token \PostAuthenticationGuardToken ;
1721use Symfony \Component \VarDumper \Cloner \Data ;
18- use Symfony \Bundle \FrameworkBundle \Console \Application ;
19- use Symfony \Component \Console \Input \ArrayInput ;
20- use Symfony \Component \Console \Output \BufferedOutput ;
2122
2223/**
2324 * This module uses Symfony Crawler and HttpKernel to emulate requests and test response.
117118 */
118119class Symfony extends Framework implements DoctrineProvider, PartedModule
119120{
120- public const SWIFTMAILER = 'swiftmailer ' ;
121- public const SYMFONY_MAILER = 'symfony_mailer ' ;
121+ const SWIFTMAILER = 'swiftmailer ' ;
122+ const SYMFONY_MAILER = 'symfony_mailer ' ;
122123
123124 private static $ possibleKernelClasses = [
124125 'AppKernel ' , // Symfony Standard
@@ -139,7 +140,8 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
139140 'cache_router ' => false ,
140141 'em_service ' => 'doctrine.orm.entity_manager ' ,
141142 'rebootable_client ' => true ,
142- 'mailer ' => self ::SWIFTMAILER
143+ 'mailer ' => self ::SWIFTMAILER ,
144+ 'guard ' => false
143145 ];
144146
145147 /**
@@ -1069,4 +1071,32 @@ public function seeCurrentActionIs($action)
10691071 }
10701072 $ this ->fail ("Action ' $ action' does not exist " );
10711073 }
1074+
1075+ public function amLoggedInAs (UserInterface $ user , $ firewallName = 'main ' , $ firewallContext = null )
1076+ {
1077+ $ container = $ this ->_getContainer ();
1078+ if (!$ container ->has ('session ' )) {
1079+ $ this ->fail ("Symfony container doesn't have 'session' service " );
1080+ return ;
1081+ }
1082+
1083+ $ session = $ this ->grabService ('session ' );
1084+
1085+ if ($ this ->config ['guard ' ]) {
1086+ $ token = new PostAuthenticationGuardToken ($ user , $ firewallName , $ user ->getRoles ());
1087+ } else {
1088+ $ token = new UsernamePasswordToken ($ user , null , $ firewallName , $ user ->getRoles ());
1089+ }
1090+
1091+ if ($ firewallContext ) {
1092+ $ session ->set ('_security_ ' .$ firewallContext , serialize ($ token ));
1093+ } else {
1094+ $ session ->set ('_security_ ' .$ firewallName , serialize ($ token ));
1095+ }
1096+
1097+ $ session ->save ();
1098+
1099+ $ cookie = new Cookie ($ session ->getName (), $ session ->getId ());
1100+ $ this ->client ->getCookieJar ()->set ($ cookie );
1101+ }
10721102}
0 commit comments