123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /*
- * 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.
- */
- 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 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());
- }
- 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()
- {
- $invokableController = new InvokableController('bar');
- $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($invokableController))
- ;
- $resolver = $this->createControllerResolver(null, null, $container);
- $request = Request::create('/');
- $request->attributes->set('_controller', 'foo');
- $controller = $resolver->getController($request);
- $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);
- }
- /**
- * @dataProvider getUndefinedControllers
- */
- public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
- {
- if (method_exists($this, 'expectException')) {
- $this->expectException($exceptionName);
- $this->expectExceptionMessage($exceptionMessage);
- } else {
- $this->setExpectedException($exceptionName, $exceptionMessage);
- }
- parent::testGetControllerOnNonUndefinedFunction($controller);
- }
- public function getUndefinedControllers()
- {
- return array(
- array('foo', '\LogicException', 'Unable to parse the controller name "foo".'),
- array('oof::bar', '\InvalidArgumentException', 'Class "oof" 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->getMockBuilder('Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser')->disableOriginalConstructor()->getMock();
- }
- protected function createMockContainer()
- {
- return $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
- }
- }
- class ContainerAwareController implements ContainerAwareInterface
- {
- private $container;
- public function setContainer(ContainerInterface $container = null)
- {
- $this->container = $container;
- }
- public function getContainer()
- {
- return $this->container;
- }
- public function testAction()
- {
- }
- public function __invoke()
- {
- }
- }
- class InvokableController
- {
- public function __construct($bar) // mandatory argument to prevent automatic instantiation
- {
- }
- public function __invoke()
- {
- }
- }
|