DaoAuthenticationProviderTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Provider;
  10. use Symfony\Component\Security\Authentication\Provider\DaoAuthenticationProvider;
  11. class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
  15. */
  16. public function testRetrieveUserWhenProviderDoesNotReturnAnAccountInterface()
  17. {
  18. $provider = $this->getProvider('fabien');
  19. $method = new \ReflectionMethod($provider, 'retrieveUser');
  20. $method->setAccessible(true);
  21. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  22. }
  23. /**
  24. * @expectedException Symfony\Component\Security\Exception\UsernameNotFoundException
  25. */
  26. public function testRetrieveUserWhenUsernameIsNotFound()
  27. {
  28. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  29. $userProvider->expects($this->once())
  30. ->method('loadUserByUsername')
  31. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\UsernameNotFoundException', null, array(), '', false)))
  32. ;
  33. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
  34. $method = new \ReflectionMethod($provider, 'retrieveUser');
  35. $method->setAccessible(true);
  36. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  37. }
  38. /**
  39. * @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
  40. */
  41. public function testRetrieveUserWhenAnExceptionOccurs()
  42. {
  43. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  44. $userProvider->expects($this->once())
  45. ->method('loadUserByUsername')
  46. ->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
  47. ;
  48. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
  49. $method = new \ReflectionMethod($provider, 'retrieveUser');
  50. $method->setAccessible(true);
  51. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  52. }
  53. public function testRetrieveUser()
  54. {
  55. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  56. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  57. $userProvider->expects($this->once())
  58. ->method('loadUserByUsername')
  59. ->will($this->returnValue($user))
  60. ;
  61. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
  62. $method = new \ReflectionMethod($provider, 'retrieveUser');
  63. $method->setAccessible(true);
  64. $this->assertSame($user, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
  65. }
  66. /**
  67. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  68. */
  69. public function testCheckAuthenticationWhenCredentialsAreEmpty()
  70. {
  71. $provider = $this->getProvider();
  72. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  73. $method->setAccessible(true);
  74. $token = $this->getSupportedToken();
  75. $token->expects($this->once())
  76. ->method('getCredentials')
  77. ->will($this->returnValue(''))
  78. ;
  79. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  80. }
  81. /**
  82. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  83. */
  84. public function testCheckAuthenticationWhenCredentialsAreNotValid()
  85. {
  86. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  87. $encoder->expects($this->once())
  88. ->method('isPasswordValid')
  89. ->will($this->returnValue(false))
  90. ;
  91. $provider = $this->getProvider(false, false, $encoder);
  92. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  93. $method->setAccessible(true);
  94. $token = $this->getSupportedToken();
  95. $token->expects($this->once())
  96. ->method('getCredentials')
  97. ->will($this->returnValue('foo'))
  98. ;
  99. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  100. }
  101. public function testCheckAuthentication()
  102. {
  103. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  104. $encoder->expects($this->once())
  105. ->method('isPasswordValid')
  106. ->will($this->returnValue(true))
  107. ;
  108. $provider = $this->getProvider(false, false, $encoder);
  109. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  110. $method->setAccessible(true);
  111. $token = $this->getSupportedToken();
  112. $token->expects($this->once())
  113. ->method('getCredentials')
  114. ->will($this->returnValue('foo'))
  115. ;
  116. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  117. }
  118. protected function getSupportedToken()
  119. {
  120. return $this->getMock('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', array('getCredentials'), array(), '', false);
  121. }
  122. protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
  123. {
  124. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  125. if (false !== $user) {
  126. $userProvider->expects($this->once())
  127. ->method('loadUserByUsername')
  128. ->will($this->returnValue($user))
  129. ;
  130. }
  131. if (false === $userChecker) {
  132. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  133. }
  134. return new DaoAuthenticationProvider($userProvider, $userChecker, $passwordEncoder);
  135. }
  136. }