Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 828efa7

Browse filesBrowse files
committed
[FrameworkBundle] Add tests for the Controller class
1 parent 40c4f0d commit 828efa7
Copy full SHA for 828efa7

File tree

2 files changed

+92
-1
lines changed
Filter options

2 files changed

+92
-1
lines changed

‎composer.json

Copy file name to clipboardExpand all lines: composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"monolog/monolog": "~1.11",
8787
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
8888
"egulias/email-validator": "~1.2",
89-
"phpdocumentor/reflection": "^1.0.7"
89+
"phpdocumentor/reflection": "^1.0.7",
90+
"twig/twig": "~1.23|~2.0"
9091
},
9192
"conflict": {
9293
"phpdocumentor/reflection": "<1.0.7"

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php
+90Lines 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
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.