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

Skip to content

Commit 66f54ff

Browse files
committed
minor #18204 [FrameworkBundle][2.7] Add tests for the Controller class (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [FrameworkBundle][2.7] Add tests for the Controller class | Q | A | ------------- | --- | Branch? | 2.7 | 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 ------- 514a060 [FrameworkBundle] Add tests for the Controller class
2 parents cb2b1dc + 514a060 commit 66f54ff

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
2021
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2122
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2223
use Symfony\Component\Security\Core\User\User;
@@ -124,6 +125,62 @@ private function getContainerWithTokenStorage($token = null)
124125

125126
return $container;
126127
}
128+
129+
public function testRedirectToRoute()
130+
{
131+
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
132+
$router->expects($this->once())->method('generate')->willReturn('/foo');
133+
134+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
135+
$container->expects($this->at(0))->method('get')->will($this->returnValue($router));
136+
137+
$controller = new TestController();
138+
$controller->setContainer($container);
139+
$response = $controller->redirectToRoute('foo');
140+
141+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
142+
$this->assertSame('/foo', $response->getTargetUrl());
143+
$this->assertSame(302, $response->getStatusCode());
144+
}
145+
146+
public function testAddFlash()
147+
{
148+
$flashBag = new FlashBag();
149+
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
150+
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
151+
152+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
153+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
154+
$container->expects($this->at(1))->method('get')->will($this->returnValue($session));
155+
156+
$controller = new TestController();
157+
$controller->setContainer($container);
158+
$controller->addFlash('foo', 'bar');
159+
160+
$this->assertSame(array('bar'), $flashBag->get('foo'));
161+
}
162+
163+
public function testCreateAccessDeniedException()
164+
{
165+
$controller = new TestController();
166+
167+
$this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException());
168+
}
169+
170+
public function testIsCsrfTokenValid()
171+
{
172+
$tokenManager = $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface');
173+
$tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
174+
175+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
176+
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
177+
$container->expects($this->at(1))->method('get')->will($this->returnValue($tokenManager));
178+
179+
$controller = new TestController();
180+
$controller->setContainer($container);
181+
182+
$this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
183+
}
127184
}
128185

129186
class TestController extends Controller
@@ -137,4 +194,19 @@ public function getUser()
137194
{
138195
return parent::getUser();
139196
}
197+
198+
public function redirectToRoute($route, array $parameters = array(), $status = 302)
199+
{
200+
return parent::redirectToRoute($route, $parameters, $status);
201+
}
202+
203+
public function addFlash($type, $message)
204+
{
205+
parent::addFlash($type, $message);
206+
}
207+
208+
public function isCsrfTokenValid($id, $token)
209+
{
210+
return parent::isCsrfTokenValid($id, $token);
211+
}
140212
}

0 commit comments

Comments
 (0)