1313
1414use Symfony \Component \DependencyInjection \Reference ;
1515use Symfony \Component \DependencyInjection \ContainerBuilder ;
16+ use Symfony \Component \DependencyInjection \DefinitionDecorator ;
1617
1718class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
1819{
1920 public function testCreate ()
2021 {
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 ' );
2629
2730 // auth provider
2831 $ this ->assertEquals ('auth_provider ' , $ authProviderId );
@@ -33,41 +36,95 @@ public function testCreate()
3336 $ definition = $ container ->getDefinition ('abstract_listener.foo ' );
3437 $ this ->assertEquals (array (
3538 '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 ' ),
3841 'index_7 ' => array (
39- 'use_forward ' => true ,
42+ 'use_forward ' => true ,
4043 ),
4144 ), $ definition ->getArguments ());
4245
4346 // entry point
4447 $ this ->assertEquals ('entry_point ' , $ entryPointId , '->create() does not change the default entry point. ' );
4548 }
4649
47- public function testDefaultFailureHandler ()
50+ /**
51+ * @dataProvider getFailureHandlers
52+ */
53+ public function testDefaultFailureHandler ($ handlerId , $ serviceId , $ defaultHandlerInjection )
4854 {
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 ' );
5465
5566 $ definition = $ container ->getDefinition ('abstract_listener.foo ' );
5667 $ 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+ );
5887 }
5988
60- public function testDefaultSuccessHandler ()
89+ /**
90+ * @dataProvider getSuccessHandlers
91+ */
92+ public function testDefaultSuccessHandler ($ handlerId , $ serviceId , $ defaultHandlerInjection )
6193 {
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 ' );
67104
68105 $ definition = $ container ->getDefinition ('abstract_listener.foo ' );
69106 $ 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+ );
71128 }
72129
73130 protected function callFactory ($ id , $ config , $ userProviderId , $ defaultEntryPointId )
@@ -92,11 +149,12 @@ protected function callFactory($id, $config, $userProviderId, $defaultEntryPoint
92149
93150 $ container = new ContainerBuilder ();
94151 $ 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 ' ));
95156
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 );
100158
101159 return array ($ container , $ authProviderId , $ listenerId , $ entryPointId );
102160 }
0 commit comments