ContainerControllerResolverTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Component\HttpKernel\Tests\Controller;
  11. use Psr\Container\ContainerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\DependencyInjection\Container;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
  16. class ContainerControllerResolverTest extends ControllerResolverTest
  17. {
  18. public function testGetControllerServiceWithSingleColon()
  19. {
  20. $service = new ControllerTestService('foo');
  21. $container = $this->createMockContainer();
  22. $container->expects($this->once())
  23. ->method('has')
  24. ->with('foo')
  25. ->will($this->returnValue(true));
  26. $container->expects($this->once())
  27. ->method('get')
  28. ->with('foo')
  29. ->will($this->returnValue($service))
  30. ;
  31. $resolver = $this->createControllerResolver(null, $container);
  32. $request = Request::create('/');
  33. $request->attributes->set('_controller', 'foo:action');
  34. $controller = $resolver->getController($request);
  35. $this->assertSame($service, $controller[0]);
  36. $this->assertSame('action', $controller[1]);
  37. }
  38. public function testGetControllerService()
  39. {
  40. $service = new ControllerTestService('foo');
  41. $container = $this->createMockContainer();
  42. $container->expects($this->once())
  43. ->method('has')
  44. ->with('foo')
  45. ->will($this->returnValue(true));
  46. $container->expects($this->once())
  47. ->method('get')
  48. ->with('foo')
  49. ->will($this->returnValue($service))
  50. ;
  51. $resolver = $this->createControllerResolver(null, $container);
  52. $request = Request::create('/');
  53. $request->attributes->set('_controller', 'foo::action');
  54. $controller = $resolver->getController($request);
  55. $this->assertSame($service, $controller[0]);
  56. $this->assertSame('action', $controller[1]);
  57. }
  58. public function testGetControllerInvokableService()
  59. {
  60. $service = new InvokableControllerService('bar');
  61. $container = $this->createMockContainer();
  62. $container->expects($this->once())
  63. ->method('has')
  64. ->with('foo')
  65. ->will($this->returnValue(true))
  66. ;
  67. $container->expects($this->once())
  68. ->method('get')
  69. ->with('foo')
  70. ->will($this->returnValue($service))
  71. ;
  72. $resolver = $this->createControllerResolver(null, $container);
  73. $request = Request::create('/');
  74. $request->attributes->set('_controller', 'foo');
  75. $controller = $resolver->getController($request);
  76. $this->assertSame($service, $controller);
  77. }
  78. public function testGetControllerInvokableServiceWithClassNameAsName()
  79. {
  80. $service = new InvokableControllerService('bar');
  81. $container = $this->createMockContainer();
  82. $container->expects($this->once())
  83. ->method('has')
  84. ->with(InvokableControllerService::class)
  85. ->will($this->returnValue(true))
  86. ;
  87. $container->expects($this->once())
  88. ->method('get')
  89. ->with(InvokableControllerService::class)
  90. ->will($this->returnValue($service))
  91. ;
  92. $resolver = $this->createControllerResolver(null, $container);
  93. $request = Request::create('/');
  94. $request->attributes->set('_controller', InvokableControllerService::class);
  95. $controller = $resolver->getController($request);
  96. $this->assertSame($service, $controller);
  97. }
  98. /**
  99. * Tests where the fallback instantiation fails due to required constructor arguments.
  100. *
  101. * @expectedException \InvalidArgumentException
  102. * @expectedExceptionMessage Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
  103. */
  104. public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
  105. {
  106. $container = $this->getMockBuilder(Container::class)->getMock();
  107. $container->expects($this->once())
  108. ->method('has')
  109. ->with(ControllerTestService::class)
  110. ->will($this->returnValue(false))
  111. ;
  112. $container->expects($this->atLeastOnce())
  113. ->method('getRemovedIds')
  114. ->with()
  115. ->will($this->returnValue([ControllerTestService::class => true]))
  116. ;
  117. $resolver = $this->createControllerResolver(null, $container);
  118. $request = Request::create('/');
  119. $request->attributes->set('_controller', [ControllerTestService::class, 'action']);
  120. $resolver->getController($request);
  121. }
  122. /**
  123. * Tests where the fallback instantiation fails due to non-existing class.
  124. *
  125. * @expectedException \InvalidArgumentException
  126. * @expectedExceptionMessage Controller "app.my_controller" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
  127. */
  128. public function testExceptionWhenUsingRemovedControllerService()
  129. {
  130. $container = $this->getMockBuilder(Container::class)->getMock();
  131. $container->expects($this->once())
  132. ->method('has')
  133. ->with('app.my_controller')
  134. ->will($this->returnValue(false))
  135. ;
  136. $container->expects($this->atLeastOnce())
  137. ->method('getRemovedIds')
  138. ->with()
  139. ->will($this->returnValue(['app.my_controller' => true]))
  140. ;
  141. $resolver = $this->createControllerResolver(null, $container);
  142. $request = Request::create('/');
  143. $request->attributes->set('_controller', 'app.my_controller');
  144. $resolver->getController($request);
  145. }
  146. public function getUndefinedControllers()
  147. {
  148. $tests = parent::getUndefinedControllers();
  149. $tests[0] = ['foo', \InvalidArgumentException::class, 'Controller "foo" does neither exist as service nor as class'];
  150. $tests[1] = ['oof::bar', \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
  151. $tests[2] = [['oof', 'bar'], \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
  152. $tests[] = [
  153. [ControllerTestService::class, 'action'],
  154. \InvalidArgumentException::class,
  155. 'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
  156. ];
  157. $tests[] = [
  158. ControllerTestService::class.'::action',
  159. \InvalidArgumentException::class, 'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
  160. ];
  161. $tests[] = [
  162. InvokableControllerService::class,
  163. \InvalidArgumentException::class,
  164. 'Controller "Symfony\Component\HttpKernel\Tests\Controller\InvokableControllerService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
  165. ];
  166. return $tests;
  167. }
  168. protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
  169. {
  170. if (!$container) {
  171. $container = $this->createMockContainer();
  172. }
  173. return new ContainerControllerResolver($container, $logger);
  174. }
  175. protected function createMockContainer()
  176. {
  177. return $this->getMockBuilder(ContainerInterface::class)->getMock();
  178. }
  179. }
  180. class InvokableControllerService
  181. {
  182. public function __construct($bar) // mandatory argument to prevent automatic instantiation
  183. {
  184. }
  185. public function __invoke()
  186. {
  187. }
  188. }
  189. class ControllerTestService
  190. {
  191. public function __construct($foo)
  192. {
  193. }
  194. public function action()
  195. {
  196. }
  197. }