8000 [HttpKernel] fix merge · symfony/symfony@e323097 · GitHub
[go: up one dir, main page]

Skip to content

Commit e323097

Browse files
[HttpKernel] fix merge
1 parent 1575e27 commit e323097

File tree

2 files changed

+181
-292
lines changed

2 files changed

+181
-292
lines changed

src/Symfony/Component/HttpKernel/Tests/Controller/ContainerControllerResolverTest.php

Lines changed: 91 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Psr\Log\LoggerInterface;
16-
use Symfony\Component\Debug\ErrorHandler;
1716
use Symfony\Component\DependencyInjection\Container;
1817
use Symfony\Component\HttpFoundation\Request;
1918
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
2019

2120
class ContainerControllerResolverTest extends ControllerResolverTest
2221
{
23-
public function testGetControllerService()
22+
public function testGetControllerServiceWithSingleColon()
2423
{
24+
$service = new ControllerTestService('foo');
25+
2526
$container = $this->createMockContainer();
2627
$container->expects($this->once())
2728
->method('has')
@@ -30,191 +31,163 @@ public function testGetControllerService()
3031
$container->expects($this->once())
3132
->method('get')
3233
->with('foo')
33-
->willReturn($this)
34+
->willReturn($service)
3435
;
3536

3637
$resolver = $this->createControllerResolver(null, $container);
3738
$request = Request::create('/');
38-
$request->attributes->set('_controller', 'foo:controllerMethod1');
39+
$request->attributes->set('_controller', 'foo:action');
3940

4041
$controller = $resolver->getController($request);
4142

42-
$this->assertInstanceOf(\get_class($this), $controller[0]);
43-
$this->assertSame('controllerMethod1', $controller[1]);
43+
$this->assertSame($service, $controller[0]);
44+
$this->assertSame('action', $controller[1]);
4445
}
4546

46-
public function testGetControllerInvokableService()
47+
public function testGetControllerService()
4748
{
48-
$invokableController = new InvokableController('bar');
49+
$service = new ControllerTestService('foo');
4950

5051
$container = $this->createMockContainer();
5152
$container->expects($this->once())
5253
->method('has')
5354
->with('foo')
54-
->willReturn(true)
55-
;
55+
->willReturn(true);
5656
$container->expects($this->once())
5757
->method('get')
5858
->with('foo')
59-
->willReturn($invokableController)
59+
->willReturn($service)
6060
;
6161

6262
$resolver = $this->createControllerResolver(null, $container);
6363
$request = Request::create('/');
64-
$request->attributes->set('_controller', 'foo');
64+
$request->attributes->set('_controller', 'foo::action');
6565

6666
$controller = $resolver->getController($request);
6767

68-
$this->assertEquals($invokableController, $controller);
68+
$this->assertSame($service, $controller[0]);
69+
$this->assertSame('action', $controller[1]);
6970
}
7071

71-
public function testGetControllerInvokableServiceWithClassNameAsName()
72+
public function testGetControllerInvokableService()
7273
{
73-
$invokableController = new InvokableController('bar');
74-
$className = __NAMESPACE__.'\InvokableController';
74+
$service = new InvokableControllerService('bar');
7575

7676
$container = $this->createMockContainer();
7777
$container->expects($this->once())
7878
->method('has')
79-
->with($className)
79+
->with('foo')
8080
->willReturn(true)
8181
;
8282
$container->expects($this->once())
8383
->method('get')
84-
->with($className)
85-
->willReturn($invokableController)
84+
->with('foo')
85+
->willReturn($service)
8686
;
8787

8888
$resolver = $this->createControllerResolver(null, $container);
8989
$request = Request::create('/');
90-
$request->attributes->set('_controller', $className);
90+
$request->attributes->set('_controller', 'foo');
9191

9292
$controller = $resolver->getController($request);
9393

94-
$this->assertEquals($invokableController, $controller);
94+
$this->assertSame($service, $controller);
9595
}
9696

97-
public function testNonInstantiableController()
97+
public function testGetControllerInvokableServiceWithClassNameAsName()
9898
{
99+
$service = new InvokableControllerService('bar');
100+
99101
$container = $this->createMockContainer();
100102
$container->expects($this->once())
101103
->method('has')
102-
->with(NonInstantiableController::class)
103-
->willReturn(false)
104+
->with(InvokableControllerService::class)
105+
->willReturn(true)
106+
;
107+
$container->expects($this->once())
108+
->method('get')
109+
->with(InvokableControllerService::class)
110+
->willReturn($service)
104111
;
105112

106113
$resolver = $this->createControllerResolver(null, $container);
107114
$request = Request::create('/');
108-
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);
115+
$request->attributes->set('_controller', InvokableControllerService::class);
109116

110117
$controller = $resolver->getController($request);
111118

112-
$this->assertSame([NonInstantiableController::class, ' 10000 action'], $controller);
119+
$this->assertSame($service, $controller);
113120
}
114121

115-
public function testNonConstructController()
116-
{
117-
$this->expectException('LogicException');
118-
$this->expectExceptionMessage('Controller "Symfony\Component\HttpKernel\Tests\Controller\ImpossibleConstructController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
119-
$container = $this->getMockBuilder(Container::class)->getMock();
120-
$container->expects($this->at(0))
121-
->method('has')
122-
->with(ImpossibleConstructController::class)
123-
->willReturn(true)
124-
;
125-
126-
$container->expects($this->at(1))
127-
->method('has')
128-
->with(ImpossibleConstructController::class)
129-
->willReturn(false)
130-
;
131-
132-
$container->expects($this->atLeastOnce())
133-
->method('getRemovedIds')
134-
->with()
135-
->willReturn([ImpossibleConstructController::class => true])
136-
;
137-
138-
$resolver = $this->createControllerResolver(null, $container);
139-
$request = Request::create('/');
140-
$request->attributes->set('_controller', [ImpossibleConstructController::class, 'action']);
141-
142-
if (\PHP_VERSION_ID < 70100) {
143-
ErrorHandler::register();
144-
try {
145-
$resolver->getController($request);
146-
} finally {
147-
restore_error_handler();
148-
restore_exception_handler();
149-
}
150-
} else {
151-
$resolver->getController($request);
152-
}
153-
}
154-
155-
public function testNonInstantiableControllerWithCorrespondingService()
122+
/**
123+
* @dataProvider getControllers
124+
*/
125+
public function testInstantiateControllerWhenControllerStartsWithABackslash($controller)
156126
{
157-
$service = new \stdClass();
127+
$service = new ControllerTestService('foo');
128+
$class = ControllerTestService::class;
158129

159130
$container = $this->createMockContainer();
160-
$container->expects($this->atLeastOnce())
161-
->method('has')
162-
->with(NonInstantiableController::class)
163-
->willReturn(true)
164-
;
165-
$container->expects($this->atLeastOnce())
166-
->method('get')
167-
->with(NonInstantiableController::class)
168-
->willReturn($service)
169-
;
131+
$container->expects($this->once())->method('has')->with($class)->willReturn(true);
132+
$container->expects($this->once())->method('get')->with($class)->willReturn($service);
170133

171134
$resolver = $this->createControllerResolver(null, $container);
172135
$request = Request::create('/');
173-
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);
136+
$request->attributes->set('_controller', $controller);
174137

175138
$controller = $resolver->getController($request);
176139

177-
$this->assertSame([$service, 'action'], $controller);
140+
$this->assertInstanceOf(ControllerTestService::class, $controller[0]);
141+
$this->assertSame('action', $controller[1]);
178142
}
179143

180-
public function testExceptionWhenUsingRemovedControllerService()
144+
public function getControllers()
181145
{
182-
$this->expectException('LogicException');
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"?');
146+
return [
147+
['\\'.ControllerTestService::class.'::action'],
148+
['\\'.ControllerTestService::class.':action'],
149+
];
150+
}
151+
152+
public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
153+
{
154+
$this->expectException('InvalidArgumentException');
155+
$this->expectExceptionMessage('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"?');
184156
$container = $this->getMockBuilder(Container::class)->getMock();
185-
$container->expects($this->at(0))
157+
$container->expects($this->once())
186158
->method('has')
187-
->with('app.my_controller')
159+
->with(ControllerTestService::class)
188160
->willReturn(false)
189161
;
190162

191163
$container->expects($this->atLeastOnce())
192164
->method('getRemovedIds')
193165
->with()
194-
->willReturn(['app.my_controller' => true])
166+
->willReturn([ControllerTestService::class => true])
195167
;
196168

197169
$resolver = $this->createControllerResolver(null, $container);
198-
199170
$request = Request::create('/');
200-
$request->attributes->set('_controller', 'app.my_controller');
171+
$request->attributes->set('_controller', [ControllerTestService::class, 'action']);
172+
201173
$resolver->getController($request);
202174
}
203175

204-
public function testExceptionWhenUsingControllerWithoutAnInvokeMethod()
176+
public function testExceptionWhenUsingRemovedControllerService()
205177
{
206-
$this->expectException('LogicException');
207-
$this->expectExceptionMessage('Controller "app.my_controller" cannot be called without a method name. Did you forget an "__invoke" method?');
178+
$this->expectException('InvalidArgumentException');
179+
$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"?');
208180
$container = $this->getMockBuilder(Container::class)->getMock();
209181
$container->expects($this->once())
210182
->method('has')
211183
->with('app.my_controller')
212-
->willReturn(true)
184+
->willReturn(false)
213185
;
214-
$container->expects($this->once())
215-
->method('get')
216-
->with('app.my_controller')
217-
->willReturn(new ImpossibleConstructController('toto', 'controller'))
186+
187+
$container->expects($this->atLeastOnce())
188+
->method('getRemovedIds')
189+
->with()
190+
->willReturn(['app.my_controller' => true])
218191
;
219192

220193
$resolver = $this->createControllerResolver(null, $container);
@@ -224,33 +197,28 @@ public function testExceptionWhenUsingControllerWithoutAnInvokeMethod()
224197
$resolver->getController($request);
225198
}
226199

227-
/**
228-
* @dataProvider getUndefinedControllers
229-
*/
230-
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
231-
{
232-
// All this logic needs to be duplicated, since calling parent::testGetControllerOnNonUndefinedFunction will override the expected excetion and not use the regex
233-
$resolver = $this->createControllerResolver();
234-
$this->expectException($exceptionName);
235-
$this->expectExceptionMessageRegExp($exceptionMessage);
236-
237-
$request = Request::create('/');
238-
$request->attributes->set('_controller', $controller);
239-
$resolver->getController($request);
240-
}
241-
242200
public function getUndefinedControllers()
243201
{
244-
return [
245-
['foo', \LogicException::class, '/Controller not found: service "foo" does not exist\./'],
246-
['oof::bar', \InvalidArgumentException::class, '/Class "oof" does not exist\./'],
247-
['stdClass', \LogicException::class, '/Controller not found: service "stdClass" does not exist\./'],
248-
[
249-
'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
250-
\InvalidArgumentException::class,
251-
'/.?[cC]ontroller(.*?) for URI "\/" is not callable\.( Expected method(.*) Available methods)?/',
252-
],
202+
$tests = parent::getUndefinedControllers();
203+
$tests[0] = ['foo', \InvalidArgumentException::class, 'Controller "foo" does neither exist as service nor as class'];
204+
$tests[1] = ['oof::bar', \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
205+
$tests[2] = [['oof', 'bar'], \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
206+
$tests[] = [
207+
[ControllerTestService::class, 'action'],
208+
\InvalidArgumentException::class,
209+
'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?',
210+
];
211+
$tests[] = [
212+
ControllerTestService::class.'::action',
213+
\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?',
214+
];
215+
$tests[] = [
216+
InvokableControllerService::class,
217+
\InvalidArgumentException::class,
218+
'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?',
253219
];
220+
221+
return $tests;
254222
}
255223

256224
protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
@@ -268,7 +236,7 @@ protected function createMockContainer()
268236
}
269237
}
270238

271-
class InvokableController
239+
class InvokableControllerService
272240
{
273241
public function __construct($bar) // mandatory argument to prevent automatic instantiation
274242
{
@@ -279,16 +247,9 @@ public function __invoke()
279247
}
280248
}
281249

282-
abstract class NonInstantiableController
283-
{
284-
public static function action()
285-
{
286-
}
287-
}
288-
289-
class ImpossibleConstructController
250+
class ControllerTestService
290251
{
291-
public function __construct($toto, $controller)
252+
public function __construct($foo)
292253
{
293254
}
294255

0 commit comments

Comments
 (0)
0