From 5ee9f933bbf16da702654198d95c4d892ad427d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 16 Mar 2016 23:41:38 +0100 Subject: [PATCH] [FrameworkBundle][2.8] Add tests for the Controller class --- .../Tests/Controller/ControllerTest.php | 90 +++++++++++++++++++ .../Bundle/FrameworkBundle/composer.json | 3 +- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php index 2bb59d9c6f..53388ec096 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php @@ -124,6 +124,86 @@ class ControllerTest extends TestCase return $container; } + + public function testIsGranted() + { + $authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'); + $authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker)); + + $controller = new TestController(); + $controller->setContainer($container); + + $this->assertTrue($controller->isGranted('foo')); + } + + /** + * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException + */ + public function testdenyAccessUnlessGranted() + { + $authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'); + $authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker)); + + $controller = new TestController(); + $controller->setContainer($container); + + $controller->denyAccessUnlessGranted('foo'); + } + + public function testRenderViewTwig() + { + $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock(); + $twig->expects($this->once())->method('render')->willReturn('bar'); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(false)); + $container->expects($this->at(1))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(2))->method('get')->will($this->returnValue($twig)); + + $controller = new TestController(); + $controller->setContainer($container); + + $this->assertEquals('bar', $controller->renderView('foo')); + } + + public function testRenderTwig() + { + $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock(); + $twig->expects($this->once())->method('render')->willReturn('bar'); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(false)); + $container->expects($this->at(1))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(2))->method('get')->will($this->returnValue($twig)); + + $controller = new TestController(); + $controller->setContainer($container); + + $this->assertEquals('bar', $controller->render('foo')->getContent()); + } + + public function testStreamTwig() + { + $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock(); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(false)); + $container->expects($this->at(1))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(2))->method('get')->will($this->returnValue($twig)); + + $controller = new TestController(); + $controller->setContainer($container); + + $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo')); + } } class TestController extends Controller @@ -137,4 +217,14 @@ class TestController extends Controller { return parent::getUser(); } + + public function isGranted($attributes, $object = null) + { + return parent::isGranted($attributes, $object); + } + + public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') + { + parent::denyAccessUnlessGranted($attributes, $object, $message); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index e6416bd031..60d4cdb370 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -49,7 +49,8 @@ "symfony/validator": "~2.5|~3.0.0", "symfony/yaml": "~2.0,>=2.0.5|~3.0.0", "symfony/property-info": "~2.8|~3.0.0", - "phpdocumentor/reflection": "^1.0.7" + "phpdocumentor/reflection": "^1.0.7", + "twig/twig": "~1.23|~2.0" }, "suggest": { "symfony/console": "For using the console commands",