4
4
5
5
use Codeception \Configuration ;
6
6
use Codeception \Exception \ModuleException ;
7
- use Codeception \Lib \Framework ;
8
7
use Codeception \Exception \ModuleRequireException ;
9
8
use Codeception \Lib \Connector \Symfony as SymfonyConnector ;
9
+ use Codeception \Lib \Framework ;
10
10
use Codeception \Lib \Interfaces \DoctrineProvider ;
11
11
use Codeception \Lib \Interfaces \PartedModule ;
12
+ use Symfony \Bundle \FrameworkBundle \Console \Application ;
13
+ use Symfony \Component \BrowserKit \Cookie ;
12
14
use Symfony \Component \Console \Tester \CommandTester ;
13
- use Symfony \Component \Finder \Finder ;
14
15
use Symfony \Component \DependencyInjection \ContainerInterface ;
15
- use Symfony \Component \Finder \SplFileInfo ;
16
+ use Symfony \Component \Finder \Finder ;
16
17
use 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 ;
17
21
use 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 ;
21
22
22
23
/**
23
24
* This module uses Symfony Crawler and HttpKernel to emulate requests and test response.
117
118
*/
118
119
class Symfony extends Framework implements DoctrineProvider, PartedModule
119
120
{
120
- public const SWIFTMAILER = 'swiftmailer ' ;
121
- public const SYMFONY_MAILER = 'symfony_mailer ' ;
121
+ const SWIFTMAILER = 'swiftmailer ' ;
122
+ const SYMFONY_MAILER = 'symfony_mailer ' ;
122
123
123
124
private static $ possibleKernelClasses = [
124
125
'AppKernel ' , // Symfony Standard
@@ -139,7 +140,8 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
139
140
'cache_router ' => false ,
140
141
'em_service ' => 'doctrine.orm.entity_manager ' ,
141
142
'rebootable_client ' => true ,
142
- 'mailer ' => self ::SWIFTMAILER
143
+ 'mailer ' => self ::SWIFTMAILER ,
144
+ 'guard ' => false
143
145
];
144
146
145
147
/**
@@ -1069,4 +1071,32 @@ public function seeCurrentActionIs($action)
1069
1071
}
1070
1072
$ this ->fail ("Action ' $ action' does not exist " );
1071
1073
}
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
+ }
1072
1102
}
0 commit comments