Skip to content

Commit 6233cc3

Browse files
Merge branch '3.0'
* 3.0: (22 commits) Fix backport [travis] Upgrade phpunit wrapper & hirak/prestissimo [Bridge\PhpUnit] Workaround old phpunit bug, no colors in weak mode, add tests [PropertyAccess] Fix isPropertyWritable not using the reflection cache [PropertyAccess] Backport fixes from 2.7 [FrameworkBundle][2.8] Add tests for the Controller class [DependencyInjection] Update changelog Added WebProfiler toolbar ajax panel table layout css. [Validator] use correct term for a property in docblock (not "option") [Routing] small refactoring for scheme requirement [PropertyAccess] Remove most ref mismatches to improve perf [PropertyInfo] Support Doctrine custom mapping type in DoctrineExtractor [Validator] EmailValidator cannot extract hostname if email contains multiple @ symbols [NumberFormatter] Fix invalid numeric literal on PHP 7 [Process] fix docblock syntax use the clock mock for progress indicator tests Use XML_ELEMENT_NODE in nodeType check [PropertyAccess] Reduce overhead of UnexpectedTypeException tracking [PropertyAccess] Throw an UnexpectedTypeException when the type do not match [FrameworkBundle] Add tests for the Controller class ... Conflicts: src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php src/Symfony/Bundle/FrameworkBundle/composer.json src/Symfony/Component/PropertyAccess/PropertyAccessor.php src/Symfony/Component/PropertyAccess/PropertyAccessorInterface.php src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
2 parents f340f7e + 161aa39 commit 6233cc3

File tree

2 files changed

+282
-1
lines changed

2 files changed

+282
-1
lines changed

Tests/Controller/ControllerTest.php

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\RequestStack;
2020
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
2122
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2223
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2324
use Symfony\Component\Security\Core\User\User;
@@ -205,6 +206,142 @@ public function testJsonWithSerializerContextOverride()
205206
$response->setEncodingOptions(JSON_FORCE_OBJECT);
206207
$this->assertEquals('{}', $response->getContent());
207208
}
209+
210+
public function testIsGranted()
211+
{
212+
$authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
213+
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
214+
215+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
216+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
217+
$container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
218+
219+
$controller = new TestController();
220+
$controller->setContainer($container);
221+
222+
$this->assertTrue($controller->isGranted('foo'));
223+
}
224+
225+
/**
226+
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
227+
*/
228+
public function testdenyAccessUnlessGranted()
229+
{
230+
$authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
231+
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
232+
233+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
234+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
235+
$container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
236+
237+
$controller = new TestController();
238+
$controller->setContainer($container);
239+
240+
$controller->denyAccessUnlessGranted('foo');
241+
}
242+
243+
public function testRenderViewTwig()
244+
{
245+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
246+
$twig->expects($this->once())->method('render')->willReturn('bar');
247+
248+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
249+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
250+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
251+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
252+
253+
$controller = new TestController();
254+
$controller->setContainer($container);
255+
256+
$this->assertEquals('bar', $controller->renderView('foo'));
257+
}
258+
259+
public function testRenderTwig()
260+
{
261+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
262+
$twig->expects($this->once())->method('render')->willReturn('bar');
263+
264+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
265+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
266+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
267+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
268+
269+
$controller = new TestController();
270+
$controller->setContainer($container);
271+
272+
$this->assertEquals('bar', $controller->render('foo')->getContent());
273+
}
274+
275+
public function testStreamTwig()
276+
{
277+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
278+
279+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
280+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
281+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
282+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
283+
284+
$controller = new TestController();
285+
$controller->setContainer($container);
286+
287+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
288+
}
289+
290+
public function testRedirectToRoute()
291+
{
292+
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
293+
$router->expects($this->once())->method('generate')->willReturn('/foo');
294+
295+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
296+
$container->expects($this->at(0))->method('get')->will($this->returnValue($router));
297+
298+
$controller = new TestController();
299+
$controller->setContainer($container);
300+
$response = $controller->redirectToRoute('foo');
301+
302+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
303+
$this->assertSame('/foo', $response->getTargetUrl());
304+
$this->assertSame(302, $response->getStatusCode());
305+
}
306+
307+
public function testAddFlash()
308+
{
309+
$flashBag = new FlashBag();
310+
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
311+
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
312+
313+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
314+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
315+
$container->expects($this->at(1))->method('get')->will($this->returnValue($session));
316+
317+
$controller = new TestController();
318+
$controller->setContainer($container);
319+
$controller->addFlash('foo', 'bar');
320+
321+
$this->assertSame(array('bar'), $flashBag->get('foo'));
322+
}
323+
324+
public function testCreateAccessDeniedException()
325+
{
326+
$controller = new TestController();
327+
328+
$this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException());
329+
}
330+
331+
public function testIsCsrfTokenValid()
332+
{
333+
$tokenManager = $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface');
334+
$tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
335+
336+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
337+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
338+
$container->expects($this->at(1))->method('get')->will($this->returnValue($tokenManager));
339+
340+
$controller = new TestController();
341+
$controller->setContainer($container);
342+
343+
$this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
344+
}
208345
}
209346

210347
class TestController extends Controller
@@ -223,4 +360,147 @@ public function json($data, $status = 200, $headers = array(), $context = array(
223360
{
224361
return parent::json($data, $status, $headers, $context);
225362
}
363+
364+
public function isGranted($attributes, $object = null)
365+
{
366+
return parent::isGranted($attributes, $object);
367+
}
368+
369+
public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
370+
{
371+
parent::denyAccessUnlessGranted($attributes, $object, $message);
372+
}
373+
374+
public function redirectToRoute($route, array $parameters = array(), $status = 302)
375+
{
376+
return parent::redirectToRoute($route, $parameters, $status);
377+
}
378+
379+
public function addFlash($type, $message)
380+
{
381+
parent::addFlash($type, $message);
382+
}
383+
384+
public function isCsrfTokenValid($id, $token)
385+
{
386+
return parent::isCsrfTokenValid($id, $token);
387+
}
388+
389+
public function testGenerateUrl()
390+
{
391+
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
392+
$router->expects($this->once())->method('generate')->willReturn('/foo');
393+
394+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
395+
$container->expects($this->at(0))->method('get')->will($this->returnValue($router));
396+
397+
$controller = new Controller();
398+
$controller->setContainer($container);
399+
400+
$this->assertEquals('/foo', $controller->generateUrl('foo'));
401+
}
402+
403+
public function testRedirect()
404+
{
405+
$controller = new Controller();
406+
$response = $controller->redirect('http://dunglas.fr', 301);
407+
408+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
409+
$this->assertSame('http://dunglas.fr', $response->getTargetUrl());
410+
$this->assertSame(301, $response->getStatusCode());
411+
}
412+
413+
public function testRenderViewTemplating()
414+
{
415+
$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
416+
$templating->expects($this->once())->method('render')->willReturn('bar');
417+
418+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
419+
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
420+
421+
$controller = new Controller();
422+
$controller->setContainer($container);
423+
424+
$this->assertEquals('bar', $controller->renderView('foo'));
425+
}
426+
427+
public function testRenderTemplating()
428+
{
429+
$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
430+
$templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
431+
432+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
433+
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
434+
435+
$controller = new Controller();
436+
$controller->setContainer($container);
437+
438+
$this->assertEquals('bar', $controller->render('foo')->getContent());
439+
}
440+
441+
public function testStreamTemplating()
442+
{
443+
$templating = $this->getMock('Symfony\Component\Routing\RouterInterface');
444+
445+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
446+
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
447+
448+
$controller = new Controller();
449+
$controller->setContainer($container);
450+
451+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
452+
}
453+
454+
public function testCreateNotFoundException()
455+
{
456+
$controller = new Controller();
457+
458+
$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
459+
}
460+
461+
public function testCreateForm()
462+
{
463+
$form = $this->getMock('Symfony\Component\Form\FormInterface');
464+
465+
$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
466+
$formFactory->expects($this->once())->method('create')->willReturn($form);
467+
468+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
469+
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
470+
471+
$controller = new Controller();
472+
$controller->setContainer($container);
473+
474+
$this->assertEquals($form, $controller->createForm('foo'));
475+
}
476+
477+
public function testCreateFormBuilder()
478+
{
479+
$formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
480+
481+
$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
482+
$formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);
483+
484+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
485+
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
486+
487+
$controller = new Controller();
488+
$controller->setContainer($container);
489+
490+
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
491+
}
492+
493+
public function testGetDoctrine()
494+
{
495+
$doctrine = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
496+
497+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
498+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
499+
$container->expects($this->at(1))->method('get')->will($this->returnValue($doctrine));
500+
501+
$controller = new Controller();
502+
$controller->setContainer($container);
503+
504+
$this->assertEquals($doctrine, $controller->getDoctrine());
505+
}
226506
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"symfony/validator": "~2.8|~3.0",
5151
"symfony/yaml": "~2.8|~3.0",
5252
"symfony/property-info": "~2.8|~3.0",
53-
"phpdocumentor/reflection-docblock": "^3.0"
53+
"phpdocumentor/reflection-docblock": "^3.0",
54+
"twig/twig": "~1.23|~2.0"
5455
},
5556
"conflict": {
5657
"phpdocumentor/reflection-docblock": "<3.0"

0 commit comments

Comments
 (0)