AccountCheckerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\User;
  10. use Symfony\Component\Security\User\AccountChecker;
  11. class AccountCheckerTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testCheckPreAuthNotAdvancedAccountInterface()
  14. {
  15. $checker = new AccountChecker();
  16. $this->assertNull($checker->checkPreAuth($this->getMock('Symfony\Component\Security\User\AccountInterface')));
  17. }
  18. public function testCheckPreAuthPass()
  19. {
  20. $checker = new AccountChecker();
  21. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  22. $account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(true));
  23. $this->assertNull($checker->checkPreAuth($account));
  24. }
  25. /**
  26. * @expectedException Symfony\Component\Security\Exception\CredentialsExpiredException
  27. */
  28. public function testCheckPreAuthCredentialsExpired()
  29. {
  30. $checker = new AccountChecker();
  31. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  32. $account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false));
  33. $checker->checkPreAuth($account);
  34. }
  35. public function testCheckPostAuthNotAdvancedAccountInterface()
  36. {
  37. $checker = new AccountChecker();
  38. $this->assertNull($checker->checkPostAuth($this->getMock('Symfony\Component\Security\User\AccountInterface')));
  39. }
  40. public function testCheckPostAuthPass()
  41. {
  42. $checker = new AccountChecker();
  43. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  44. $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
  45. $account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
  46. $account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(true));
  47. $this->assertNull($checker->checkPostAuth($account));
  48. }
  49. /**
  50. * @expectedException Symfony\Component\Security\Exception\LockedException
  51. */
  52. public function testCheckPostAuthAccountLocked()
  53. {
  54. $checker = new AccountChecker();
  55. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  56. $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(false));
  57. $checker->checkPostAuth($account);
  58. }
  59. /**
  60. * @expectedException Symfony\Component\Security\Exception\DisabledException
  61. */
  62. public function testCheckPostAuthDisabled()
  63. {
  64. $checker = new AccountChecker();
  65. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  66. $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
  67. $account->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
  68. $checker->checkPostAuth($account);
  69. }
  70. /**
  71. * @expectedException Symfony\Component\Security\Exception\AccountExpiredException
  72. */
  73. public function testCheckPostAuthAccountExpired()
  74. {
  75. $checker = new AccountChecker();
  76. $account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
  77. $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
  78. $account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
  79. $account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(false));
  80. $checker->checkPostAuth($account);
  81. }
  82. }