|
15 | 15 | use Symfony\Component\Security\Core\Exception\DisabledException;
|
16 | 16 | use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
|
17 | 17 | use Symfony\Component\Security\Core\Exception\LockedException;
|
| 18 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
18 | 19 |
|
19 | 20 | class SimpleAuthenticationProviderTest extends TestCase
|
20 | 21 | {
|
@@ -72,6 +73,54 @@ public function testAuthenticateWhenPostChecksFails()
|
72 | 73 | $provider->authenticate($token);
|
73 | 74 | }
|
74 | 75 |
|
| 76 | + public function testAuthenticateFromString() |
| 77 | + { |
| 78 | + $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); |
| 79 | + |
| 80 | + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); |
| 81 | + $token->expects($this->any()) |
| 82 | + ->method('getUser') |
| 83 | + ->will($this->returnValue('foo')); |
| 84 | + |
| 85 | + $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); |
| 86 | + $authenticator->expects($this->once()) |
| 87 | + ->method('authenticateToken') |
| 88 | + ->will($this->returnValue($token)); |
| 89 | + |
| 90 | + $userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock(); |
| 91 | + $userProvider->expects($this->once()) |
| 92 | + ->method('loadUserByUsername') |
| 93 | + ->willReturn($this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock()); |
| 94 | + $provider = $this->getProvider($authenticator, $userProvider); |
| 95 | + |
| 96 | + $this->assertSame($token, $provider->authenticate($token)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
| 101 | + */ |
| 102 | + public function testUsernameNotFound() |
| 103 | + { |
| 104 | + $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); |
| 105 | + |
| 106 | + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); |
| 107 | + $token->expects($this->any()) |
| 108 | + ->method('getUser') |
| 109 | + ->will($this->returnValue('foo')); |
| 110 | + |
| 111 | + $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); |
| 112 | + $authenticator->expects($this->once()) |
| 113 | + ->method('authenticateToken') |
| 114 | + ->will($this->returnValue($token)); |
| 115 | + |
| 116 | + $userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock(); |
| 117 | + $userProvider->expects($this->once()) |
| 118 | + ->method('loadUserByUsername') |
| 119 | + ->willThrowException(new UsernameNotFoundException()); |
| 120 | + |
| 121 | + $this->getProvider($authenticator, $userProvider)->authenticate($token); |
| 122 | + } |
| 123 | + |
75 | 124 | protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
|
76 | 125 | {
|
77 | 126 | if (null === $userChecker) {
|
|
0 commit comments