RememberMeAuthenticationProviderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Authentication\Provider;
  3. use Symfony\Component\Security\Authentication\Provider\RememberMeAuthenticationProvider;
  4. use Symfony\Component\Security\Authentication\Token\RememberMeToken;
  5. use Symfony\Component\Security\Role\Role;
  6. class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testSupports()
  9. {
  10. $provider = $this->getProvider();
  11. $this->assertTrue($provider->supports($this->getSupportedToken()));
  12. $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  13. }
  14. public function testAuthenticateWhenTokenIsNotSupported()
  15. {
  16. $provider = $this->getProvider();
  17. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  18. $this->assertNull($provider->authenticate($token));
  19. }
  20. /**
  21. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  22. */
  23. public function testAuthenticateWhenKeysDoNotMatch()
  24. {
  25. $provider = $this->getProvider(null, 'key1');
  26. $token = $this->getSupportedToken(null, 'key2');
  27. $provider->authenticate($token);
  28. }
  29. /**
  30. * @expectedException Symfony\Component\Security\Exception\CredentialsExpiredException
  31. */
  32. public function testAuthenticateWhenPreChecksFails()
  33. {
  34. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  35. $userChecker->expects($this->once())
  36. ->method('checkPreAuth')
  37. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\CredentialsExpiredException', null, array(), '', false)))
  38. ;
  39. $provider = $this->getProvider($userChecker);
  40. $provider->authenticate($this->getSupportedToken());
  41. }
  42. /**
  43. * @expectedException Symfony\Component\Security\Exception\AccountExpiredException
  44. */
  45. public function testAuthenticateWhenPostChecksFails()
  46. {
  47. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  48. $userChecker->expects($this->once())
  49. ->method('checkPostAuth')
  50. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\AccountExpiredException', null, array(), '', false)))
  51. ;
  52. $provider = $this->getProvider($userChecker);
  53. $provider->authenticate($this->getSupportedToken());
  54. }
  55. public function testAuthenticate()
  56. {
  57. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  58. $user->expects($this->once())
  59. ->method('getRoles')
  60. ->will($this->returnValue(array('ROLE_FOO')))
  61. ;
  62. $provider = $this->getProvider();
  63. $token = $this->getSupportedToken($user);
  64. $token
  65. ->expects($this->once())
  66. ->method('getCredentials')
  67. ->will($this->returnValue('foo'))
  68. ;
  69. $authToken = $provider->authenticate($token);
  70. $this->assertInstanceOf('Symfony\Component\Security\Authentication\Token\RememberMeToken', $authToken);
  71. $this->assertSame($user, $authToken->getUser());
  72. $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
  73. $this->assertEquals('foo', $authToken->getCredentials());
  74. }
  75. protected function getSupportedToken($user = null, $key = 'test')
  76. {
  77. if (null === $user) {
  78. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  79. $user
  80. ->expects($this->any())
  81. ->method('getRoles')
  82. ->will($this->returnValue(array()))
  83. ;
  84. }
  85. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\RememberMeToken', array('getCredentials', 'getProviderKey'), array($user, 'foo', $key));
  86. $token
  87. ->expects($this->once())
  88. ->method('getProviderKey')
  89. ->will($this->returnValue('foo'))
  90. ;
  91. return $token;
  92. }
  93. protected function getProvider($userChecker = null, $key = 'test')
  94. {
  95. if (null === $userChecker) {
  96. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  97. }
  98. return new RememberMeAuthenticationProvider($userChecker, $key, 'foo');
  99. }
  100. }