[Security] added some missing unit tests

This commit is contained in:
Fabien Potencier 2010-10-31 23:41:36 +01:00
parent 58bd4acdd1
commit a19cdce1bc
3 changed files with 56 additions and 1 deletions

View File

@ -71,8 +71,10 @@ abstract class Token implements TokenInterface
{
if (!is_object($this->user)) {
return (string) $this->user;
} else {
} elseif ($this->user instanceof AccountInterface) {
return $this->user->getUsername();
} else {
return 'n/a';
}
}

View File

@ -15,10 +15,57 @@ use Symfony\Component\Security\Role\Role;
class Token extends BaseToken
{
public function setUser($user)
{
$this->user = $user;
}
public function setCredentials($credentials)
{
$this->credentials = $credentials;
}
}
class TokenTest extends \PHPUnit_Framework_TestCase
{
public function testMagicToString()
{
$token = new Token(array('ROLE_FOO'));
$token->setUser('fabien');
$this->assertEquals('fabien', (string) $token);
$token->setUser(new \stdClass('fabien'));
$this->assertEquals('n/a', (string) $token);
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
$user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
$token->setUser($user);
$this->assertEquals('fabien', (string) $token);
}
public function testEraseCredentials()
{
$token = new Token(array('ROLE_FOO'));
$credentials = $this->getMock('Symfony\Component\Security\User\AccountInterface');
$credentials->expects($this->once())->method('eraseCredentials');
$token->setCredentials($credentials);
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
$user->expects($this->once())->method('eraseCredentials');
$token->setUser($user);
$token->eraseCredentials();
}
public function testSerialize()
{
$token = new Token(array('ROLE_FOO'));
$this->assertEquals($token, unserialize(serialize($token)));
}
/**
* @covers Symfony\Component\Security\Authentication\Token\Token::__construct
*/

View File

@ -16,6 +16,12 @@ use Symfony\Component\Security\Role\Role;
class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase
{
public function testSupportsClass()
{
$voter = new AuthenticatedVoter();
$this->assertTrue($voter->supportsClass('stdClass'));
}
/**
* @dataProvider getVoteTests
*/