@@ -130,10 +130,8 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
130130 {
131131 $ event = $ this ->createEvent ($ exception );
132132
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 (null , $ this ->createTrustResolver (true ), null , null , null , $ this ->createCustomAccessDeniedHandler (new Response ('error ' )));
135134
136- $ listener = $ this ->createExceptionListener (null , $ this ->createTrustResolver (true ), null , null , null , $ accessDeniedHandler );
137135 $ listener ->onKernelException ($ event );
138136
139137 $ this ->assertEquals ('error ' , $ event ->getResponse ()->getContent ());
@@ -147,16 +145,51 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
147145 {
148146 $ event = $ this ->createEvent ($ exception );
149147
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 ());
148+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint ());
154149 $ listener ->onKernelException ($ event );
155150
156151 $ this ->assertEquals ('OK ' , $ event ->getResponse ()->getContent ());
157152 $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
158153 }
159154
155+ /**
156+ * @dataProvider getAccessDeniedExceptionProvider
157+ */
158+ public function testAccessDeniedExceptionNotFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage (\Exception $ exception , \Exception $ eventException = null )
159+ {
160+ $ event = $ this ->createEvent ($ exception );
161+
162+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint (), null , $ this ->createCustomAccessDeniedHandler (new Response ('denied ' , 403 )));
163+ $ listener ->onKernelException ($ event );
164+
165+ $ this ->assertEquals ('denied ' , $ event ->getResponse ()->getContent ());
166+ $ this ->assertEquals (403 , $ event ->getResponse ()->getStatusCode ());
167+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
168+ }
169+
170+ /**
171+ * @dataProvider getAccessDeniedExceptionProvider
172+ */
173+ public function testAccessDeniedExceptionNotFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage (\Exception $ exception , \Exception $ eventException = null )
174+ {
175+ $ kernel = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock ();
176+ $ kernel ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('Unauthorized ' , 401 )));
177+
178+ $ event = $ this ->createEvent ($ exception , $ kernel );
179+
180+ $ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
181+ $ httpUtils ->expects ($ this ->once ())->method ('createRequest ' )->will ($ this ->returnValue (Request::create ('/error ' )));
182+
183+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (true ), $ httpUtils , null , '/error ' );
184+ $ listener ->onKernelException ($ event );
185+
186+ $ this ->assertTrue ($ event ->isAllowingCustomResponseCode ());
187+
188+ $ this ->assertEquals ('Unauthorized ' , $ event ->getResponse ()->getContent ());
189+ $ this ->assertEquals (401 , $ event ->getResponse ()->getStatusCode ());
190+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
191+ }
192+
160193 public function getAccessDeniedExceptionProvider ()
161194 {
162195 return [
@@ -168,6 +201,22 @@ public function getAccessDeniedExceptionProvider()
168201 ];
169202 }
170203
204+ private function createTokenStorage ()
205+ {
206+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
207+ $ tokenStorage ->expects ($ this ->once ())->method ('getToken ' )->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
208+
209+ return $ tokenStorage ;
210+ }
211+
212+ private function createCustomAccessDeniedHandler (Response $ response )
213+ {
214+ $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
215+ $ accessDeniedHandler ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue ($ response ));
216+
217+ return $ accessDeniedHandler ;
218+ }
219+
171220 private function createEntryPoint (Response $ response = null )
172221 {
173222 $ entryPoint = $ this ->getMockBuilder ('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface ' )->getMock ();
0 commit comments