ControllerResolverTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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;
  11. use Symfony\Component\HttpKernel\Controller\ControllerResolver;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class ControllerResolverTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  18. $this->markTestSkipped('The "HttpFoundation" component is not available');
  19. }
  20. }
  21. public function testGetController()
  22. {
  23. $logger = new Logger();
  24. $resolver = new ControllerResolver($logger);
  25. $request = Request::create('/');
  26. $this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
  27. $this->assertEquals(array('Unable to look for the controller as the "_controller" parameter is missing'), $logger->getLogs('warning'));
  28. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\ControllerResolverTest::testGetController');
  29. $controller = $resolver->getController($request);
  30. $this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
  31. $request->attributes->set('_controller', $lambda = function () {});
  32. $controller = $resolver->getController($request);
  33. $this->assertSame($lambda, $controller);
  34. $request->attributes->set('_controller', $this);
  35. $controller = $resolver->getController($request);
  36. $this->assertSame($this, $controller);
  37. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\ControllerResolverTest');
  38. $controller = $resolver->getController($request);
  39. $this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\ControllerResolverTest', $controller);
  40. $request->attributes->set('_controller', array($this, 'controllerMethod1'));
  41. $controller = $resolver->getController($request);
  42. $this->assertSame(array($this, 'controllerMethod1'), $controller);
  43. $request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\ControllerResolverTest', 'controllerMethod4'));
  44. $controller = $resolver->getController($request);
  45. $this->assertSame(array('Symfony\Component\HttpKernel\Tests\ControllerResolverTest', 'controllerMethod4'), $controller);
  46. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\some_controller_function');
  47. $controller = $resolver->getController($request);
  48. $this->assertSame('Symfony\Component\HttpKernel\Tests\some_controller_function', $controller);
  49. $request->attributes->set('_controller', 'foo');
  50. try {
  51. $resolver->getController($request);
  52. $this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute is not well-formatted');
  53. } catch (\Exception $e) {
  54. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute is not well-formatted');
  55. }
  56. $request->attributes->set('_controller', 'foo::bar');
  57. try {
  58. $resolver->getController($request);
  59. $this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent class');
  60. } catch (\Exception $e) {
  61. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent class');
  62. }
  63. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\ControllerResolverTest::bar');
  64. try {
  65. $resolver->getController($request);
  66. $this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent method');
  67. } catch (\Exception $e) {
  68. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent method');
  69. }
  70. }
  71. public function testGetArguments()
  72. {
  73. $resolver = new ControllerResolver();
  74. $request = Request::create('/');
  75. $controller = array(new self(), 'testGetArguments');
  76. $this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
  77. $request = Request::create('/');
  78. $request->attributes->set('foo', 'foo');
  79. $controller = array(new self(), 'controllerMethod1');
  80. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
  81. $request = Request::create('/');
  82. $request->attributes->set('foo', 'foo');
  83. $controller = array(new self(), 'controllerMethod2');
  84. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
  85. $request->attributes->set('bar', 'bar');
  86. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
  87. $request = Request::create('/');
  88. $request->attributes->set('foo', 'foo');
  89. $controller = function ($foo) {};
  90. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
  91. $request = Request::create('/');
  92. $request->attributes->set('foo', 'foo');
  93. $controller = function ($foo, $bar = 'bar') {};
  94. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  95. $request = Request::create('/');
  96. $request->attributes->set('foo', 'foo');
  97. $controller = new self();
  98. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller));
  99. $request->attributes->set('bar', 'bar');
  100. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  101. $request = Request::create('/');
  102. $request->attributes->set('foo', 'foo');
  103. $request->attributes->set('foobar', 'foobar');
  104. $controller = 'Symfony\Component\HttpKernel\Tests\some_controller_function';
  105. $this->assertEquals(array('foo', 'foobar'), $resolver->getArguments($request, $controller));
  106. $request = Request::create('/');
  107. $request->attributes->set('foo', 'foo');
  108. $request->attributes->set('foobar', 'foobar');
  109. $controller = array(new self(), 'controllerMethod3');
  110. if (version_compare(PHP_VERSION, '5.3.16', '==')) {
  111. $this->markTestSkipped('PHP 5.3.16 has a major bug in the Reflection sub-system');
  112. } else {
  113. try {
  114. $resolver->getArguments($request, $controller);
  115. $this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  116. } catch (\Exception $e) {
  117. $this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  118. }
  119. }
  120. $request = Request::create('/');
  121. $controller = array(new self(), 'controllerMethod5');
  122. $this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
  123. }
  124. public function testCreateControllerCanReturnAnyCallable()
  125. {
  126. $mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
  127. $mock->expects($this->once())->method('createController')->will($this->returnValue('Symfony\Component\HttpKernel\Tests\some_controller_function'));
  128. $request = Request::create('/');
  129. $request->attributes->set('_controller', 'foobar');
  130. $mock->getController($request);
  131. }
  132. public function __invoke($foo, $bar = null)
  133. {
  134. }
  135. protected function controllerMethod1($foo)
  136. {
  137. }
  138. protected function controllerMethod2($foo, $bar = null)
  139. {
  140. }
  141. protected function controllerMethod3($foo, $bar = null, $foobar)
  142. {
  143. }
  144. protected static function controllerMethod4()
  145. {
  146. }
  147. protected function controllerMethod5(Request $request)
  148. {
  149. }
  150. }
  151. function some_controller_function($foo, $foobar)
  152. {
  153. }