* * 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:----dTl/uT0+664hTm4W1qQWq6R+aUXLIu0p3YKA0gi+Znelzo803wNZGoXjUjgcE2lXhfL8oNW1DJIwsZJRAxzy4wDCeQ2bqTksnb53WZZVJ3CWmOjoMc/zG5o27QF36QoDTVPqsZVa13UCnBRH5BlE53hJYyyhwErumm/dc7aGHRYFSTHDh+ooyqWZ5P03I56vo/FIpGJgBJnk7ho9+zG9NMS4cq5999CWPg4BfTuokEZCnPCiu77YWi7OH5Rh2T8uSHkHWcwSTIo/mGRsnTbPqxB1siyOb7XJWiHAV8PU2vpIk133vWmNjaoLwttv2oq4kGrcAd8572KifuH5PqUxNPtlyNezYLtPD5gL11z3IDd6S3grspmzW3vPkafdi/5sxICliNGOMNpRk0/etwRzueTrCflJKN8jrOWMvnbqKF2me9FZUZ0c/kpv4COfUVI10s5hx2SuDaytQBrcpe9sG8U9HhgorVCV6m/faMXm77Lhp7yk7GppGVRrnmlSGdSiiHgjcF+uy1hF4WBAxb6vQzOwppWUzoJ+UR2dl9EIErLXB6UfP9wjcgdW/aN2Bz7mYAJ5fr6QmFUFBHQkJBsRjPvcj9n/7p2SQgZjthS1bnyoCTVBhSVL/YF5klnt/egQcMd9YTt8478fn0nPmu9y0Ka3t1w+sQNNFiMN+97S/64=----ATTACHMENT:----OTQ0NDM4OTg2MDkyMjY5NiA2OTg3NDMwNjg4Njk2MTQzIDgyMTkwMzgzMjQ0Mjg0OQ==