1414use PHPUnit \Framework \TestCase ;
1515use Symfony \Component \HttpFoundation \Request ;
1616use Symfony \Component \HttpFoundation \Response ;
17+ use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
18+ use Symfony \Component \Security \Http \Authentication \DefaultAuthenticationFailureHandler ;
19+ use Symfony \Component \Security \Http \Authentication \DefaultAuthenticationSuccessHandler ;
1720use Symfony \Component \Security \Http \Firewall \UsernamePasswordFormAuthenticationListener ;
1821use Symfony \Component \Security \Core \SecurityContextInterface ;
22+ use Symfony \Component \Security \Http \HttpUtils ;
23+ use Symfony \Component \Security \Http \Session \SessionAuthenticationStrategy ;
1924
2025class UsernamePasswordFormAuthenticationListenerTest extends TestCase
2126{
@@ -24,7 +29,7 @@ class UsernamePasswordFormAuthenticationListenerTest extends TestCase
2429 */
2530 public function testHandleWhenUsernameLength ($ username , $ ok )
2631 {
27- $ request = Request::create ('/login_check ' , 'POST ' , array ('_username ' => $ username ));
32+ $ request = Request::create ('/login_check ' , 'POST ' , array ('_username ' => $ username, '' ));
2833 $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
2934
3035 $ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
@@ -69,6 +74,66 @@ public function testHandleWhenUsernameLength($username, $ok)
6974 $ listener ->handle ($ event );
7075 }
7176
77+ /**
78+ * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
79+ * @expectedExceptionMessage The key "_username" must be a string, "array" given.
80+ */
81+ public function testHandleNonStringUsername ()
82+ {
83+ $ request = Request::create ('/login_check ' , 'POST ' , array ('_username ' => array ()));
84+ $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
85+
86+ $ listener = new UsernamePasswordFormAuthenticationListener (
87+ new TokenStorage (),
88+ $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock (),
89+ new SessionAuthenticationStrategy (SessionAuthenticationStrategy::NONE ),
90+ $ httpUtils = new HttpUtils (),
91+ 'foo ' ,
92+ new DefaultAuthenticationSuccessHandler ($ httpUtils ),
93+ new DefaultAuthenticationFailureHandler ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ httpUtils ),
94+ array ('require_previous_session ' => false )
95+ );
96+
97+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
98+ $ event
99+ ->expects ($ this ->any ())
100+ ->method ('getRequest ' )
101+ ->will ($ this ->returnValue ($ request ))
102+ ;
103+
104+ $ listener ->handle ($ event );
105+ }
106+
107+ /**
108+ * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
109+ * @expectedExceptionMessage The key "_password" must be a string, "array" given.
110+ */
111+ public function testHandleNonStringPassword ()
112+ {
113+ $ request = Request::create ('/login_check ' , 'POST ' , array ('_username ' => 'foo ' , '_password ' => array ()));
114+ $ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
115+
116+ $ listener = new UsernamePasswordFormAuthenticationListener (
117+ new TokenStorage (),
118+ $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock (),
119+ new SessionAuthenticationStrategy (SessionAuthenticationStrategy::NONE ),
120+ $ httpUtils = new HttpUtils (),
121+ 'foo ' ,
122+ new DefaultAuthenticationSuccessHandler ($ httpUtils ),
123+ new DefaultAuthenticationFailureHandler ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ httpUtils ),
124+ array ('require_previous_session ' => false )
125+ );
126+
127+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
128+ $ event
129+ ->expects ($ this ->any ())
130+ ->method ('getRequest ' )
131+ ->will ($ this ->returnValue ($ request ))
132+ ;
133+
134+ $ listener ->handle ($ event );
135+ }
136+
72137 public function getUsernameForLength ()
73138 {
74139 return array (
0 commit comments