14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \HttpFoundation \Request ;
16
16
use 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 ;
17
20
use Symfony \Component \Security \Http \Firewall \UsernamePasswordFormAuthenticationListener ;
18
21
use Symfony \Component \Security \Core \SecurityContextInterface ;
22
+ use Symfony \Component \Security \Http \HttpUtils ;
23
+ use Symfony \Component \Security \Http \Session \SessionAuthenticationStrategy ;
19
24
20
25
class UsernamePasswordFormAuthenticationListenerTest extends TestCase
21
26
{
@@ -24,7 +29,7 @@ class UsernamePasswordFormAuthenticationListenerTest extends TestCase
24
29
*/
25
30
public function testHandleWhenUsernameLength ($ username , $ ok )
26
31
{
27
- $ request = Request::c
F438
reate ('/login_check ' , 'POST ' , array ('_username ' => $ username ));
32
+ $ request = Request::create ('/login_check ' , 'POST ' , array ('_username ' => $ username, '' ));
28
33
$ request ->setSession ($ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )->getMock ());
29
34
30
35
$ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
@@ -69,6 +74,66 @@ public function testHandleWhenUsernameLength($username, $ok)
69
74
$ listener ->handle ($ event );
70
75
}
71
76
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
+
72
137
public function getUsernameForLength ()
73
138
{
74
139
return array (
0 commit comments