|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
|
| 16 | +use Symfony\Component\Security\Core\AuthenticationEvents; |
| 17 | +use Symfony\Component\Security\Core\Event\AuthenticationEvent; |
| 18 | +use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; |
16 | 19 | use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
|
17 | 20 | use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
18 | 21 | use Symfony\Component\Security\Core\Exception\AccountStatusException;
|
@@ -125,6 +128,50 @@ public function testEraseCredentialFlag()
|
125 | 128 | $this->assertEquals('bar', $token->getCredentials());
|
126 | 129 | }
|
127 | 130 |
|
| 131 | + public function testAuthenticateDispatchesAuthenticationFailureEvent() |
| 132 | + { |
| 133 | + $token = new UsernamePasswordToken('foo', 'bar', 'key'); |
| 134 | + $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); |
| 135 | + $provider->expects($this->once())->method('supports')->willReturn(true); |
| 136 | + $provider->expects($this->once())->method('authenticate')->willThrowException($exception = new AuthenticationException()); |
| 137 | + |
| 138 | + $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); |
| 139 | + $dispatcher |
| 140 | + ->expects($this->once()) |
| 141 | + ->method('dispatch') |
| 142 | + ->with(AuthenticationEvents::AUTHENTICATION_FAILURE, $this->equalTo(new AuthenticationFailureEvent($token, $exception))); |
| 143 | + |
| 144 | + $manager = new AuthenticationProviderManager(array($provider)); |
| 145 | + $manager->setEventDispatcher($dispatcher); |
| 146 | + |
| 147 | + try { |
| 148 | + $manager->authenticate($token); |
| 149 | + $this->fail('->authenticate() should rethrow exceptions'); |
| 150 | + } catch (AuthenticationException $e) { |
| 151 | + $this->assertSame($token, $exception->getToken()); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public function testAuthenticateDispatchesAuthenticationSuccessEvent() |
| 156 | + { |
| 157 | + $token = new UsernamePasswordToken('foo', 'bar', 'key'); |
| 158 | + |
| 159 | + $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); |
| 160 | + $provider->expects($this->once())->method('supports')->willReturn(true); |
| 161 | + $provider->expects($this->once())->method('authenticate')->willReturn($token); |
| 162 | + |
| 163 | + $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); |
| 164 | + $dispatcher |
| 165 | + ->expects($this->once()) |
| 166 | + ->method('dispatch') |
| 167 | + ->with(AuthenticationEvents::AUTHENTICATION_SUCCESS, $this->equalTo(new AuthenticationEvent($token))); |
| 168 | + |
| 169 | + $manager = new AuthenticationProviderManager(array($provider)); |
| 170 | + $manager->setEventDispatcher($dispatcher); |
| 171 | + |
| 172 | + $this->assertSame($token, $manager->authenticate($token)); |
| 173 | + } |
| 174 | + |
128 | 175 | protected function getAuthenticationProvider($supports, $token = null, $exception = null)
|
129 | 176 | {
|
130 | 177 | $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();
|
|
0 commit comments