|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Test\Controller; |
| 13 | + |
| 14 | +use Psr\Container\ContainerInterface; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | +use Symfony\Component\Debug\ErrorHandler; |
| 17 | +use Symfony\Component\DependencyInjection\Container; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +class ContainerControllerResolverTestCase extends ControllerResolverTestCase |
| 25 | +{ |
| 26 | + public function testGetControllerServiceWithSingleColon() |
| 27 | + { |
| 28 | + $service = new ControllerTestService('foo'); |
| 29 | + |
| 30 | + $container = $this->createMockContainer(); |
| 31 | + $container->expects($this->once()) |
| 32 | + ->method('has') |
| 33 | + ->with('foo') |
| 34 | + ->willReturn(true); |
| 35 | + $container->expects($this->once()) |
| 36 | + ->method('get') |
| 37 | + ->with('foo') |
| 38 | + ->willReturn($service) |
| 39 | + ; |
| 40 | + |
| 41 | + $resolver = $this->createControllerResolver(null, $container); |
| 42 | + $request = Request::create('/'); |
| 43 | + $request->attributes->set('_controller', 'foo:action'); |
| 44 | + |
| 45 | + $controller = $resolver->getController($request); |
| 46 | + |
| 47 | + $this->assertSame($service, $controller[0]); |
| 48 | + $this->assertSame('action', $controller[1]); |
| 49 | + } |
| 50 | + |
| 51 | + public function testGetControllerService() |
| 52 | + { |
| 53 | + $service = new ControllerTestService('foo'); |
| 54 | + |
| 55 | + $container = $this->createMockContainer(); |
| 56 | + $container->expects($this->once()) |
| 57 | + ->method('has') |
| 58 | + ->with('foo') |
| 59 | + ->willReturn(true); |
| 60 | + $container->expects($this->once()) |
| 61 | + ->method('get') |
| 62 | + ->with('foo') |
| 63 | + ->willReturn($service) |
| 64 | + ; |
| 65 | + |
| 66 | + $resolver = $this->createControllerResolver(null, $container); |
| 67 | + $request = Request::create('/'); |
| 68 | + $request->attributes->set('_controller', 'foo::action'); |
| 69 | + |
| 70 | + $controller = $resolver->getController($request); |
| 71 | + |
| 72 | + $this->assertSame($service, $controller[0]); |
| 73 | + $this->assertSame('action', $controller[1]); |
| 74 | + } |
| 75 | + |
| 76 | + public function testGetControllerInvokableService() |
| 77 | + { |
| 78 | + $service = new InvokableControllerService('bar'); |
| 79 | + |
| 80 | + $container = $this->createMockContainer(); |
| 81 | + $container->expects($this->once()) |
| 82 | + ->method('has') |
| 83 | + ->with('foo') |
| 84 | + ->willReturn(true) |
| 85 | + ; |
| 86 | + $container->expects($this->once()) |
| 87 | + ->method('get') |
| 88 | + ->with('foo') |
| 89 | + ->willReturn($service) |
| 90 | + ; |
| 91 | + |
| 92 | + $resolver = $this->createControllerResolver(null, $container); |
| 93 | + $request = Request::create('/'); |
| 94 | + $request->attributes->set('_controller', 'foo'); |
| 95 | + |
| 96 | + $controller = $resolver->getController($request); |
| 97 | + |
| 98 | + $this->assertSame($service, $controller); |
| 99 | + } |
| 100 | + |
| 101 | + public function testGetControllerInvokableServiceWithClassNameAsName() |
| 102 | + { |
| 103 | + $service = new InvokableControllerService('bar'); |
| 104 | + |
| 105 | + $container = $this->createMockContainer(); |
| 106 | + $container->expects($this->once()) |
| 107 | + ->method('has') |
| 108 | + ->with(InvokableControllerService::class) |
| 109 | + ->willReturn(true) |
| 110 | + ; |
| 111 | + $container->expects($this->once()) |
| 112 | + ->method('get') |
| 113 | + ->with(InvokableControllerService::class) |
| 114 | + ->willReturn($service) |
| 115 | + ; |
| 116 | + |
| 117 | + $resolver = $this->createControllerResolver(null, $container); |
| 118 | + $request = Request::create('/'); |
| 119 | + $request->attributes->set('_controller', InvokableControllerService::class); |
| 120 | + |
| 121 | + $controller = $resolver->getController($request); |
| 122 | + |
| 123 | + $this->assertSame($service, $controller); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @dataProvider getControllers |
| 128 | + */ |
| 129 | + public function testInstantiateControllerWhenControllerStartsWithABackslash($controller) |
| 130 | + { |
| 131 | + $service = new ControllerTestService('foo'); |
| 132 | + $class = ControllerTestService::class; |
| 133 | + |
| 134 | + $container = $this->createMockContainer(); |
| 135 | + $container->expects($this->once())->method('has')->with($class)->willReturn(true); |
| 136 | + $container->expects($this->once())->method('get')->with($class)->willReturn($service); |
| 137 | + |
| 138 | + $resolver = $this->createControllerResolver(null, $container); |
| 139 | + $request = Request::create('/'); |
| 140 | + $request->attributes->set('_controller', $controller); |
| 141 | + |
| 142 | + $controller = $resolver->getController($request); |
| 143 | + |
| 144 | + $this->assertInstanceOf(ControllerTestService::class, $controller[0]); |
| 145 | + $this->assertSame('action', $controller[1]); |
| 146 | + } |
| 147 | + |
| 148 | + public function getControllers() |
| 149 | + { |
| 150 | + return [ |
| 151 | + ['\\'.ControllerTestService::class.'::action'], |
| 152 | + ['\\'.ControllerTestService::class.':action'], |
| 153 | + ]; |
| 154 | + } |
| 155 | + |
| 156 | + public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName() |
| 157 | + { |
| 158 | + $this->expectException('InvalidArgumentException'); |
| 159 | + $this->expectExceptionMessage('Controller "Symfony\Component\HttpKernel\Test\Controller\ControllerTestService" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?'); |
| 160 | + $container = $this->getMockBuilder(Container::class)->getMock(); |
| 161 | + $container->expects($this->once()) |
| 162 | + ->method('has') |
| 163 | + ->with(ControllerTestService::class) |
| 164 | + ->willReturn(false) |
| 165 | + ; |
| 166 | + |
| 167 | + $container->expects($this->atLeastOnce()) |
| 168 | + ->method('getRemovedIds') |
| 169 | + ->with() |
| 170 | + ->willReturn([ControllerTestService::class => true]) |
| 171 | + ; |
| 172 | + |
| 173 | + $resolver = $this->createControllerResolver(null, $container); |
| 174 | + $request = Request::create('/'); |
| 175 | + $request->attributes->set('_controller', [ControllerTestService::class, 'action']); |
| 176 | + |
| 177 | + $resolver->getController($request); |
| 178 | + } |
| 179 | + |
| 180 | + public function testExceptionWhenUsingRemovedControllerService() |
| 181 | + { |
| 182 | + $this->expectException('InvalidArgumentException'); |
| 183 | + $this->expectExceptionMessage('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"?'); |
| 184 | + $container = $this->getMockBuilder(Container::class)->getMock(); |
| 185 | + $container->expects($this->once()) |
| 186 | + ->method('has') |
| 187 | + ->with('app.my_controller') |
| 188 | + ->willReturn(false) |
| 189 | + ; |
| 190 | + |
| 191 | + $container->expects($this->atLeastOnce()) |
| 192 | + ->method('getRemovedIds') |
| 193 | + ->with() |
| 194 | + ->willReturn(['app.my_controller' => true]) |
| 195 | + ; |
| 196 | + |
| 197 | + $resolver = $this->createControllerResolver(null, $container); |
| 198 | + |
| 199 | + $request = Request::create('/'); |
| 200 | + $request->attributes->set('_controller', 'app.my_controller'); |
| 201 | + $resolver->getController($request); |
| 202 | + } |
| 203 | + |
| 204 | + public function getUndefinedControllers() |
| 205 | + { |
| 206 | + $tests = parent::getUndefinedControllers(); |
| 207 | + $tests[0] = ['foo', \InvalidArgumentException::class, 'Controller "foo" does neither exist as service nor as class']; |
| 208 | + $tests[1] = ['oof::bar', \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class']; |
| 209 | + $tests[2] = [['oof', 'bar'], \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class']; |
| 210 | + $tests[] = [ |
| 211 | + [ControllerTestService::class, 'action'], |
| 212 | + \InvalidArgumentException::class, |
| 213 | + 'Controller "Symfony\Component\HttpKernel\Test\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?', |
| 214 | + ]; |
| 215 | + $tests[] = [ |
| 216 | + ControllerTestService::class.'::action', |
| 217 | + \InvalidArgumentException::class, 'Controller "Symfony\Component\HttpKernel\Test\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?', |
| 218 | + ]; |
| 219 | + $tests[] = [ |
| 220 | + InvokableControllerService::class, |
| 221 | + \InvalidArgumentException::class, |
| 222 | + 'Controller "Symfony\Component\HttpKernel\Test\Controller\InvokableControllerService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?', |
| 223 | + ]; |
| 224 | + |
| 225 | + return $tests; |
| 226 | + } |
| 227 | + |
| 228 | + protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null) |
| 229 | + { |
| 230 | + if (!$container) { |
| 231 | + $container = $this->createMockContainer(); |
| 232 | + } |
| 233 | + |
| 234 | + return new ContainerControllerResolver($container, $logger); |
| 235 | + } |
| 236 | + |
| 237 | + protected function createMockContainer() |
| 238 | + { |
| 239 | + return $this->getMockBuilder(ContainerInterface::class)->getMock(); |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +class InvokableControllerService |
| 244 | +{ |
| 245 | + public function __construct($bar) // mandatory argument to prevent automatic instantiation |
| 246 | + { |
| 247 | + } |
| 248 | + |
| 249 | + public function __invoke() |
| 250 | + { |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +class ControllerTestService |
| 255 | +{ |
| 256 | + public function __construct($foo) |
| 257 | + { |
| 258 | + } |
| 259 | + |
| 260 | + public function action() |
| 261 | + { |
| 262 | + } |
| 263 | +} |
0 commit comments