Вход Регистрация
Файл: symfony-2.7/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php
Строк: 340
<?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 SymfonyBundleFrameworkBundleTestsController;

use 
PsrLogLoggerInterface;
use 
SymfonyBundleFrameworkBundleControllerControllerNameParser;
use 
SymfonyBundleFrameworkBundleControllerControllerResolver;
use 
SymfonyComponentDependencyInjectionContainerAwareInterface;
use 
SymfonyComponentDependencyInjectionContainerInterface;
use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpKernelTestsControllerControllerResolverTest as BaseControllerResolverTest;

class 
ControllerResolverTest extends BaseControllerResolverTest
{
    public function 
testGetControllerOnContainerAware()
    {
        
$resolver $this->createControllerResolver();
        
$request Request::create('/');
        
$request->attributes->set('_controller''SymfonyBundleFrameworkBundleTestsControllerContainerAwareController::testAction');

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

        
$this->assertInstanceOf('SymfonyComponentDependencyInjectionContainerInterface'$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('SymfonyBundleFrameworkBundleTestsControllerContainerAwareController::testAction'))
        ;

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

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

        
$this->assertInstanceOf('SymfonyBundleFrameworkBundleTestsControllerContainerAwareController'$controller[0]);
        
$this->assertInstanceOf('SymfonyComponentDependencyInjectionContainerInterface'$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(nullnull$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(nullnull$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(
                
'SymfonyComponentHttpKernelTestsControllerControllerResolverTest::bar',
                
'InvalidArgumentException',
                
'Controller "SymfonyComponentHttpKernelTestsControllerControllerResolverTest::bar" for URI "/" is not callable.',
            ),
        );
    }

    protected function 
createControllerResolver(LoggerInterface $logger nullControllerNameParser $parser nullContainerInterface $container null)
    {
        if (!
$parser) {
            
$parser $this->createMockParser();
        }

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

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

    protected function 
createMockParser()
    {
        return 
$this->getMock('SymfonyBundleFrameworkBundleControllerControllerNameParser', array(), array(), ''false);
    }

    protected function 
createMockContainer()
    {
        return 
$this->getMock('SymfonyComponentDependencyInjectionContainerInterface');
    }
}

class 
ContainerAwareController implements ContainerAwareInterface
{
    private 
$container;

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

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

    public function 
testAction()
    {
    }
}
Онлайн: 0
Реклама