@@ -130,10 +130,15 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
130
130
{
131
131
$ event = $ this ->createEvent ($ exception );
132
132
133
- $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
134
- $ accessDeniedHandler ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('error ' )));
133
+ $ listener = $ this ->createExceptionListener (
134
+ null ,
135
+ $ this ->createTrustResolver (true ),
136
+ null ,
137
+ null ,
138
+ null ,
139
+ $ this ->createCustomAccessDeniedHandler (new Response ('error ' ))
140
+ );
135
141
136
- $ listener = $ this ->createExceptionListener (null , $ this ->createTrustResolver (true ), null , null , null , $ accessDeniedHandler );
137
142
$ listener ->onKernelException ($ event );
138
143
139
144
$ this ->assertEquals ('error ' , $ event ->getResponse ()->getContent ());
@@ -147,16 +152,69 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
147
152
{
148
153
$ event = $ this ->createEvent ($ exception );
149
154
150
- $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
151
- $ tokenStorage ->expects ($ this ->once ())->method ('getToken ' )->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
152
-
153
- $ listener = $ this ->createExceptionListener ($ tokenStorage , $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint ());
155
+ $ listener = $ this ->createExceptionListener (
156
+ $ this ->createTokenStorage (),
157
+ $ this ->createTrustResolver (false ),
158
+ null ,
159
+ $ this ->createEntryPoint ()
160
+ );
154
161
$ listener ->onKernelException ($ event );
155
162
156
163
$ this ->assertEquals ('OK ' , $ event ->getResponse ()->getContent ());
157
164
$ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
158
165
}
159
166
167
+ /**
168
+ * @dataProvider getAccessDeniedExceptionProvider
169
+ */
170
+ public function testAccessDeniedExceptionNotFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage (\Exception $ exception , \Exception $ eventException = null )
171
+ {
172
+ $ event = $ this ->createEvent ($ exception );
173
+
174
+ $ listener = $ this ->createExceptionListener (
175
+ $ this ->createTokenStorage (),
176
+ $ this ->createTrustResolver (false ),
177
+ null ,
178
+ $ this ->createEntryPoint (),
179
+ null ,
180
+ $ this ->createCustomAccessDeniedHandler (new Response ('denied ' , 403 ))
181
+ );
182
+ $ listener ->onKernelException ($ event );
183
+
184
+ $ this ->assertEquals ('denied ' , $ event ->getResponse ()->getContent ());
185
+ $ this ->assertEquals (403 , $ event ->getResponse ()->getStatusCode ());
186
+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
187
+ }
188
+
189
+ /**
190
+ * @dataProvider getAccessDeniedExceptionProvider
191
+ */
192
+ public function testAccessDeniedExceptionNotFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage (\Exception $ exception , \Exception $ eventException = null )
193
+ {
194
+ $ kernel = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock ();
195
+ $ kernel ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('Unauthorized ' , 401 )));
196
+
197
+ $ event = $ this ->createEvent ($ exception , $ kernel );
198
+
199
+ $ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
200
+ $ httpUtils ->expects ($ this ->once ())->method ('createRequest ' )->will ($ this ->returnValue (Request::create ('/error ' )));
201
+
202
+ $ listener = $ this ->createExceptionListener (
203
+ $ this ->createTokenStorage (),
204
+ $ this ->createTrustResolver (true ),
205
+ $ httpUtils ,
206
+ null ,
207
+ '/error '
208
+ );
209
+ $ listener ->onKernelException ($ event );
210
+
211
+ $ this ->assertTrue ($ event ->isAllowingCustomResponseCode ());
212
+
213
+ $ this ->assertEquals ('Unauthorized ' , $ event ->getResponse ()->getContent ());
214
+ $ this ->assertEquals (401 , $ event ->getResponse ()->getStatusCode ());
215
+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
216
+ }
217
+
160
218
public function getAccessDeniedExceptionProvider ()
161
219
{
162
220
return [
@@ -168,6 +226,28 @@ public function getAccessDeniedExceptionProvider()
168
226
];
169
227
}
170
228
229
+ private function createTokenStorage ()
230
+ {
231
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
232
+ $ tokenStorage
233
+ ->expects ($ this ->once ())
234
+ ->method ('getToken ' )
235
+ ->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
236
+
237
+ return $ tokenStorage ;
238
+ }
239
+
240
+ private function createCustomAccessDeniedHandler (Response $ response )
241
+ {
242
+ $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
243
+ $ accessDeniedHandler
244
+ ->expects ($ this ->once ())
245
+ ->method ('handle ' )
246
+ ->will ($ this ->returnValue ($ response ));
247
+
248
+ return $ accessDeniedHandler ;
249
+ }
250
+
171
251
private function createEntryPoint (Response $ response = null )
172
252
{
173
253
$ entryPoint = $ this ->getMockBuilder ('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface ' )->getMock ();
@@ -193,8 +273,14 @@ private function createEvent(\Exception $exception, $kernel = null)
193
273
return new GetResponseForExceptionEvent ($ kernel , Request::create ('/ ' ), HttpKernelInterface::MASTER_REQUEST , $ exception );
194
274
}
195
275
196
- private function createExceptionListener (TokenStorageInterface $ tokenStorage = null , AuthenticationTrustResolverInterface $ trustResolver = null , HttpUtils $ httpUtils = null , AuthenticationEntryPointInterface $ authenticationEntryPoint = null , $ errorPage = null , AccessDeniedHandlerInterface $ accessDeniedHandler = null )
197
- {
276
+ private function createExceptionListener (
277
+ TokenStorageInterface $ tokenStorage = null ,
278
+ AuthenticationTrustResolverInterface $ trustResolver = null ,
279
+ HttpUtils $ httpUtils = null ,
280
+ AuthenticationEntryPointInterface $ authenticationEntryPoint = null ,
281
+ $ errorPage = null ,
282
+ AccessDeniedHandlerInterface $ accessDeniedHandler = null
283
+ ) {
198
284
return new ExceptionListener (
199
285
$ tokenStorage ?: $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock (),
200
286
$ trustResolver ?: $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface ' )->getMock (),
0 commit comments