* * 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:----rc8Yi1JXiwJk0qRxk0W/d9ZteoFA14fPriWc1Xvh1aOisU/0bEZ2gIxET9su0rrMFufksAmL3Co5Hns+j8vMJ3yO4NggiC7sGX2DSkPt5/w0cakz6X1yVov1wyTUp0mXsIucFn1qroQL2rKBlX/oXCk7+1eTMPCBhk6DDaOlpE/B2KZlA1O8VHVDGsqOxucSh8KQFszVK8FPv4Y9ZghHLkmezgnmS/s3We6BTFCqitw8h56VT9TF4QMfDQVyhsUPDTxQELufz2Vq8e2NonMFU64PpOEHtfFM9Fc2by5VyEYs2erqv7Y0d5mtLMJby0LcbXwR8zOORBWoOeiNztGmuBhQruc5RoYUHI4l24JprxcHwgDLnSpPblKlIPzZ130DlKGHPZ9ABXyuC+8ohDysaYY5mv0YpgBihhk/1lHN0T7XRHGPB1YR030iXpkEPWKdl3TKAP4oHvv9f8+hQxcMnTmi7EPfax6HTONUb6ZNtPaUs9kbWMIjQVu7Qw6UgmoLbmGq33IR8WNWG4bHqGDEY4I/YVZiM8Q7la01W0p5h36HLlu5j9ZMgZuaGrL9ExGe7hUw58cDfAbST1JDH/RPUeYtTmon6p9Kov9/yyAu90q0Ge2Mkc6iyQD7KbUZY0KLgj/TnF3oNHzd5HGi3fKZuQghylt/YolJoyrFIjZ8jpE=----ATTACHMENT:----NjkyNTM5NTQzNDQ2MzM4IDQzMzA4MjU3NTI3NDkyNjYgMjIxNzAwODg0OTQ5NTUyOA==