|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSUserBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\UserBundle\Tests\Security; |
| 13 | + |
| 14 | +use FOS\UserBundle\Security\UserChecker; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class UserCheckerTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @expectedException \Symfony\Component\Security\Core\Exception\LockedException |
| 21 | + * @expectedExceptionMessage User account is locked. |
| 22 | + */ |
| 23 | + public function testCheckPreAuthFailsLockedOut() |
| 24 | + { |
| 25 | + $userMock = $this->getUser(false, false, false, false); |
| 26 | + $checker = new UserChecker(); |
| 27 | + $checker->checkPreAuth($userMock); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @expectedException \Symfony\Component\Security\Core\Exception\DisabledException |
| 32 | + * @expectedExceptionMessage User account is disabled. |
| 33 | + */ |
| 34 | + public function testCheckPreAuthFailsIsEnabled() |
| 35 | + { |
| 36 | + $userMock = $this->getUser(true, false, false, false); |
| 37 | + $checker = new UserChecker(); |
| 38 | + $checker->checkPreAuth($userMock); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException |
| 43 | + * @expectedExceptionMessage User account has expired. |
| 44 | + */ |
| 45 | + public function testCheckPreAuthFailsIsAccountNonExpired() |
| 46 | + { |
| 47 | + $userMock = $this->getUser(true, true, false, false); |
| 48 | + $checker = new UserChecker(); |
| 49 | + $checker->checkPreAuth($userMock); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCheckPreAuthSuccess() |
| 53 | + { |
| 54 | + $userMock = $this->getUser(true, true, true, false); |
| 55 | + $checker = new UserChecker(); |
| 56 | + |
| 57 | + try { |
| 58 | + $this->assertNull($checker->checkPreAuth($userMock)); |
| 59 | + } catch (\Exception $ex) { |
| 60 | + $this->fail(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException |
| 66 | + * @expectedExceptionMessage User credentials have expired. |
| 67 | + */ |
| 68 | + public function testCheckPostAuthFailsIsCredentialsNonExpired() |
| 69 | + { |
| 70 | + $userMock = $this->getUser(true, true, true, false); |
| 71 | + $checker = new UserChecker(); |
| 72 | + $checker->checkPostAuth($userMock); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCheckPostAuthSuccess() |
| 76 | + { |
| 77 | + $userMock = $this->getUser(true, true, true, true); |
| 78 | + $checker = new UserChecker(); |
| 79 | + |
| 80 | + try { |
| 81 | + $this->assertNull($checker->checkPostAuth($userMock)); |
| 82 | + } catch (\Exception $ex) { |
| 83 | + $this->fail(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private function getUser($isAccountNonLocked, $isEnabled, $isAccountNonExpired, $isCredentialsNonExpired) |
| 88 | + { |
| 89 | + $userMock = $this->getMockBuilder('FOS\UserBundle\Model\User')->getMock(); |
| 90 | + $userMock |
| 91 | + ->method('isAccountNonLocked') |
| 92 | + ->willReturn($isAccountNonLocked); |
| 93 | + $userMock |
| 94 | + ->method('isEnabled') |
| 95 | + ->willReturn($isEnabled); |
| 96 | + $userMock |
| 97 | + ->method('isAccountNonExpired') |
| 98 | + ->willReturn($isAccountNonExpired); |
| 99 | + $userMock |
| 100 | + ->method('isCredentialsNonExpired') |
| 101 | + ->willReturn($isCredentialsNonExpired); |
| 102 | + |
| 103 | + return $userMock; |
| 104 | + } |
| 105 | +} |
0 commit comments