Skip to content

Commit

Permalink
[FrameworkBundle] Return the invokable service if its name is the cla…
Browse files Browse the repository at this point in the history
…ss name
  • Loading branch information
dunglas authored and fabpot committed Mar 25, 2016
1 parent bd6d9bb commit 70b9309
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Expand Up @@ -78,6 +78,10 @@ protected function createController($controller)
*/
protected function instantiateController($class)
{
if ($this->container->has($class)) {
return $this->container->get($class);
}

$controller = parent::instantiateController($class);

if ($controller instanceof ContainerAwareInterface) {
Expand Down
Expand Up @@ -87,6 +87,8 @@ public function testGetControllerService()

public function testGetControllerInvokableService()
{
$invokableController = new InvokableController('bar');

$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
Expand All @@ -96,7 +98,7 @@ public function testGetControllerInvokableService()
$container->expects($this->once())
->method('get')
->with('foo')
->will($this->returnValue($this))
->will($this->returnValue($invokableController))
;

$resolver = $this->createControllerResolver(null, null, $container);
Expand All @@ -105,7 +107,33 @@ public function testGetControllerInvokableService()

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

$this->assertInstanceOf(get_class($this), $controller);
$this->assertEquals($invokableController, $controller);
}

public function testGetControllerInvokableServiceWithClassNameAsName()
{
$invokableController = new InvokableController('bar');
$className = __NAMESPACE__.'\InvokableController';

$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with($className)
->will($this->returnValue(true))
;
$container->expects($this->once())
->method('get')
->with($className)
->will($this->returnValue($invokableController))
;

$resolver = $this->createControllerResolver(null, null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', $className);

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

$this->assertEquals($invokableController, $controller);
}

/**
Expand Down Expand Up @@ -178,3 +206,14 @@ public function __invoke()
{
}
}

class InvokableController
{
public function __construct($bar) // mandatory argument to prevent automatic instantiation
{
}

public function __invoke()
{
}
}

0 comments on commit 70b9309

Please sign in to comment.