PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php

https://gitlab.com/Isaki/le331.fr
PHP | 180 lines | 135 code | 34 blank | 11 comment | 2 complexity | dfed377cdcb2162f796cf04206855581 MD5 | raw file
  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. $container = $this->createMockContainer();
  73. $container->expects($this->once())
  74. ->method('has')
  75. ->with('foo')
  76. ->will($this->returnValue(true))
  77. ;
  78. $container->expects($this->once())
  79. ->method('get')
  80. ->with('foo')
  81. ->will($this->returnValue($this))
  82. ;
  83. $resolver = $this->createControllerResolver(null, null, $container);
  84. $request = Request::create('/');
  85. $request->attributes->set('_controller', 'foo');
  86. $controller = $resolver->getController($request);
  87. $this->assertInstanceOf(get_class($this), $controller);
  88. }
  89. /**
  90. * @dataProvider getUndefinedControllers
  91. */
  92. public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
  93. {
  94. $this->setExpectedException($exceptionName, $exceptionMessage);
  95. parent::testGetControllerOnNonUndefinedFunction($controller);
  96. }
  97. public function getUndefinedControllers()
  98. {
  99. return array(
  100. array('foo', '\LogicException', 'Unable to parse the controller name "foo".'),
  101. array('foo::bar', '\InvalidArgumentException', 'Class "foo" does not exist.'),
  102. array('stdClass', '\LogicException', 'Unable to parse the controller name "stdClass".'),
  103. array(
  104. 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
  105. '\InvalidArgumentException',
  106. 'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar" for URI "/" is not callable.',
  107. ),
  108. );
  109. }
  110. protected function createControllerResolver(LoggerInterface $logger = null, ControllerNameParser $parser = null, ContainerInterface $container = null)
  111. {
  112. if (!$parser) {
  113. $parser = $this->createMockParser();
  114. }
  115. if (!$container) {
  116. $container = $this->createMockContainer();
  117. }
  118. return new ControllerResolver($container, $parser, $logger);
  119. }
  120. protected function createMockParser()
  121. {
  122. return $this->getMock('Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser', array(), array(), '', false);
  123. }
  124. protected function createMockContainer()
  125. {
  126. return $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  127. }
  128. }
  129. class ContainerAwareController implements ContainerAwareInterface
  130. {
  131. private $container;
  132. public function setContainer(ContainerInterface $container = null)
  133. {
  134. $this->container = $container;
  135. }
  136. public function getContainer()
  137. {
  138. return $this->container;
  139. }
  140. public function testAction()
  141. {
  142. }
  143. public function __invoke()
  144. {
  145. }
  146. }