Skip to content

Commit dd7e05d

Browse files
committed
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
2 parents a6941f6 + 5ee9f93 commit dd7e05d

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy