TokenTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Security\Authentication\Token;
  10. use Symfony\Component\Security\Authentication\Token\Token as BaseToken;
  11. use Symfony\Component\Security\Role\Role;
  12. class Token extends BaseToken
  13. {
  14. public function setCredentials($credentials)
  15. {
  16. $this->credentials = $credentials;
  17. }
  18. }
  19. class TestUser
  20. {
  21. protected $name;
  22. public function __construct($name)
  23. {
  24. $this->name = $name;
  25. }
  26. public function __toString()
  27. {
  28. return $this->name;
  29. }
  30. }
  31. class TokenTest extends \PHPUnit_Framework_TestCase
  32. {
  33. public function testMagicToString()
  34. {
  35. $token = new Token(array('ROLE_FOO'));
  36. $token->setUser('fabien');
  37. $this->assertEquals('fabien', (string) $token);
  38. $token->setUser(new TestUser('fabien'));
  39. $this->assertEquals('n/a', (string) $token);
  40. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  41. $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
  42. $token->setUser($user);
  43. $this->assertEquals('fabien', (string) $token);
  44. }
  45. public function testEraseCredentials()
  46. {
  47. $token = new Token(array('ROLE_FOO'));
  48. $credentials = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  49. $credentials->expects($this->once())->method('eraseCredentials');
  50. $token->setCredentials($credentials);
  51. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  52. $user->expects($this->once())->method('eraseCredentials');
  53. $token->setUser($user);
  54. $token->eraseCredentials();
  55. }
  56. public function testSerialize()
  57. {
  58. $token = new Token(array('ROLE_FOO'));
  59. $this->assertEquals($token, unserialize(serialize($token)));
  60. }
  61. /**
  62. * @covers Symfony\Component\Security\Authentication\Token\Token::__construct
  63. */
  64. public function testConstructor()
  65. {
  66. $token = new Token(array('ROLE_FOO'));
  67. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  68. $token = new Token(array(new Role('ROLE_FOO')));
  69. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  70. $token = new Token(array(new Role('ROLE_FOO'), 'ROLE_BAR'));
  71. $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_BAR')), $token->getRoles());
  72. }
  73. /**
  74. * @covers Symfony\Component\Security\Authentication\Token\Token::addRole
  75. * @covers Symfony\Component\Security\Authentication\Token\Token::getRoles
  76. */
  77. public function testAddRole()
  78. {
  79. $token = new Token();
  80. $token->addRole(new Role('ROLE_FOO'));
  81. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  82. $token->addRole(new Role('ROLE_BAR'));
  83. $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_BAR')), $token->getRoles());
  84. }
  85. /**
  86. * @covers Symfony\Component\Security\Authentication\Token\Token::isAuthenticated
  87. * @covers Symfony\Component\Security\Authentication\Token\Token::setAuthenticated
  88. */
  89. public function testAuthenticatedFlag()
  90. {
  91. $token = new Token();
  92. $this->assertFalse($token->isAuthenticated());
  93. $token->setAuthenticated(true);
  94. $this->assertTrue($token->isAuthenticated());
  95. $token->setAuthenticated(false);
  96. $this->assertFalse($token->isAuthenticated());
  97. }
  98. /**
  99. * @covers Symfony\Component\Security\Authentication\Token\Token::isImmutable
  100. * @covers Symfony\Component\Security\Authentication\Token\Token::setImmutable
  101. */
  102. public function testImmutableFlag()
  103. {
  104. $token = new Token();
  105. $this->assertFalse($token->isImmutable());
  106. $token->setImmutable();
  107. $this->assertTrue($token->isImmutable());
  108. }
  109. /**
  110. * @expectedException \LogicException
  111. * @dataProvider getImmutabilityTests
  112. */
  113. public function testImmutabilityIsEnforced($setter, $value)
  114. {
  115. $token = new Token();
  116. $token->setImmutable(true);
  117. $token->$setter($value);
  118. }
  119. public function getImmutabilityTests()
  120. {
  121. return array(
  122. array('setUser', 'foo'),
  123. array('eraseCredentials', null),
  124. array('setAuthenticated', true),
  125. array('setAuthenticated', false),
  126. array('addRole', new Role('foo')),
  127. array('setRoles', array('foo', 'asdf')),
  128. );
  129. }
  130. }