RememerMeTokenTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Authentication\Token;
  3. use Symfony\Component\Security\Authentication\Token\RememberMeToken;
  4. use Symfony\Component\Security\Role\Role;
  5. class RememberMeTokenTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testConstructor()
  8. {
  9. $user = $this->getUser();
  10. $token = new RememberMeToken($user, 'fookey', 'foo');
  11. $this->assertEquals('fookey', $token->getProviderKey());
  12. $this->assertEquals('foo', $token->getKey());
  13. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  14. $this->assertSame($user, $token->getUser());
  15. $this->assertTrue($token->isAuthenticated());
  16. }
  17. /**
  18. * @expectedException \InvalidArgumentException
  19. */
  20. public function testConstructorKeyCannotBeNull()
  21. {
  22. new RememberMeToken(
  23. $this->getUser(),
  24. null,
  25. null
  26. );
  27. }
  28. /**
  29. * @expectedException \InvalidArgumentException
  30. */
  31. public function testConstructorKeyCannotBeEmptyString()
  32. {
  33. new RememberMeToken(
  34. $this->getUser(),
  35. '',
  36. ''
  37. );
  38. }
  39. /**
  40. * @expectedException PHPUnit_Framework_Error
  41. * @dataProvider getUserArguments
  42. */
  43. public function testConstructorUserCannotBeNull($user)
  44. {
  45. new RememberMeToken($user, 'foo', 'foo');
  46. }
  47. public function getUserArguments()
  48. {
  49. return array(
  50. array(null),
  51. array('foo'),
  52. );
  53. }
  54. public function testPersistentToken()
  55. {
  56. $token = new RememberMeToken($this->getUser(), 'fookey', 'foo');
  57. $persistentToken = $this->getMock('Symfony\Component\Security\Authentication\RememberMe\PersistentTokenInterface');
  58. $this->assertNull($token->getPersistentToken());
  59. $token->setPersistentToken($persistentToken);
  60. $this->assertSame($persistentToken, $token->getPersistentToken());
  61. }
  62. protected function getUser($roles = array('ROLE_FOO'))
  63. {
  64. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  65. $user
  66. ->expects($this->once())
  67. ->method('getRoles')
  68. ->will($this->returnValue($roles))
  69. ;
  70. return $user;
  71. }
  72. }