minor #18206 [FrameworkBundle][2.8] Add tests for the Controller class (dunglas)

This PR was squashed before being merged into the 2.8 branch (closes #18206).

Discussion
----------

[FrameworkBundle][2.8] Add tests for the Controller class

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Backport tests of #18193 to the `abstract` Controller.

Commits
-------

5ee9f93 [FrameworkBundle][2.8] Add tests for the Controller class
This commit is contained in:
Fabien Potencier 2016-03-21 06:51:25 -07:00
commit dd7e05dd14
2 changed files with 92 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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",