TokenTest.php 4.6 KB

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