Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/AccessListenerTest.php
Строк: 389
<?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 SymfonyComponentSecurityHttpFirewallAccessListener;
class AccessListenerTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAccessDeniedException
*/
public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess()
{
$request = $this->getMock('SymfonyComponentHttpFoundationRequest', array(), array(), '', false, false);
$accessMap = $this->getMock('SymfonyComponentSecurityHttpAccessMapInterface');
$accessMap
->expects($this->any())
->method('getPatterns')
->with($this->equalTo($request))
->will($this->returnValue(array(array('foo' => 'bar'), null)))
;
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$token
->expects($this->any())
->method('isAuthenticated')
->will($this->returnValue(true))
;
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$accessDecisionManager = $this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface');
$accessDecisionManager
->expects($this->once())
->method('decide')
->with($this->equalTo($token), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
->will($this->returnValue(false))
;
$listener = new AccessListener(
$tokenStorage,
$accessDecisionManager,
$accessMap,
$this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
$listener->handle($event);
}
public function testHandleWhenTheTokenIsNotAuthenticated()
{
$request = $this->getMock('SymfonyComponentHttpFoundationRequest', array(), array(), '', false, false);
$accessMap = $this->getMock('SymfonyComponentSecurityHttpAccessMapInterface');
$accessMap
->expects($this->any())
->method('getPatterns')
->with($this->equalTo($request))
->will($this->returnValue(array(array('foo' => 'bar'), null)))
;
$notAuthenticatedToken = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$notAuthenticatedToken
->expects($this->any())
->method('isAuthenticated')
->will($this->returnValue(false))
;
$authenticatedToken = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$authenticatedToken
->expects($this->any())
->method('isAuthenticated')
->will($this->returnValue(true))
;
$authManager = $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface');
$authManager
->expects($this->once())
->method('authenticate')
->with($this->equalTo($notAuthenticatedToken))
->will($this->returnValue($authenticatedToken))
;
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($notAuthenticatedToken))
;
$tokenStorage
->expects($this->once())
->method('setToken')
->with($this->equalTo($authenticatedToken))
;
$accessDecisionManager = $this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface');
$accessDecisionManager
->expects($this->once())
->method('decide')
->with($this->equalTo($authenticatedToken), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
->will($this->returnValue(true))
;
$listener = new AccessListener(
$tokenStorage,
$accessDecisionManager,
$accessMap,
$authManager
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
$listener->handle($event);
}
public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
{
$request = $this->getMock('SymfonyComponentHttpFoundationRequest', array(), array(), '', false, false);
$accessMap = $this->getMock('SymfonyComponentSecurityHttpAccessMapInterface');
$accessMap
->expects($this->any())
->method('getPatterns')
->with($this->equalTo($request))
->will($this->returnValue(array(null, null)))
;
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$token
->expects($this->never())
->method('isAuthenticated')
;
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$listener = new AccessListener(
$tokenStorage,
$this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface'),
$accessMap,
$this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
$listener->handle($event);
}
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException
*/
public function testHandleWhenTheSecurityTokenStorageHasNoToken()
{
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;
$listener = new AccessListener(
$tokenStorage,
$this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface'),
$this->getMock('SymfonyComponentSecurityHttpAccessMapInterface'),
$this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$listener->handle($event);
}
}