Файл: symfony-2.7/src/Symfony/Component/Security/Core/Tests/Authorization/AuthorizationCheckerTest.php
Строк: 192
<?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 SymfonyComponentSecurityCoreTestsAuthorization;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationChecker;
class AuthorizationCheckerTest extends PHPUnit_Framework_TestCase
{
private $authenticationManager;
private $accessDecisionManager;
private $authorizationChecker;
private $tokenStorage;
public function setUp()
{
$this->authenticationManager = $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface');
$this->accessDecisionManager = $this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface');
$this->tokenStorage = new TokenStorage();
$this->authorizationChecker = new AuthorizationChecker(
$this->tokenStorage,
$this->authenticationManager,
$this->accessDecisionManager
);
}
public function testVoteAuthenticatesTokenIfNecessary()
{
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$this->tokenStorage->setToken($token);
$newToken = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$this->authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->equalTo($token))
->will($this->returnValue($newToken));
// default with() isn't a strict check
$tokenComparison = function ($value) use ($newToken) {
// make sure that the new token is used in "decide()" and not the old one
return $value === $newToken;
};
$this->accessDecisionManager
->expects($this->once())
->method('decide')
->with($this->callback($tokenComparison))
->will($this->returnValue(true));
// first run the token has not been re-authenticated yet, after isGranted is called, it should be equal
$this->assertFalse($newToken === $this->tokenStorage->getToken());
$this->assertTrue($this->authorizationChecker->isGranted('foo'));
$this->assertTrue($newToken === $this->tokenStorage->getToken());
}
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException
*/
public function testVoteWithoutAuthenticationToken()
{
$this->authorizationChecker->isGranted('ROLE_FOO');
}
/**
* @dataProvider isGrantedProvider
*/
public function testIsGranted($decide)
{
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$token
->expects($this->once())
->method('isAuthenticated')
->will($this->returnValue(true));
$this->accessDecisionManager
->expects($this->once())
->method('decide')
->will($this->returnValue($decide));
$this->tokenStorage->setToken($token);
$this->assertTrue($decide === $this->authorizationChecker->isGranted('ROLE_FOO'));
}
public function isGrantedProvider()
{
return array(array(true), array(false));
}
}