@@ -130,10 +130,8 @@ 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 (null , $ this ->createTrustResolver (true ), null , null , null , $ this ->createCustomAccessDeniedHandler (new Response ('error ' )));
135
134
136
- $ listener = $ this ->createExceptionListener (null , $ this ->createTrustResolver (true ), null , null , null , $ accessDeniedHandler );
137
135
$ listener ->onKernelException ($ event );
138
136
139
137
$ this ->assertEquals ('error ' , $ event ->getResponse ()->getContent ());
@@ -147,16 +145,51 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
147
145
{
148
146
$ event = $ this ->createEvent ($ exception );
149
147
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 ());
154
149
$ listener ->onKernelException ($ event );
155
150
156
151
$ this ->assertEquals ('OK ' , $ event ->getResponse ()->getContent ());
157
152
$ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
158
153
}
159
154
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
+
160
193
public function getAccessDeniedExceptionProvider ()
161
194
{
162
195
return [
@@ -168,6 +201,22 @@ public function getAccessDeniedExceptionProvider()
168
201
];
169
202
}
170
203
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
+
171
220
private function createEntryPoint (Response $ response = null )
172
221
{
173
222
$ entryPoint = $ this ->getMockBuilder ('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface ' )->getMock ();
0 commit comments