ContainerControllerResolverTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
  15. class ContainerControllerResolverTest extends ControllerResolverTest
  16. {
  17. public function testGetControllerService()
  18. {
  19. $container = $this->createMockContainer();
  20. $container->expects($this->once())
  21. ->method('get')
  22. ->with('foo')
  23. ->will($this->returnValue($this))
  24. ;
  25. $resolver = $this->createControllerResolver(null, $container);
  26. $request = Request::create('/');
  27. $request->attributes->set('_controller', 'foo:controllerMethod1');
  28. $controller = $resolver->getController($request);
  29. $this->assertInstanceOf(get_class($this), $controller[0]);
  30. $this->assertSame('controllerMethod1', $controller[1]);
  31. }
  32. public function testGetControllerInvokableService()
  33. {
  34. $invokableController = new InvokableController('bar');
  35. $container = $this->createMockContainer();
  36. $container->expects($this->once())
  37. ->method('has')
  38. ->with('foo')
  39. ->will($this->returnValue(true))
  40. ;
  41. $container->expects($this->once())
  42. ->method('get')
  43. ->with('foo')
  44. ->will($this->returnValue($invokableController))
  45. ;
  46. $resolver = $this->createControllerResolver(null, $container);
  47. $request = Request::create('/');
  48. $request->attributes->set('_controller', 'foo');
  49. $controller = $resolver->getController($request);
  50. $this->assertEquals($invokableController, $controller);
  51. }
  52. public function testGetControllerInvokableServiceWithClassNameAsName()
  53. {
  54. $invokableController = new InvokableController('bar');
  55. $className = __NAMESPACE__.'\InvokableController';
  56. $container = $this->createMockContainer();
  57. $container->expects($this->once())
  58. ->method('has')
  59. ->with($className)
  60. ->will($this->returnValue(true))
  61. ;
  62. $container->expects($this->once())
  63. ->method('get')
  64. ->with($className)
  65. ->will($this->returnValue($invokableController))
  66. ;
  67. $resolver = $this->createControllerResolver(null, $container);
  68. $request = Request::create('/');
  69. $request->attributes->set('_controller', $className);
  70. $controller = $resolver->getController($request);
  71. $this->assertEquals($invokableController, $controller);
  72. }
  73. /**
  74. * @dataProvider getUndefinedControllers
  75. */
  76. public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
  77. {
  78. // All this logic needs to be duplicated, since calling parent::testGetControllerOnNonUndefinedFunction will override the expected excetion and not use the regex
  79. $resolver = $this->createControllerResolver();
  80. if (method_exists($this, 'expectException')) {
  81. $this->expectException($exceptionName);
  82. $this->expectExceptionMessageRegExp($exceptionMessage);
  83. } else {
  84. $this->setExpectedExceptionRegExp($exceptionName, $exceptionMessage);
  85. }
  86. $request = Request::create('/');
  87. $request->attributes->set('_controller', $controller);
  88. $resolver->getController($request);
  89. }
  90. public function getUndefinedControllers()
  91. {
  92. return array(
  93. array('foo', \LogicException::class, '/Unable to parse the controller name "foo"\./'),
  94. array('oof::bar', \InvalidArgumentException::class, '/Class "oof" does not exist\./'),
  95. array('stdClass', \LogicException::class, '/Unable to parse the controller name "stdClass"\./'),
  96. array(
  97. 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
  98. \InvalidArgumentException::class,
  99. '/.?[cC]ontroller(.*?) for URI "\/" is not callable\.( Expected method(.*) Available methods)?/',
  100. ),
  101. );
  102. }
  103. protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
  104. {
  105. if (!$container) {
  106. $container = $this->createMockContainer();
  107. }
  108. return new ContainerControllerResolver($container, $logger);
  109. }
  110. protected function createMockContainer()
  111. {
  112. return $this->getMockBuilder(ContainerInterface::class)->getMock();
  113. }
  114. }
  115. class InvokableController
  116. {
  117. public function __construct($bar) // mandatory argument to prevent automatic instantiation
  118. {
  119. }
  120. public function __invoke()
  121. {
  122. }
  123. }