AccountCheckerTest.php 3.8 KB

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