Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ee9f93

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle][2.8] Add tests for the Controller class
1 parent 40c4f0d commit 5ee9f93

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,86 @@ private function getContainerWithTokenStorage($token = null)
124124

125125
return $container;
126126
}
127+
128+
public function testIsGranted()
129+
{
130+
$authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
131+
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
132+
133+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
134+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
135+
$container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
136+
137+
$controller = new TestController();
138+
$controller->setContainer($container);
139+
140+
$this->assertTrue($controller->isGranted('foo'));
141+
}
142+
143+
/**
144+
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
145+
*/
146+
public function testdenyAccessUnlessGranted()
147+
{
148+
$authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
149+
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
150+
151+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
152+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
153+
$container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
154+
155+
$controller = new TestController();
156+
$controller->setContainer($container);
157+
158+
$controller->denyAccessUnlessGranted('foo');
159+
}
160+
161+
public function testRenderViewTwig()
162+
{
163+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
164+
$twig->expects($this->once())->method('render')->willReturn('bar');
165+
166+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
167+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
168+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
169+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
170+
171+
$controller = new TestController();
172+
$controller->setContainer($container);
173+
174+
$this->assertEquals('bar', $controller->renderView('foo'));
175+
}
176+
177+
public function testRenderTwig()
178+
{
179+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
180+
$twig->expects($this->once())->method('render')->willReturn('bar');
181+
182+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
183+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
184+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
185+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
186+
187+
$controller = new TestController();
188+
$controller->setContainer($container);
189+
190+
$this->assertEquals('bar', $controller->render('foo')->getContent());
191+
}
192+
193+
public function testStreamTwig()
194+
{
195+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
196+
197+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
198+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
199+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
200+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
201+
202+
$controller = new TestController();
203+
$controller->setContainer($container);
204+
205+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
206+
}
127207
}
128208

129209
class TestController extends Controller
@@ -137,4 +217,14 @@ public function getUser()
137217
{
138218
return parent::getUser();
139219
}
220+
221+
public function isGranted($attributes, $object = null)
222+
{
223+
return parent::isGranted($attributes, $object);
224+
}
225+
226+
public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
227+
{
228+
parent::denyAccessUnlessGranted($attributes, $object, $message);
229+
}
140230
}

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"symfony/validator": "~2.5|~3.0.0",
5050
"symfony/yaml": "~2.0,>=2.0.5|~3.0.0",
5151
"symfony/property-info": "~2.8|~3.0.0",
52-
"phpdocumentor/reflection": "^1.0.7"
52+
"phpdocumentor/reflection": "^1.0.7",
53+
"twig/twig": "~1.23|~2.0"
5354
},
5455
"suggest": {
5556
"symfony/console": "For using the console commands",

0 commit comments

Comments
 (0)