1717use Symfony \Component \HttpFoundation \Request ;
1818use Symfony \Component \HttpFoundation \RequestStack ;
1919use Symfony \Component \HttpFoundation \Response ;
20+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBag ;
2021use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
2122use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
2223use Symfony \Component \Security \Core \User \User ;
@@ -124,6 +125,142 @@ private function getContainerWithTokenStorage($token = null)
124125
125126 return $ container ;
126127 }
128+
129+ public function testIsGranted ()
130+ {
131+ $ authorizationChecker = $ this ->getMock ('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' );
132+ $ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (true );
133+
134+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
135+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
136+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ authorizationChecker ));
137+
138+ $ controller = new TestController ();
139+ $ controller ->setContainer ($ container );
140+
141+ $ this ->assertTrue ($ controller ->isGranted ('foo ' ));
142+ }
143+
144+ /**
145+ * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
146+ */
147+ public function testdenyAccessUnlessGranted ()
148+ {
149+ $ authorizationChecker = $ this ->getMock ('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' );
150+ $ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (false );
151+
152+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
153+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
154+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ authorizationChecker ));
155+
156+ $ controller = new TestController ();
157+ $ controller ->setContainer ($ container );
158+
159+ $ controller ->denyAccessUnlessGranted ('foo ' );
160+ }
161+
162+ public function testRenderViewTwig ()
163+ {
164+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
165+ $ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
166+
167+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
168+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
169+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
170+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
171+
172+ $ controller = new TestController ();
173+ $ controller ->setContainer ($ container );
174+
175+ $ this ->assertEquals ('bar ' , $ controller ->renderView ('foo ' ));
176+ }
177+
178+ public function testRenderTwig ()
179+ {
180+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
181+ $ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
182+
183+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
184+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
185+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
186+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
187+
188+ $ controller = new TestController ();
189+ $ controller ->setContainer ($ container );
190+
191+ $ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
192+ }
193+
194+ public function testStreamTwig ()
195+ {
196+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
197+
198+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
199+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
200+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
201+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
202+
203+ $ controller = new TestController ();
204+ $ controller ->setContainer ($ container );
205+
206+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
207+ }
208+
209+ public function testRedirectToRoute ()
210+ {
211+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
212+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
213+
214+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
215+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
216+
217+ $ controller = new TestController ();
218+ $ controller ->setContainer ($ container );
219+ $ response = $ controller ->redirectToRoute ('foo ' );
220+
221+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
222+ $ this ->assertSame ('/foo ' , $ response ->getTargetUrl ());
223+ $ this ->assertSame (302 , $ response ->getStatusCode ());
224+ }
225+
226+ public function testAddFlash ()
227+ {
228+ $ flashBag = new FlashBag ();
229+ $ session = $ this ->getMock ('Symfony\Component\HttpFoundation\Session\Session ' );
230+ $ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ flashBag );
231+
232+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
233+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
234+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ session ));
235+
236+ $ controller = new TestController ();
237+ $ controller ->setContainer ($ container );
238+ $ controller ->addFlash ('foo ' , 'bar ' );
239+
240+ $ this ->assertSame (array ('bar ' ), $ flashBag ->get ('foo ' ));
241+ }
242+
243+ public function testCreateAccessDeniedException ()
244+ {
245+ $ controller = new TestController ();
246+
247+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' , $ controller ->createAccessDeniedException ());
248+ }
249+
250+ public function testIsCsrfTokenValid ()
251+ {
252+ $ tokenManager = $ this ->getMock ('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface ' );
253+ $ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
254+
255+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
256+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
257+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ tokenManager ));
258+
259+ $ controller = new TestController ();
260+ $ controller ->setContainer ($ container );
261+
262+ $ this ->assertTrue ($ controller ->isCsrfTokenValid ('foo ' , 'bar ' ));
263+ }
127264}
128265
129266class TestController extends Controller
@@ -137,4 +274,147 @@ public function getUser()
137274 {
138275 return parent ::getUser ();
139276 }
277+
278+ public function isGranted ($ attributes , $ object = null )
279+ {
280+ return parent ::isGranted ($ attributes , $ object );
281+ }
282+
283+ public function denyAccessUnlessGranted ($ attributes , $ object = null , $ message = 'Access Denied. ' )
284+ {
285+ parent ::denyAccessUnlessGranted ($ attributes , $ object , $ message );
286+ }
287+
288+ public function redirectToRoute ($ route , array $ parameters = array (), $ status = 302 )
289+ {
290+ return parent ::redirectToRoute ($ route , $ parameters , $ status );
291+ }
292+
293+ public function addFlash ($ type , $ message )
294+ {
295+ parent ::addFlash ($ type , $ message );
296+ }
297+
298+ public function isCsrfTokenValid ($ id , $ token )
299+ {
300+ return parent ::isCsrfTokenValid ($ id , $ token );
301+ }
302+
303+ public function testGenerateUrl ()
304+ {
305+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
306+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
307+
308+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
309+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
310+
311+ $ controller = new Controller ();
312+ $ controller ->setContainer ($ container );
313+
314+ $ this ->assertEquals ('/foo ' , $ controller ->generateUrl ('foo ' ));
315+ }
316+
317+ public function testRedirect ()
318+ {
319+ $ controller = new Controller ();
320+ $ response = $ controller ->redirect ('http://dunglas.fr ' , 301 );
321+
322+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
323+ $ this ->assertSame ('http://dunglas.fr ' , $ response ->getTargetUrl ());
324+ $ this ->assertSame (301 , $ response ->getStatusCode ());
325+ }
326+
327+ public function testRenderViewTemplating ()
328+ {
329+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
330+ $ templating ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
331+
332+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
333+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
334+
335+ $ controller = new Controller ();
336+ $ controller ->setContainer ($ container );
337+
338+ $ this ->assertEquals ('bar ' , $ controller ->renderView ('foo ' ));
339+ }
340+
341+ public function testRenderTemplating ()
342+ {
343+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
344+ $ templating ->expects ($ this ->once ())->method ('renderResponse ' )->willReturn (new Response ('bar ' ));
345+
346+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
347+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
348+
349+ $ controller = new Controller ();
350+ $ controller ->setContainer ($ container );
351+
352+ $ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
353+ }
354+
355+ public function testStreamTemplating ()
356+ {
357+ $ templating = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
358+
359+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
360+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
361+
362+ $ controller = new Controller ();
363+ $ controller ->setContainer ($ container );
364+
365+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
366+ }
367+
368+ public function testCreateNotFoundException ()
369+ {
370+ $ controller = new Controller ();
371+
372+ $ this ->assertInstanceOf ('Symfony\Component\HttpKernel\Exception\NotFoundHttpException ' , $ controller ->createNotFoundException ());
373+ }
374+
375+ public function testCreateForm ()
376+ {
377+ $ form = $ this ->getMock ('Symfony\Component\Form\FormInterface ' );
378+
379+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
380+ $ formFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ form );
381+
382+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
383+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
384+
385+ $ controller = new Controller ();
386+ $ controller ->setContainer ($ container );
387+
388+ $ this ->assertEquals ($ form , $ controller ->createForm ('foo ' ));
389+ }
390+
391+ public function testCreateFormBuilder ()
392+ {
393+ $ formBuilder = $ this ->getMock ('Symfony\Component\Form\FormBuilderInterface ' );
394+
395+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
396+ $ formFactory ->expects ($ this ->once ())->method ('createBuilder ' )->willReturn ($ formBuilder );
397+
398+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
399+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
400+
401+ $ controller = new Controller ();
402+ $ controller ->setContainer ($ container );
403+
404+ $ this ->assertEquals ($ formBuilder , $ controller ->createFormBuilder ('foo ' ));
405+ }
406+
407+ public function testGetDoctrine ()
408+ {
409+ $ doctrine = $ this ->getMock ('Doctrine\Common\Persistence\ManagerRegistry ' );
410+
411+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
412+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
413+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ doctrine ));
414+
415+ $ controller = new Controller ();
416+ $ controller ->setContainer ($ container );
417+
418+ $ this ->assertEquals ($ doctrine , $ controller ->getDoctrine ());
419+ }
140420}
0 commit comments