8000 [FrameworkBundle] Simplify "invokable" controllers as services by kbond · Pull Request #11193 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Simplify "invokable" controllers as services #11193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ protected function createController($controller)
list($service, $method) = explode(':', $controller, 2);

return array($this->container->get($service), $method);
} elseif ($this->container->has($controller) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably previous condition also must check whether container has such service.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. The previous condition is explicitly a service syntax. Not checking if the container has a service means you get an exception telling you the service does not exist in case you made a typo.

method_exists($service = $this->container->get($controller), '__invoke')
) {
return $service;
} else {
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
}
Expand Down
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest as BaseControllerResolverTest;

class ControllerResolverTest extends BaseControllerResolverTest
{
public function testGetControllerOnContainerAware()
{
$resolver = $this->createControllerResolver();
$request = Request::create('/');
$request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController::testAction');

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

$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller[0]->getContainer());
$this->assertSame('testAction', $controller[1]);
}

public function testGetControllerWithBundleNotation()
{
$shortName = 'FooBundle:Default:test';
$parser = $this->createMockParser();
$parser->expects($this->once())
->method('parse')
->with($shortName)
->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController::testAction'))
;

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

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

$this->assertInstanceOf('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController', $controller[0]);
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller[0]->getContainer());
$this->assertSame('testAction', $controller[1]);
}

public function testGetControllerService()
{
$container = $this->createMockContainer();
$container->expects($this->once())
->method('get')
->with('foo')
->will($this->returnValue($this))
;

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

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

$this->assertInstanceOf(get_class($this), $controller[0]);
$this->assertSame('controllerMethod1', $controller[1]);
}

public function testGetControllerInvokableService()
{
$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with('foo')
->will($this->returnValue(true))
;
$container->expects($this->once())
->method('get')
->with('foo')
->will($this->returnValue($this))
;

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

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

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

/**
* @dataProvider getUndefinedControllers
*/
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
{
$this->setExpectedException($exceptionName, $exceptionMessage);

parent::testGetControllerOnNonUndefinedFunction($controller);
}

public function getUndefinedControllers()
{
return array(
array('foo', '\LogicException', 'Unable to parse the controller name "foo".'),
array('foo::bar', '\InvalidArgumentException', 'Class "foo" does not exist.'),
array('stdClass', '\LogicException', 'Unable to parse the controller name "stdClass".'),
array(
'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
'\InvalidArgumentException',
'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar" for URI "/" is not callable.'
)
);
}

protected function createControllerResolver(LoggerInterface $logger = null, ControllerNameParser $parser = null, ContainerInterface $container = null)
{
if (!$parser) {
$parser = $this->createMockParser();
}

if (!$container) {
$container = $this->createMockContainer();
}

return new ControllerResolver($container, $parser, $logger);
}

protected function createMockParser()
{
return $this->getMock('Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser', array(), array(), '', false);
}

protected function createMockContainer()
{
return $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
}
}

class ContainerAwareController implements ContainerAwareInterface
{
private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function getContainer()
{
return $this->container;
}

public function testAction()
{
}
}
Origi ED4F nal file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\HttpKernel\Tests\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Tests\Logger;
use Symfony\Component\HttpFoundation\Request;

class ControllerResolverTest extends \PHPUnit_Framework_TestCase
Expand All @@ -28,15 +28,15 @@ public function testGetControllerWithoutControllerParameter()
{
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing');
$resolver = new ControllerResolver($logger);
$resolver = $this->createControllerResolver($logger);

$request = Request::create('/');
$this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
}

public function testGetControllerWithLambda()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', $lambda = function () {});
Expand All @@ -46,7 +46,7 @@ public function testGetControllerWithLambda()

public function testGetControllerWithObjectAndInvokeMethod()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', $this);
Expand All @@ -56,7 +56,7 @@ public function testGetControllerWithObjectAndInvokeMethod()

public function testGetControllerWithObjectAndMethod()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', array($this, 'controllerMethod1'));
Expand All @@ -66,7 +66,7 @@ public function testGetControllerWithObjectAndMethod()

public function testGetControllerWithClassAndMethod()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'));
Expand All @@ -76,7 +76,7 @@ public function testGetControllerWithClassAndMethod()

public function testGetControllerWithObjectAndMethodAsString()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod1');
Expand All @@ -86,7 +86,7 @@ public function testGetControllerWithObjectAndMethodAsString()

public function testGetControllerWithClassAndInvokeMethod()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest');
Expand All @@ -99,7 +99,7 @@ public function testGetControllerWithClassAndInvokeMethod()
*/
public function testGetControllerOnObjectWithoutInvokeMethod()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', new \stdClass());
Expand All @@ -108,7 +108,7 @@ public function testGetControllerOnObjectWithoutInvokeMethod()

public function testGetControllerWithFunction()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
Expand All @@ -122,7 +122,7 @@ public function testGetControllerWithFunction()
*/
public function testGetControllerOnNonUndefinedFunction($controller)
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$request->attributes->set('_controller', $controller);
Expand All @@ -141,7 +141,7 @@ public function getUndefinedControllers()

public function testGetArguments()
{
$resolver = new ControllerResolver();
$resolver = $this->createControllerResolver();

$request = Request::create('/');
$controller = array(new self(), 'testGetArguments');
Expand Down Expand Up @@ -214,6 +214,11 @@ public function testCreateControllerCanReturnAnyCallable()
$mock->getController($request);
}

protected function createControllerResolver(LoggerInterface $logger = null)
{
return new ControllerResolver($logger);
}

public function __invoke($foo, $bar = null)
{
}
Expand Down
0