2014-06-21 13:25:07 +01:00
< ? php
2014-06-26 08:13:51 +01:00
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2014-06-21 13:25:07 +01:00
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 ]);
}
2015-09-09 13:19:27 +01:00
public function testGetControllerOnContainerAwareInvokable ()
{
$resolver = $this -> createControllerResolver ();
$request = Request :: create ( '/' );
$request -> attributes -> set ( '_controller' , 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController' );
$controller = $resolver -> getController ( $request );
$this -> assertInstanceOf ( 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController' , $controller );
$this -> assertInstanceOf ( 'Symfony\Component\DependencyInjection\ContainerInterface' , $controller -> getContainer ());
}
2014-06-21 13:25:07 +01:00
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 ()
{
2016-03-24 00:09:44 +00:00
$invokableController = new InvokableController ( 'bar' );
2014-06-21 13:25:07 +01:00
$container = $this -> createMockContainer ();
$container -> expects ( $this -> once ())
-> method ( 'has' )
-> with ( 'foo' )
-> will ( $this -> returnValue ( true ))
;
$container -> expects ( $this -> once ())
-> method ( 'get' )
-> with ( 'foo' )
2016-03-24 00:09:44 +00:00
-> will ( $this -> returnValue ( $invokableController ))
2014-06-21 13:25:07 +01:00
;
$resolver = $this -> createControllerResolver ( null , null , $container );
$request = Request :: create ( '/' );
$request -> attributes -> set ( '_controller' , 'foo' );
$controller = $resolver -> getController ( $request );
2016-03-24 00:09:44 +00:00
$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 );
2014-06-21 13:25:07 +01:00
}
/**
* @ dataProvider getUndefinedControllers
*/
public function testGetControllerOnNonUndefinedFunction ( $controller , $exceptionName = null , $exceptionMessage = null )
{
2014-04-26 01:10:40 +01:00
// All this logic needs to be duplicated, since calling parent::testGetControllerOnNonUndefinedFunction will override the expected excetion and not use the regex
$resolver = $this -> createControllerResolver ();
$this -> setExpectedExceptionRegExp ( $exceptionName , $exceptionMessage );
2014-06-21 13:25:07 +01:00
2014-04-26 01:10:40 +01:00
$request = Request :: create ( '/' );
$request -> attributes -> set ( '_controller' , $controller );
$resolver -> getController ( $request );
2014-06-21 13:25:07 +01:00
}
public function getUndefinedControllers ()
{
return array (
2014-04-26 01:10:40 +01:00
array ( 'foo' , '\LogicException' , '/Unable to parse the controller name "foo"\./' ),
2016-06-06 16:08:35 +01:00
array ( 'oof::bar' , '\InvalidArgumentException' , '/Class "oof" does not exist\./' ),
2014-04-26 01:10:40 +01:00
array ( 'stdClass' , '\LogicException' , '/Unable to parse the controller name "stdClass"\./' ),
2014-06-21 13:25:07 +01:00
array (
'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar' ,
'\InvalidArgumentException' ,
2014-04-26 01:10:40 +01:00
'/.?[cC]ontroller(.*?) for URI "\/" is not callable\.( Expected method(.*) Available methods)?/' ,
2014-09-22 12:59:59 +01:00
),
2014-06-21 13:25:07 +01:00
);
}
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 ()
{
2016-12-19 09:02:29 +00:00
return $this -> getMockBuilder ( 'Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser' ) -> disableOriginalConstructor () -> getMock ();
2014-06-21 13:25:07 +01:00
}
protected function createMockContainer ()
{
2016-12-19 09:02:29 +00:00
return $this -> getMockBuilder ( 'Symfony\Component\DependencyInjection\ContainerInterface' ) -> getMock ();
2014-06-21 13:25:07 +01:00
}
}
class ContainerAwareController implements ContainerAwareInterface
{
private $container ;
public function setContainer ( ContainerInterface $container = null )
{
$this -> container = $container ;
}
public function getContainer ()
{
return $this -> container ;
}
public function testAction ()
{
}
2015-09-09 13:19:27 +01:00
public function __invoke ()
{
}
2014-06-21 13:25:07 +01:00
}
2016-03-24 00:09:44 +00:00
class InvokableController
{
public function __construct ( $bar ) // mandatory argument to prevent automatic instantiation
{
}
public function __invoke ()
{
}
}