13
13
14
14
use Symfony \Component \DependencyInjection \Reference ;
15
15
use Symfony \Component \DependencyInjection \ContainerBuilder ;
16
+ use Symfony \Component \DependencyInjection \DefinitionDecorator ;
16
17
17
18
class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
18
19
{
19
20
public function testCreate ()
20
21
{
21
- list ($ container ,
22
- $ authProviderId ,
23
- $ listenerId ,
24
- $ entryPointId
25
- ) = $ this ->callFactory ('foo ' , array ('use_forward ' => true , 'failure_path ' => '/foo ' , 'success_handler ' => 'qux ' , 'failure_handler ' => 'bar ' , 'remember_me ' => true ), 'user_provider ' , 'entry_point ' );
22
+ list ($ container , $ authProviderId , $ listenerId , $ entryPointId ) = $ this ->callFactory ('foo ' , array (
23
+ 'use_forward ' => true ,
24
+ 'failure_path ' => '/foo ' ,
25
+ 'success_handler ' => 'custom_success_handler ' ,
26
+ 'failure_handler ' => 'custom_failure_handler ' ,
27
+ 'remember_me ' => true ,
28
+ ), 'user_provider ' , 'entry_point ' );
26
29
27
30
// auth provider
28
31
$ this ->assertEquals ('auth_provider ' , $ authProviderId );
@@ -33,41 +36,95 @@ public function testCreate()
33
36
$ definition = $ container ->getDefinition ('abstract_listener.foo ' );
34
37
$ this ->assertEquals (array (
35
38
'index_4 ' => 'foo ' ,
36
- 'index_5 ' => new Reference ('qux ' ),
37
- 'index_6 ' => new Reference ('bar ' ),
39
+ 'index_5 ' => new Reference ('custom_success_handler ' ),
40
+ 'index_6 ' => new Reference ('custom_failure_handler ' ),
38
41
'index_7 ' => array (
39
- 'use_forward ' => true ,
42
+ 'use_forward ' => true ,
40
43
),
41
44
), $ definition ->getArguments ());
42
45
43
46
// entry point
44
47
$ this ->assertEquals ('entry_point ' , $ entryPointId , '->create() does not change the default entry point. ' );
45
48
}
46
49
47
- public function testDefaultFailureHandler ()
50
+ /**
51
+ * @dataProvider getFailureHandlers
52
+ */
53
+ public function testDefaultFailureHandler ($ handlerId , $ serviceId , $ defaultHandlerInjection )
48
54
{
49
- list ($ container ,
50
- $ authProviderId ,
51
- $ listenerId ,
52
- $ entryPointId
53
- ) = $ this ->callFactory ('foo ' , array ('remember_me ' => true ), 'user_provider ' , 'entry_point ' );
55
+ $ options = array (
56
+ 'remember_me ' => true ,
57
+ 'login_path ' => '/bar ' ,
58
+ );
59
+
60
+ if ($ serviceId ) {
61
+ $ options ['failure_handler ' ] = $ serviceId ;
62
+ }
63
+
64
+ list ($ container , $ authProviderId , $ listenerId , $ entryPointId ) = $ this ->callFactory ('foo ' , $ options , 'user_provider ' , 'entry_point ' );
54
65
55
66
$ definition = $ container ->getDefinition ('abstract_listener.foo ' );
56
67
$ arguments = $ definition ->getArguments ();
57
- $ this ->assertEquals (new Reference ('security.authentication.failure_handler.foo.abstract_factory ' ), $ arguments ['index_6 ' ]);
68
+ $ this ->assertEquals (new Reference ($ handlerId ), $ arguments ['index_6 ' ]);
69
+ $ failureHandler = $ container ->findDefinition ((string ) $ arguments ['index_6 ' ]);
70
+
71
+ $ methodCalls = $ failureHandler ->getMethodCalls ();
72
+ if ($ defaultHandlerInjection ) {
73
+ $ this ->assertEquals ('setOptions ' , $ methodCalls [0 ][0 ]);
74
+ $ this ->assertEquals (array ('login_path ' => '/bar ' ), $ methodCalls [0 ][1 ][0 ]);
75
+ } else {
76
+ $ this ->assertCount (0 , $ methodCalls );
77
+ }
78
+ }
79
+
80
+ public function getFailureHandlers ()
81
+ {
82
+ return array (
83
+ array ('security.authentication.failure_handler.foo.abstract_factory ' , null , true ),
84
+ array ('custom_failure_handler_default ' , 'custom_failure_handler_default ' , true ),
85
+ array ('custom_failure_handler ' , 'custom_failure_handler ' , false ),
86
+ );
58
87
}
59
88
60
- public function testDefaultSuccessHandler ()
89
+ /**
90
+ * @dataProvider getSuccessHandlers
91
+ */
92
+ public function testDefaultSuccessHandler ($ handlerId , $ serviceId , $ defaultHandlerInjection )
61
93
{
62
- list ($ container ,
63
- $ authProviderId ,
64
- $ listenerId ,
65
- $ entryPointId
66
- ) = $ this ->callFactory ('foo ' , array ('remember_me ' => true ), 'user_provider ' , 'entry_point ' );
94
+ $ options = array (
95
+ 'remember_me ' => true ,
96
+ 'default_target_path ' => '/bar ' ,
97
+ );
98
+
99
+ if ($ serviceId ) {
100
+ $ options ['success_handler ' ] = $ serviceId ;
101
+ }
102
+
103
+ list ($ container , $ authProviderId , $ listenerId , $ entryPointId ) = $ this ->callFactory ('foo ' , $ options , 'user_provider ' , 'entry_point ' );
67
104
68
105
$ definition = $ container ->getDefinition ('abstract_listener.foo ' );
69
106
$ arguments = $ definition ->getArguments ();
70
- $ this ->assertEquals (new Reference ('security.authentication.success_handler.foo.abstract_factory ' ), $ arguments ['index_5 ' ]);
107
+ $ this ->assertEquals (new Reference ($ handlerId ), $ arguments ['index_5 ' ]);
108
+ $ successHandler = $ container ->findDefinition ((string ) $ arguments ['index_5 ' ]);
109
+ $ methodCalls = $ successHandler ->getMethodCalls ();
110
+
111
+ if ($ defaultHandlerInjection ) {
112
+ $ this ->assertEquals ('setOptions ' , $ methodCalls [0 ][0 ]);
113
+ $ this ->assertEquals (array ('default_target_path ' => '/bar ' ), $ methodCalls [0 ][1 ][0 ]);
114
+ $ this ->assertEquals ('setProviderKey ' , $ methodCalls [1 ][0 ]);
115
+ $ this ->assertEquals (array ('foo ' ), $ methodCalls [1 ][1 ]);
116
+ } else {
117
+ $ this ->assertCount (0 , $ methodCalls );
118
+ }
119
+ }
120
+
121
+ public function getSuccessHandlers ()
122
+ {
123
+ return array (
124
+ array ('security.authentication.success_handler.foo.abstract_factory ' , null , true ),
125
+ array ('custom_success_handler_default ' , 'custom_success_handler_default ' , true ),
126
+ array ('custom_success_handler ' , 'custom_success_handler ' , false ),
127
+ );
71
128
}
72
129
73
130
protected function callFactory ($ id , $ config , $ userProviderId , $ defaultEntryPointId )
@@ -92,11 +149,12 @@ protected function callFactory($id, $config, $userProviderId, $defaultEntryPoint
92
149
93
150
$ container = new ContainerBuilder ();
94
151
$ container ->register ('auth_provider ' );
152
+ $ container ->register ('custom_success_handler ' );
153
+ $ container ->register ('custom_failure_handler ' );
154
+ $ container ->setDefinition ('custom_success_handler_default ' , new DefinitionDecorator ('security.authentication.success_handler ' ));
155
+ $ container ->setDefinition ('custom_failure_handler_default ' , new DefinitionDecorator ('security.authentication.failure_handler ' ));
95
156
96
- list ($ authProviderId ,
97
- $ listenerId ,
98
- $ entryPointId
99
- ) = $ factory ->create ($ container , $ id , $ config , $ userProviderId , $ defaultEntryPointId );
157
+ list ($ authProviderId , $ listenerId , $ entryPointId ) = $ factory ->create ($ container , $ id , $ config , $ userProviderId , $ defaultEntryPointId );
100
158
101
159
return array ($ container , $ authProviderId , $ listenerId , $ entryPointId );
102
160
}
0 commit comments