Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Simplify "invokable" controllers as services
- Loading branch information
Showing
3 changed files
with
176 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters