Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,86 @@ private function getContainerWithTokenStorage($token = null)

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
Expand All @@ -137,4 +217,14 @@ public function getUser()
{
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);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down