Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php
Строк: 604
<?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 SymfonyComponentSecurityHttpTestsFirewall;

use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpFoundationResponse;
use 
SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentSecurityCoreAuthenticationAuthenticationTrustResolverInterface;
use 
SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
use 
SymfonyComponentSecurityCoreExceptionAccessDeniedException;
use 
SymfonyComponentSecurityCoreExceptionAuthenticationException;
use 
SymfonyComponentSecurityHttpAuthorizationAccessDeniedHandlerInterface;
use 
SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface;
use 
SymfonyComponentSecurityHttpFirewallExceptionListener;
use 
SymfonyComponentSecurityHttpHttpUtils;

class 
ExceptionListenerTest extends PHPUnit_Framework_TestCase
{
    
/**
     * @dataProvider getAuthenticationExceptionProvider
     */
    
public function testAuthenticationExceptionWithoutEntryPoint(Exception $exceptionException $eventException null)
    {
        
$event $this->createEvent($exception);

        
$listener $this->createExceptionListener();
        
$listener->onKernelException($event);

        
$this->assertNull($event->getResponse());
        
$this->assertSame(null === $eventException $exception $eventException$event->getException());
    }

    
/**
     * @dataProvider getAuthenticationExceptionProvider
     */
    
public function testAuthenticationExceptionWithEntryPoint(Exception $exceptionException $eventException null)
    {
        
$event $this->createEvent($exception = new AuthenticationException());

        
$listener $this->createExceptionListener(nullnullnull$this->createEntryPoint());
        
$listener->onKernelException($event);

        
$this->assertEquals('OK'$event->getResponse()->getContent());
        
$this->assertSame($exception$event->getException());
    }

    public function 
getAuthenticationExceptionProvider()
    {
        return array(
            array(new 
AuthenticationException()),
            array(new 
LogicException('random'0$e = new AuthenticationException()), $e),
            array(new 
LogicException('random'0$e = new AuthenticationException('embed'0, new AuthenticationException())), $e),
            array(new 
LogicException('random'0$e = new AuthenticationException('embed'0, new AccessDeniedException())), $e),
            array(new 
AuthenticationException('random'0, new LogicException())),
        );
    }

    
/**
     * @dataProvider getAccessDeniedExceptionProvider
     */
    
public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandlerAndWithoutErrorPage(Exception $exceptionException $eventException null)
    {
        
$event $this->createEvent($exception);

        
$listener $this->createExceptionListener(null$this->createTrustResolver(true));
        
$listener->onKernelException($event);

        
$this->assertNull($event->getResponse());
        
$this->assertSame(null === $eventException $exception $eventException$event->getException()->getPrevious());
    }

    
/**
     * @dataProvider getAccessDeniedExceptionProvider
     */
    
public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage(Exception $exceptionException $eventException null)
    {
        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$kernel->expects($this->once())->method('handle')->will($this->returnValue(new Response('error')));

        
$event $this->createEvent($exception$kernel);

        
$httpUtils $this->getMock('SymfonyComponentSecurityHttpHttpUtils');
        
$httpUtils->expects($this->once())->method('createRequest')->will($this->returnValue(Request::create('/error')));

        
$listener $this->createExceptionListener(null$this->createTrustResolver(true), $httpUtilsnull'/error');
        
$listener->onKernelException($event);

        
$this->assertEquals('error'$event->getResponse()->getContent());
        
$this->assertSame(null === $eventException $exception $eventException$event->getException()->getPrevious());
    }

    
/**
     * @dataProvider getAccessDeniedExceptionProvider
     */
    
public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage(Exception $exceptionException $eventException null)
    {
        
$event $this->createEvent($exception);

        
$accessDeniedHandler $this->getMock('SymfonyComponentSecurityHttpAuthorizationAccessDeniedHandlerInterface');
        
$accessDeniedHandler->expects($this->once())->method('handle')->will($this->returnValue(new Response('error')));

        
$listener $this->createExceptionListener(null$this->createTrustResolver(true), nullnullnull$accessDeniedHandler);
        
$listener->onKernelException($event);

        
$this->assertEquals('error'$event->getResponse()->getContent());
        
$this->assertSame(null === $eventException $exception $eventException$event->getException()->getPrevious());
    }

    
/**
     * @dataProvider getAccessDeniedExceptionProvider
     */
    
public function testAccessDeniedExceptionNotFullFledged(Exception $exceptionException $eventException null)
    {
        
$event $this->createEvent($exception);

        
$tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
        
$tokenStorage->expects($this->once())->method('getToken')->will($this->returnValue($this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface')));

        
$listener $this->createExceptionListener($tokenStorage$this->createTrustResolver(false), null$this->createEntryPoint());
        
$listener->onKernelException($event);

        
$this->assertEquals('OK'$event->getResponse()->getContent());
        
$this->assertSame(null === $eventException $exception $eventException$event->getException()->getPrevious());
    }

    public function 
getAccessDeniedExceptionProvider()
    {
        return array(
            array(new 
AccessDeniedException()),
            array(new 
LogicException('random'0$e = new AccessDeniedException()), $e),
            array(new 
LogicException('random'0$e = new AccessDeniedException('embed', new AccessDeniedException())), $e),
            array(new 
LogicException('random'0$e = new AccessDeniedException('embed', new AuthenticationException())), $e),
            array(new 
AccessDeniedException('random', new LogicException())),
        );
    }

    private function 
createEntryPoint()
    {
        
$entryPoint $this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface');
        
$entryPoint->expects($this->once())->method('start')->will($this->returnValue(new Response('OK')));

        return 
$entryPoint;
    }

    private function 
createTrustResolver($fullFledged)
    {
        
$trustResolver $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationTrustResolverInterface');
        
$trustResolver->expects($this->once())->method('isFullFledged')->will($this->returnValue($fullFledged));

        return 
$trustResolver;
    }

    private function 
createEvent(Exception $exception$kernel null)
    {
        if (
null === $kernel) {
            
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        }

        return new 
GetResponseForExceptionEvent($kernelRequest::create('/'), HttpKernelInterface::MASTER_REQUEST$exception);
    }

    private function 
createExceptionListener(TokenStorageInterface $tokenStorage nullAuthenticationTrustResolverInterface $trustResolver nullHttpUtils $httpUtils nullAuthenticationEntryPointInterface $authenticationEntryPoint null$errorPage nullAccessDeniedHandlerInterface $accessDeniedHandler null)
    {
        return new 
ExceptionListener(
            
$tokenStorage $tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface'),
            
$trustResolver $trustResolver $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationTrustResolverInterface'),
            
$httpUtils $httpUtils $this->getMock('SymfonyComponentSecurityHttpHttpUtils'),
            
'key',
            
$authenticationEntryPoint,
            
$errorPage,
            
$accessDeniedHandler
        
);
    }
}
Онлайн: 1
Реклама