Вход Регистрация
Файл: symfony-2.7/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php
Строк: 279
<?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 
SymfonyBundleFrameworkBundleTestsTestCase;
use 
SymfonyBundleFrameworkBundleControllerController;
use 
SymfonyComponentDependencyInjectionContainerInterface;
use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpFoundationRequestStack;
use 
SymfonyComponentHttpFoundationResponse;
use 
SymfonyComponentSecurityCoreAuthenticationTokenAnonymousToken;
use 
SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
use 
SymfonyComponentSecurityCoreUserUser;

class 
ControllerTest extends TestCase
{
    public function 
testForward()
    {
        
$request Request::create('/');
        
$request->setLocale('fr');
        
$request->setRequestFormat('xml');

        
$requestStack = new RequestStack();
        
$requestStack->push($request);

        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
            return new 
Response($request->getRequestFormat().'--'.$request->getLocale());
        }));

        
$container $this->getMock('SymfonyComponentDependencyInjectionContainerInterface');
        
$container->expects($this->at(0))->method('get')->will($this->returnValue($requestStack));
        
$container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));

        
$controller = new TestController();
        
$controller->setContainer($container);

        
$response $controller->forward('a_controller');
        
$this->assertEquals('xml--fr'$response->getContent());
    }

    public function 
testGetUser()
    {
        
$user = new User('user''pass');
        
$token = new UsernamePasswordToken($user'pass''default', array('ROLE_USER'));

        
$controller = new TestController();
        
$controller->setContainer($this->getContainerWithTokenStorage($token));

        
$this->assertSame($controller->getUser(), $user);
    }

    public function 
testGetUserAnonymousUserConvertedToNull()
    {
        
$token = new AnonymousToken('default''anon.');

        
$controller = new TestController();
        
$controller->setContainer($this->getContainerWithTokenStorage($token));

        
$this->assertNull($controller->getUser());
    }

    public function 
testGetUserWithEmptyTokenStorage()
    {
        
$controller = new TestController();
        
$controller->setContainer($this->getContainerWithTokenStorage(null));

        
$this->assertNull($controller->getUser());
    }

    
/**
     * @expectedException LogicException
     * @expectedExceptionMessage The SecurityBundle is not registered in your application.
     */
    
public function testGetUserWithEmptyContainer()
    {
        
$container $this->getMock('SymfonyComponentDependencyInjectionContainerInterface');
        
$container
            
->expects($this->once())
            ->
method('has')
            ->
with('security.token_storage')
            ->
will($this->returnValue(false));

        
$controller = new TestController();
        
$controller->setContainer($container);

        
$controller->getUser();
    }

    
/**
     * @param $token
     * @return ContainerInterface
     */
    
private function getContainerWithTokenStorage($token null)
    {
        
$tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage');
        
$tokenStorage
            
->expects($this->once())
            ->
method('getToken')
            ->
will($this->returnValue($token));

        
$container $this->getMock('SymfonyComponentDependencyInjectionContainerInterface');
        
$container
            
->expects($this->once())
            ->
method('has')
            ->
with('security.token_storage')
            ->
will($this->returnValue(true));

        
$container
            
->expects($this->once())
            ->
method('get')
            ->
with('security.token_storage')
            ->
will($this->returnValue($tokenStorage));

        return 
$container;
    }
}

class 
TestController extends Controller
{
    public function 
forward($controller, array $path = array(), array $query = array())
    {
        return 
parent::forward($controller$path$query);
    }

    public function 
getUser()
    {
        return 
parent::getUser();
    }
}
Онлайн: 0
Реклама