@@ -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,70 @@ 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
+ /**
191
+ * @dataProvider getAccessDeniedExceptionProvider
192
+ */
193
+ public function testAccessDeniedExceptionNotFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage (\Exception $ exception , \Exception $ eventException = null )
194
+ {
195
+ $ kernel = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock ();
196
+ $ kernel ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('Unauthorized ' , 401 )));
197
+
198
+ $ event = $ this ->createEvent ($ exception , $ kernel );
199
+
200
+ $ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
201
+ $ httpUtils ->expects ($ this ->once ())->method ('createRequest ' )->will ($ this ->returnValue (Request::create ('/error ' )));
202
+
203
+ $ listener = $ this ->createExceptionListener (
204
+ $ this ->createTokenStorage (),
205
+ $ this ->createTrustResolver (true ),
206
+ $ httpUtils ,
207
+ null ,
208
+ '/error '
209
+ );
210
+ $ listener ->onKernelException ($ event );
211
+
212
+ $ this ->assertTrue ($ event ->isAllowingCustomResponseCode ());
213
+
214
+ $ this ->assertEquals ('Unauthorized ' , $ event ->getResponse ()->getContent ());
215
+ $ this ->assertEquals (401 , $ event ->getResponse ()->getStatusCode ());
216
+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
217
+ }
218
+
160
219
public function getAccessDeniedExceptionProvider ()
161
220
{
162
221
return [
@@ -168,6 +227,28 @@ public function getAccessDeniedExceptionProvider()
168
227
];
169
228
}
170
229
230
+ private function createTokenStorage ()
231
+ {
232
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
233
+ $ tokenStorage
234
+ ->expects ($ this ->once ())
235
+ ->method ('getToken ' )
236
+ ->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
237
+
238
+ return $ tokenStorage ;
239
+ }
240
+
241
+ private function createCustomAccessDeniedHandler (Response $ response )
242
+ {
243
+ $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
244
+ $ accessDeniedHandler
245
+ ->expects ($ this ->once ())
246
+ ->method ('handle ' )
247
+ ->will ($ this ->returnValue ($ response ));
248
+
249
+ return $ accessDeniedHandler ;
250
+ }
251
+
171
252
private function createEntryPoint (Response $ response = null )
172
253
{
173
254
$ entryPoint = $ this ->getMockBuilder ('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface ' )->getMock ();
@@ -193,8 +274,14 @@ private function createEvent(\Exception $exception, $kernel = null)
193
274
return new GetResponseForExceptionEvent ($ kernel , Request::create ('/ ' ), HttpKernelInterface::MASTER_REQUEST , $ exception );
194
275
}
195
276
196
- private function createExceptionListener (TokenStorageInterface $ tokenStorage = null , AuthenticationTrustResolverInterface $ trustResolver = null , HttpUtils $ httpUtils = null , AuthenticationEntryPointInterface $ authenticationEntryPoint = null , $ errorPage = null , AccessDeniedHandlerInterface $ accessDeniedHandler = null )
197
- {
277
+ private function createExceptionListener (
278
+ TokenStorageInterface $ tokenStorage = null ,
279
+ AuthenticationTrustResolverInterface $ trustResolver = null ,
280
+ HttpUtils $ httpUtils = null ,
281
+ AuthenticationEntryPointInterface $ authenticationEntryPoint = null ,
282
+ $ errorPage = null ,
283
+ AccessDeniedHandlerInterface $ accessDeniedHandler = null
284
+ ) {
198
285
return new ExceptionListener (
199
286
$ tokenStorage ?: $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock (),
200
287
$ trustResolver ?: $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface ' )->getMock (),
0 commit comments