ControllerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  18. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  19. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  20. use Symfony\Component\Security\Core\User\User;
  21. class ControllerTest extends TestCase
  22. {
  23. public function testForward()
  24. {
  25. $request = Request::create('/');
  26. $request->setLocale('fr');
  27. $request->setRequestFormat('xml');
  28. $requestStack = new RequestStack();
  29. $requestStack->push($request);
  30. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  31. $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
  32. return new Response($request->getRequestFormat().'--'.$request->getLocale());
  33. }));
  34. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  35. $container->expects($this->at(0))->method('get')->will($this->returnValue($requestStack));
  36. $container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));
  37. $controller = new TestController();
  38. $controller->setContainer($container);
  39. $response = $controller->forward('a_controller');
  40. $this->assertEquals('xml--fr', $response->getContent());
  41. }
  42. public function testGetUser()
  43. {
  44. $user = new User('user', 'pass');
  45. $token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER'));
  46. $controller = new TestController();
  47. $controller->setContainer($this->getContainerWithTokenStorage($token));
  48. $this->assertSame($controller->getUser(), $user);
  49. }
  50. public function testGetUserAnonymousUserConvertedToNull()
  51. {
  52. $token = new AnonymousToken('default', 'anon.');
  53. $controller = new TestController();
  54. $controller->setContainer($this->getContainerWithTokenStorage($token));
  55. $this->assertNull($controller->getUser());
  56. }
  57. public function testGetUserWithEmptyTokenStorage()
  58. {
  59. $controller = new TestController();
  60. $controller->setContainer($this->getContainerWithTokenStorage(null));
  61. $this->assertNull($controller->getUser());
  62. }
  63. /**
  64. * @expectedException \LogicException
  65. * @expectedExceptionMessage The SecurityBundle is not registered in your application.
  66. */
  67. public function testGetUserWithEmptyContainer()
  68. {
  69. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  70. $container
  71. ->expects($this->once())
  72. ->method('has')
  73. ->with('security.token_storage')
  74. ->will($this->returnValue(false));
  75. $controller = new TestController();
  76. $controller->setContainer($container);
  77. $controller->getUser();
  78. }
  79. /**
  80. * @param $token
  81. *
  82. * @return ContainerInterface
  83. */
  84. private function getContainerWithTokenStorage($token = null)
  85. {
  86. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage')->getMock();
  87. $tokenStorage
  88. ->expects($this->once())
  89. ->method('getToken')
  90. ->will($this->returnValue($token));
  91. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  92. $container
  93. ->expects($this->once())
  94. ->method('has')
  95. ->with('security.token_storage')
  96. ->will($this->returnValue(true));
  97. $container
  98. ->expects($this->once())
  99. ->method('get')
  100. ->with('security.token_storage')
  101. ->will($this->returnValue($tokenStorage));
  102. return $container;
  103. }
  104. public function testIsGranted()
  105. {
  106. $authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
  107. $authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
  108. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  109. $container->expects($this->at(0))->method('has')->will($this->returnValue(true));
  110. $container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
  111. $controller = new TestController();
  112. $controller->setContainer($container);
  113. $this->assertTrue($controller->isGranted('foo'));
  114. }
  115. /**
  116. * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  117. */
  118. public function testdenyAccessUnlessGranted()
  119. {
  120. $authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
  121. $authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
  122. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  123. $container->expects($this->at(0))->method('has')->will($this->returnValue(true));
  124. $container->expects($this->at(1))->method('get')->will($this->returnValue($authorizationChecker));
  125. $controller = new TestController();
  126. $controller->setContainer($container);
  127. $controller->denyAccessUnlessGranted('foo');
  128. }
  129. public function testRenderViewTwig()
  130. {
  131. $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
  132. $twig->expects($this->once())->method('render')->willReturn('bar');
  133. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  134. $container->expects($this->at(0))->method('has')->will($this->returnValue(false));
  135. $container->expects($this->at(1))->method('has')->will($this->returnValue(true));
  136. $container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
  137. $controller = new TestController();
  138. $controller->setContainer($container);
  139. $this->assertEquals('bar', $controller->renderView('foo'));
  140. }
  141. public function testRenderTwig()
  142. {
  143. $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
  144. $twig->expects($this->once())->method('render')->willReturn('bar');
  145. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  146. $container->expects($this->at(0))->method('has')->will($this->returnValue(false));
  147. $container->expects($this->at(1))->method('has')->will($this->returnValue(true));
  148. $container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
  149. $controller = new TestController();
  150. $controller->setContainer($container);
  151. $this->assertEquals('bar', $controller->render('foo')->getContent());
  152. }
  153. public function testStreamTwig()
  154. {
  155. $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
  156. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  157. $container->expects($this->at(0))->method('has')->will($this->returnValue(false));
  158. $container->expects($this->at(1))->method('has')->will($this->returnValue(true));
  159. $container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
  160. $controller = new TestController();
  161. $controller->setContainer($container);
  162. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
  163. }
  164. public function testRedirectToRoute()
  165. {
  166. $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
  167. $router->expects($this->once())->method('generate')->willReturn('/foo');
  168. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  169. $container->expects($this->at(0))->method('get')->will($this->returnValue($router));
  170. $controller = new TestController();
  171. $controller->setContainer($container);
  172. $response = $controller->redirectToRoute('foo');
  173. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  174. $this->assertSame('/foo', $response->getTargetUrl());
  175. $this->assertSame(302, $response->getStatusCode());
  176. }
  177. /**
  178. * @runInSeparateProcess
  179. */
  180. public function testAddFlash()
  181. {
  182. $flashBag = new FlashBag();
  183. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
  184. $session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
  185. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  186. $container->expects($this->at(0))->method('has')->will($this->returnValue(true));
  187. $container->expects($this->at(1))->method('get')->will($this->returnValue($session));
  188. $controller = new TestController();
  189. $controller->setContainer($container);
  190. $controller->addFlash('foo', 'bar');
  191. $this->assertSame(array('bar'), $flashBag->get('foo'));
  192. }
  193. public function testCreateAccessDeniedException()
  194. {
  195. $controller = new TestController();
  196. $this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException());
  197. }
  198. public function testIsCsrfTokenValid()
  199. {
  200. $tokenManager = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
  201. $tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
  202. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  203. $container->expects($this->at(0))->method('has')->will($this->returnValue(true));
  204. $container->expects($this->at(1))->method('get')->will($this->returnValue($tokenManager));
  205. $controller = new TestController();
  206. $controller->setContainer($container);
  207. $this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
  208. }
  209. public function testGenerateUrl()
  210. {
  211. $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
  212. $router->expects($this->once())->method('generate')->willReturn('/foo');
  213. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  214. $container->expects($this->at(0))->method('get')->will($this->returnValue($router));
  215. $controller = new Controller();
  216. $controller->setContainer($container);
  217. $this->assertEquals('/foo', $controller->generateUrl('foo'));
  218. }
  219. public function testRedirect()
  220. {
  221. $controller = new Controller();
  222. $response = $controller->redirect('http://dunglas.fr', 301);
  223. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  224. $this->assertSame('http://dunglas.fr', $response->getTargetUrl());
  225. $this->assertSame(301, $response->getStatusCode());
  226. }
  227. public function testRenderViewTemplating()
  228. {
  229. $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
  230. $templating->expects($this->once())->method('render')->willReturn('bar');
  231. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  232. $container->expects($this->at(0))->method('has')->willReturn(true);
  233. $container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
  234. $controller = new Controller();
  235. $controller->setContainer($container);
  236. $this->assertEquals('bar', $controller->renderView('foo'));
  237. }
  238. public function testRenderTemplating()
  239. {
  240. $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
  241. $templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
  242. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  243. $container->expects($this->at(0))->method('has')->willReturn(true);
  244. $container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
  245. $controller = new Controller();
  246. $controller->setContainer($container);
  247. $this->assertEquals('bar', $controller->render('foo')->getContent());
  248. }
  249. public function testStreamTemplating()
  250. {
  251. $templating = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
  252. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  253. $container->expects($this->at(0))->method('has')->willReturn(true);
  254. $container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
  255. $controller = new Controller();
  256. $controller->setContainer($container);
  257. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
  258. }
  259. public function testCreateNotFoundException()
  260. {
  261. $controller = new Controller();
  262. $this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
  263. }
  264. public function testCreateForm()
  265. {
  266. $form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
  267. $formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  268. $formFactory->expects($this->once())->method('create')->willReturn($form);
  269. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  270. $container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
  271. $controller = new Controller();
  272. $controller->setContainer($container);
  273. $this->assertEquals($form, $controller->createForm('foo'));
  274. }
  275. public function testCreateFormBuilder()
  276. {
  277. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilderInterface')->getMock();
  278. $formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  279. $formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);
  280. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  281. $container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
  282. $controller = new Controller();
  283. $controller->setContainer($container);
  284. $this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
  285. }
  286. public function testGetDoctrine()
  287. {
  288. $doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
  289. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  290. $container->expects($this->at(0))->method('has')->will($this->returnValue(true));
  291. $container->expects($this->at(1))->method('get')->will($this->returnValue($doctrine));
  292. $controller = new Controller();
  293. $controller->setContainer($container);
  294. $this->assertEquals($doctrine, $controller->getDoctrine());
  295. }
  296. }
  297. class TestController extends Controller
  298. {
  299. public function forward($controller, array $path = array(), array $query = array())
  300. {
  301. return parent::forward($controller, $path, $query);
  302. }
  303. public function getUser()
  304. {
  305. return parent::getUser();
  306. }
  307. public function isGranted($attributes, $object = null)
  308. {
  309. return parent::isGranted($attributes, $object);
  310. }
  311. public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
  312. {
  313. parent::denyAccessUnlessGranted($attributes, $object, $message);
  314. }
  315. public function redirectToRoute($route, array $parameters = array(), $status = 302)
  316. {
  317. return parent::redirectToRoute($route, $parameters, $status);
  318. }
  319. public function addFlash($type, $message)
  320. {
  321. parent::addFlash($type, $message);
  322. }
  323. public function isCsrfTokenValid($id, $token)
  324. {
  325. return parent::isCsrfTokenValid($id, $token);
  326. }
  327. }