* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\Tests\Authentication; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; use Symfony\Component\Security\Core\AuthenticationEvents; use Symfony\Component\Security\Core\Event\AuthenticationEvent; use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; use Symfony\Component\Security\Core\Exception\ProviderNotFoundException; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AccountStatusException; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; class AuthenticationProviderManagerTest extends TestCase { /** * @expectedException \InvalidArgumentException */ public function testAuthenticateWithoutProviders() { new AuthenticationProviderManager(array()); } /** * @expectedException \InvalidArgumentException */ public function testAuthenticateWithProvidersWithIncorrectInterface() { (new AuthenticationProviderManager(array( new \stdClass(), )))->authenticate($this->getMockBuilder(TokenInterface::class)->getMock()); } public function testAuthenticateWhenNoProviderSupportsToken() { $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(false), )); try { $manager->authenticate($token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->fail(); } catch (ProviderNotFoundException $e) { $this->assertSame($token, $e->getToken()); } } public function testAuthenticateWhenProviderReturnsAccountStatusException() { $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AccountStatusException'), )); try { $manager->authenticate($token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->fail(); } catch (AccountStatusException $e) { $this->assertSame($token, $e->getToken()); } } public function testAuthenticateWhenProviderReturnsAuthenticationException() { $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'), )); try { $manager->authenticate($token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->fail(); } catch (AuthenticationException $e) { $this->assertSame($token, $e->getToken()); } } public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll() { $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'), $this->getAuthenticationProvider(true, $expected = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()), )); $token = $manager->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->assertSame($expected, $token); } public function testAuthenticateReturnsTokenOfTheFirstMatchingProvider() { $second = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); $second ->expects($this->never()) ->method('supports') ; $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, $expected = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()), $second, )); $token = $manager->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->assertSame($expected, $token); } public function testEraseCredentialFlag() { $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')), )); $token = $manager->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->assertEquals('', $token->getCredentials()); $manager = new AuthenticationProviderManager(array( $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')), ), false); $token = $manager->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); $this->assertEquals('bar', $token->getCredentials()); } public function testAuthenticateDispatchesAuthenticationFailureEvent() { $token = new UsernamePasswordToken('foo', 'bar', 'key'); $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); $provider->expects($this->once())->method('supports')->willReturn(true); $provider->expects($this->once())->method('authenticate')->willThrowException($exception = new AuthenticationException()); $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $dispatcher ->expects($this->once()) ->method('dispatch') ->with(AuthenticationEvents::AUTHENTICATION_FAILURE, $this->equalTo(new AuthenticationFailureEvent($token, $exception))); $manager = new AuthenticationProviderManager(array($provider)); $manager->setEventDispatcher($dispatcher); try { $manager->authenticate($token); $this->fail('->authenticate() should rethrow exceptions'); } catch (AuthenticationException $e) { $this->assertSame($token, $exception->getToken()); } } public function testAuthenticateDispatchesAuthenticationSuccessEvent() { $token = new UsernamePasswordToken('foo', 'bar', 'key'); $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); $provider->expects($this->once())->method('supports')->willReturn(true); $provider->expects($this->once())->method('authenticate')->willReturn($token); $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $dispatcher ->expects($this->once()) ->method('dispatch') ->with(AuthenticationEvents::AUTHENTICATION_SUCCESS, $this->equalTo(new AuthenticationEvent($token))); $manager = new AuthenticationProviderManager(array($provider)); $manager->setEventDispatcher($dispatcher); $this->assertSame($token, $manager->authenticate($token)); } protected function getAuthenticationProvider($supports, $token = null, $exception = null) { $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); $provider->expects($this->once()) ->method('supports') ->will($this->returnValue($supports)) ; if (null !== $token) { $provider->expects($this->once()) ->method('authenticate') ->will($this->returnValue($token)) ; } elseif (null !== $exception) { $provider->expects($this->once()) ->method('authenticate') ->will($this->throwException($this->getMockBuilder($exception)->setMethods(null)->getMock())) ; } return $provider; } } __halt_compiler();----SIGNATURE:----j/909TkVoV3k6hP6U6Acv2KFHEEIoPABQU8ItQ+csP6NZkgYq3Lma9TV4JBC7O9026ul1bDI8DMhbIGJJB86FiGQsd6KAtJSUCN6jpRyOusXbVecR6s6OkxLG+jEmqKUxWsQ0UEWCWAPf82n7aQG5QUfWCJp/u1FFPX4ndA4nPz5yq7K2/dtaq8lIHZ75UM3BBPhPALaiTR1oI2944SLHmXWxSkz66FMCdZvDFq8xBp0qy7HsR62X2SnLYniYb+/FQbcs93VIsHsO+aLzRQSBMiSCwbPE0svGFhGzS5zfD3me4L5/v6u8sMUx8Y/lJucq2SQQF/m69GAlzw6QW+f5r9W8KLTILmoArMkI9UPDv4+ogEONdFxiU0rASuJE9qvJVeXtRfAVVJFaKaKdB5DJHgucG1Si8Q9GKJ94XvGBb2eKHx+IVsHCcM+lfhfVD71sOQfvIdLxgvRBezZAd/vty9kTCALCAoMMqzQH4vw+kW9ve7OkkJgwG8/zfjUhZHub6wpWaZH6BdchhpvjctDDEB+BJxS9f2aYBfqKKRrEj1txh47xvlgIteq5DLLZxsfeWNw3cnLmAtob2ouYONo1AqI+t5S6rbdXekD2r1dcXLv/fZZLfm6xqiFvv+rHAAC5gVe2lEf93tZA5CtcuynjWHeDIzY24ism3FKngMHfos=----ATTACHMENT:----OTU0NDU4MjM2MDE3OTQ5MyA5NjgxNjQ0ODYwNDY2OTM1IDE4MDA3NTg3NzUzNTc5NTg=