@@ -81,7 +81,7 @@ public function testHandleWhenUsernameLength($username, $ok)
8181 * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
8282 * @expectedExceptionMessage The key "_username" must be a string, "array" given.
8383 */
84- public function testHandleNonStringUsername ($ postOnly )
84+ public function testHandleNonStringUsernameWithArray ($ postOnly )
8585 {
8686 $ request = Request::create ('/login_check ' , 'POST ' , ['_username ' => []]);
8787 $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
@@ -99,6 +99,79 @@ public function testHandleNonStringUsername($postOnly)
9999 $ listener ->handle ($ event );
100100 }
101101
102+ /**
103+ * @dataProvider postOnlyDataProvider
104+ * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
105+ * @expectedExceptionMessage The key "_username" must be a string, "integer" given.
106+ */
107+ public function testHandleNonStringUsernameWithInt ($ postOnly )
108+ {
109+ $ request = Request::create ('/login_check ' , 'POST ' , ['_username ' => 42 ]);
110+ $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
111+ $ listener = new UsernamePasswordFormAuthenticationListener (
112+ new TokenStorage (),
113+ $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock (),
114+ new SessionAuthenticationStrategy (SessionAuthenticationStrategy::NONE ),
115+ $ httpUtils = new HttpUtils (),
116+ 'foo ' ,
117+ new DefaultAuthenticationSuccessHandler ($ httpUtils ),
118+ new DefaultAuthenticationFailureHandler ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ httpUtils ),
119+ ['require_previous_session ' => false , 'post_only ' => $ postOnly ]
120+ );
121+ $ event = new GetResponseEvent ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ request , HttpKernelInterface::MASTER_REQUEST );
122+ $ listener ->handle ($ event );
123+ }
124+
125+ /**
126+ * @dataProvider postOnlyDataProvider
127+ * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
128+ * @expectedExceptionMessage The key "_username" must be a string, "object" given.
129+ */
130+ public function testHandleNonStringUsernameWithObject ($ postOnly )
131+ {
132+ $ request = Request::create ('/login_check ' , 'POST ' , ['_username ' => new \stdClass ()]);
133+ $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
134+ $ listener = new UsernamePasswordFormAuthenticationListener (
135+ new TokenStorage (),
136+ $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock (),
137+ new SessionAuthenticationStrategy (SessionAuthenticationStrategy::NONE ),
138+ $ httpUtils = new HttpUtils (),
139+ 'foo ' ,
140+ new DefaultAuthenticationSuccessHandler ($ httpUtils ),
141+ new DefaultAuthenticationFailureHandler ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ httpUtils ),
142+ ['require_previous_session ' => false , 'post_only ' => $ postOnly ]
143+ );
144+ $ event = new GetResponseEvent ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ request , HttpKernelInterface::MASTER_REQUEST );
145+ $ listener ->handle ($ event );
146+ }
147+
148+ /**
149+ * @dataProvider postOnlyDataProvider
150+ */
151+ public function testHandleNonStringUsernameWith__toString ($ postOnly )
152+ {
153+ $ usernameClass = $ this ->getMockBuilder (DummyUserClass::class)->getMock ();
154+ $ usernameClass
155+ ->expects ($ this ->atLeastOnce ())
156+ ->method ('__toString ' )
157+ ->will ($ this ->returnValue ('someUsername ' ));
158+
159+ $ request = Request::create ('/login_check ' , 'POST ' , ['_username ' => $ usernameClass ]);
160+ $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
161+ $ listener = new UsernamePasswordFormAuthenticationListener (
162+ new TokenStorage (),
163+ $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock (),
164+ new SessionAuthenticationStrategy (SessionAuthenticationStrategy::NONE ),
165+ $ httpUtils = new HttpUtils (),
166+ 'foo ' ,
167+ new DefaultAuthenticationSuccessHandler ($ httpUtils ),
168+ new DefaultAuthenticationFailureHandler ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ httpUtils ),
169+ ['require_previous_session ' => false , 'post_only ' => $ postOnly ]
170+ );
171+ $ event = new GetResponseEvent ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ request , HttpKernelInterface::MASTER_REQUEST );
172+ $ listener ->handle ($ event );
173+ }
174+
102175 public function postOnlyDataProvider ()
103176 {
104177 return [
@@ -115,3 +188,11 @@ public function getUsernameForLength()
115188 ];
116189 }
117190}
191+
192+ class DummyUserClass
193+ {
194+ public function __toString ()
195+ {
196+ return '' ;
197+ }
198+ }
0 commit comments