ControllerResolverTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Psr\Log\LoggerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
  13. use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
  14. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest as BaseControllerResolverTest;
  18. class ControllerResolverTest extends BaseControllerResolverTest
  19. {
  20. public function testGetControllerOnContainerAware()
  21. {
  22. $resolver = $this->createControllerResolver();
  23. $request = Request::create('/');
  24. $request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController::testAction');
  25. $controller = $resolver->getController($request);
  26. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller[0]->getContainer());
  27. $this->assertSame('testAction', $controller[1]);
  28. }
  29. public function testGetControllerOnContainerAwareInvokable()
  30. {
  31. $resolver = $this->createControllerResolver();
  32. $request = Request::create('/');
  33. $request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController');
  34. $controller = $resolver->getController($request);
  35. $this->assertInstanceOf('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController', $controller);
  36. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller->getContainer());
  37. }
  38. public function testGetControllerWithBundleNotation()
  39. {
  40. $shortName = 'FooBundle:Default:test';
  41. $parser = $this->createMockParser();
  42. $parser->expects($this->once())
  43. ->method('parse')
  44. ->with($shortName)
  45. ->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController::testAction'))
  46. ;
  47. $resolver = $this->createControllerResolver(null, $parser);
  48. $request = Request::create('/');
  49. $request->attributes->set('_controller', $shortName);
  50. $controller = $resolver->getController($request);
  51. $this->assertInstanceOf('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController', $controller[0]);
  52. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller[0]->getContainer());
  53. $this->assertSame('testAction', $controller[1]);
  54. }
  55. public function testGetControllerService()
  56. {
  57. $container = $this->createMockContainer();
  58. $container->expects($this->once())
  59. ->method('get')
  60. ->with('foo')
  61. ->will($this->returnValue($this))
  62. ;
  63. $resolver = $this->createControllerResolver(null, null, $container);
  64. $request = Request::create('/');
  65. $request->attributes->set('_controller', 'foo:controllerMethod1');
  66. $controller = $resolver->getController($request);
  67. $this->assertInstanceOf(\get_class($this), $controller[0]);
  68. $this->assertSame('controllerMethod1', $controller[1]);
  69. }
  70. public function testGetControllerInvokableService()
  71. {
  72. $invokableController = new InvokableController('bar');
  73. $container = $this->createMockContainer();
  74. $container->expects($this->once())
  75. ->method('has')
  76. ->with('foo')
  77. ->will($this->returnValue(true))
  78. ;
  79. $container->expects($this->once())
  80. ->method('get')
  81. ->with('foo')
  82. ->will($this->returnValue($invokableController))
  83. ;
  84. $resolver = $this->createControllerResolver(null, null, $container);
  85. $request = Request::create('/');
  86. $request->attributes->set('_controller', 'foo');
  87. $controller = $resolver->getController($request);
  88. $this->assertEquals($invokableController, $controller);
  89. }
  90. public function testGetControllerInvokableServiceWithClassNameAsName()
  91. {
  92. $invokableController = new InvokableController('bar');
  93. $className = __NAMESPACE__.'\InvokableController';
  94. $container = $this->createMockContainer();
  95. $container->expects($this->once())
  96. ->method('has')
  97. ->with($className)
  98. ->will($this->returnValue(true))
  99. ;
  100. $container->expects($this->once())
  101. ->method('get')
  102. ->with($className)
  103. ->will($this->returnValue($invokableController))
  104. ;
  105. $resolver = $this->createControllerResolver(null, null, $container);
  106. $request = Request::create('/');
  107. $request->attributes->set('_controller', $className);
  108. $controller = $resolver->getController($request);
  109. $this->assertEquals($invokableController, $controller);
  110. }
  111. /**
  112. * @dataProvider getUndefinedControllers
  113. */
  114. public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
  115. {
  116. if (method_exists($this, 'expectException')) {
  117. $this->expectException($exceptionName);
  118. $this->expectExceptionMessage($exceptionMessage);
  119. } else {
  120. $this->setExpectedException($exceptionName, $exceptionMessage);
  121. }
  122. parent::testGetControllerOnNonUndefinedFunction($controller);
  123. }
  124. public function getUndefinedControllers()
  125. {
  126. return array(
  127. array('foo', '\LogicException', 'Unable to parse the controller name "foo".'),
  128. array('oof::bar', '\InvalidArgumentException', 'Class "oof" does not exist.'),
  129. array('stdClass', '\LogicException', 'Unable to parse the controller name "stdClass".'),
  130. array(
  131. 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
  132. '\InvalidArgumentException',
  133. 'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar" for URI "/" is not callable.',
  134. ),
  135. );
  136. }
  137. protected function createControllerResolver(LoggerInterface $logger = null, ControllerNameParser $parser = null, ContainerInterface $container = null)
  138. {
  139. if (!$parser) {
  140. $parser = $this->createMockParser();
  141. }
  142. if (!$container) {
  143. $container = $this->createMockContainer();
  144. }
  145. return new ControllerResolver($container, $parser, $logger);
  146. }
  147. protected function createMockParser()
  148. {
  149. return $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser')->disableOriginalConstructor()->getMock();
  150. }
  151. protected function createMockContainer()
  152. {
  153. return $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  154. }
  155. }
  156. class ContainerAwareController implements ContainerAwareInterface
  157. {
  158. private $container;
  159. public function setContainer(ContainerInterface $container = null)
  160. {
  161. $this->container = $container;
  162. }
  163. public function getContainer()
  164. {
  165. return $this->container;
  166. }
  167. public function testAction()
  168. {
  169. }
  170. public function __invoke()
  171. {
  172. }
  173. }
  174. class InvokableController
  175. {
  176. public function __construct($bar) // mandatory argument to prevent automatic instantiation
  177. {
  178. }
  179. public function __invoke()
  180. {
  181. }
  182. }