@@ -131,10 +131,8 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
131
131
{
132
132
$ event = $ this ->createEvent ($ exception );
133
133
134
- $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
135
- $ accessDeniedHandler ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('error ' )));
134
+ $ listener = $ this ->createExceptionListener (null , $ this ->createTrustResolver (true ), null , null , null , $ this ->createCustomAccessDeniedHandler (new Response ('error ' )));
136
135
137
- $ listener = $ this ->createExceptionListener (null , $ this ->createTrustResolver (true ), null , null , null , $ accessDeniedHandler );
138
136
$ listener ->onKernelException ($ event );
139
137
140
138
$ this ->assertEquals ('error ' , $ event ->getResponse ()->getContent ());
@@ -148,16 +146,51 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
148
146
{
149
147
$ event = $ this ->createEvent ($ exception );
150
148
151
- $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
152
- $ tokenStorage ->expects ($ this ->once ())->method ('getToken ' )->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
153
-
154
- $ listener = $ this ->createExceptionListener ($ tokenStorage , $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint ());
149
+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint ());
155
150
$ listener ->onKernelException ($ event );
156
151
157
152
$ this ->assertEquals ('OK ' , $ event ->getResponse ()->getContent ());
158
153
$ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
159
154
}
160
155
156
+ /**
157
+ * @dataProvider getAccessDeniedExceptionProvider
158
+ */
159
+ public function testAccessDeniedExceptionNotFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage (\Exception $ exception , \Exception $ eventException = null )
160
+ {
161
+ $ event = $ this ->createEvent ($ exception );
162
+
163
+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (false ), null , $ this ->createEntryPoint (), null , $ this ->createCustomAccessDeniedHandler (new Response ('denied ' , 403 )));
164
+ $ listener ->onKernelException ($ event );
165
+
166
+ $ this ->assertEquals ('denied ' , $ event ->getResponse ()->getContent ());
167
+ $ this ->assertEquals (403 , $ event ->getResponse ()->getStatusCode ());
168
+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
169
+ }
170
+
171
+ /**
172
+ * @dataProvider getAccessDeniedExceptionProvider
173
+ */
174
+ public function testAccessDeniedExceptionNotFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage (\Exception $ exception , \Exception $ eventException = null )
175
+ {
176
+ $ kernel = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock ();
177
+ $ kernel ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue (new Response ('Unauthorized ' , 401 )));
178
+
179
+ $ event = $ this ->createEvent ($ exception , $ kernel );
180
+
181
+ $ httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
182
+ $ httpUtils ->expects ($ this ->once ())->method ('createRequest ' )->will ($ this ->returnValue (Request::create ('/error ' )));
183
+
184
+ $ listener = $ this ->createExceptionListener ($ this ->createTokenStorage (), $ this ->createTrustResolver (true ), $ httpUtils , null , '/error ' );
185
+ $ listener ->onKernelException ($ event );
186
+
187
+ $ this ->assertTrue ($ event ->isAllowingCustomResponseCode ());
188
+
189
+ $ this ->assertEquals ('Unauthorized ' , $ event ->getResponse ()->getContent ());
190
+ $ this ->assertEquals (401 , $ event ->getResponse ()->getStatusCode ());
191
+ $ this ->assertSame (null === $ eventException ? $ exception : $ eventException , $ event ->getException ()->getPrevious ());
192
+ }
193
+
161
194
public function getAccessDeniedExceptionProvider ()
162
195
{
163
196
return [
@@ -169,6 +202,22 @@ public function getAccessDeniedExceptionProvider()
169
202
];
170
203
}
171
204
205
+ private function createTokenStorage ()
206
+ {
207
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
208
+ $ tokenStorage ->expects ($ this ->once ())->method ('getToken ' )->will ($ this ->returnValue ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ()));
209
+
210
+ return $ tokenStorage ;
211
+ }
212
+
213
+ private function createCustomAccessDeniedHandler (Response $ response )
214
+ {
215
+ $ accessDeniedHandler = $ this ->getMockBuilder ('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface ' )->getMock ();
216
+ $ accessDeniedHandler ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnValue ($ response ));
217
+
218
+ return $ accessDeniedHandler ;
219
+ }
220
+
172
221
private function createEntryPoint (Response $ response = null )
173
222
{
174
223
$ entryPoint = $ this ->getMockBuilder ('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface ' )->getMock ();
0 commit comments