AuthenticationProviderManagerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Security\Authentication;
  10. use Symfony\Component\Security\Authentication\AuthenticationProviderManager;
  11. use Symfony\Component\Security\Exception\ProviderNotFoundException;
  12. use Symfony\Component\Security\Exception\AuthenticationException;
  13. use Symfony\Component\Security\Exception\AccountStatusException;
  14. use Symfony\Component\Security\Authentication\Token\UsernamePasswordToken;
  15. class AuthenticationProviderManagerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testProviderAccessors()
  18. {
  19. $manager = new AuthenticationProviderManager();
  20. $manager->add($provider = $this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface'));
  21. $this->assertSame(array($provider), $manager->all());
  22. $manager->setProviders($providers = array($this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface')));
  23. $this->assertSame($providers, $manager->all());
  24. }
  25. /**
  26. * @expectedException LogicException
  27. */
  28. public function testAuthenticateWithoutProviders()
  29. {
  30. $manager = new AuthenticationProviderManager();
  31. $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  32. }
  33. public function testAuthenticateWhenNoProviderSupportsToken()
  34. {
  35. $manager = new AuthenticationProviderManager(array(
  36. $this->getAuthenticationProvider(false),
  37. ));
  38. try {
  39. $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  40. $this->fail();
  41. } catch (ProviderNotFoundException $e) {
  42. $this->assertSame($token, $e->getExtraInformation());
  43. }
  44. }
  45. public function testAuthenticateWhenProviderReturnsAccountStatusException()
  46. {
  47. $manager = new AuthenticationProviderManager(array(
  48. $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AccountStatusException'),
  49. ));
  50. try {
  51. $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  52. $this->fail();
  53. } catch (AccountStatusException $e) {
  54. $this->assertSame($token, $e->getExtraInformation());
  55. }
  56. }
  57. public function testAuthenticateWhenProviderReturnsAuthenticationException()
  58. {
  59. $manager = new AuthenticationProviderManager(array(
  60. $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AuthenticationException'),
  61. ));
  62. try {
  63. $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  64. $this->fail();
  65. } catch (AuthenticationException $e) {
  66. $this->assertSame($token, $e->getExtraInformation());
  67. }
  68. }
  69. public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll()
  70. {
  71. $manager = new AuthenticationProviderManager(array(
  72. $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AuthenticationException'),
  73. $this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
  74. ));
  75. $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  76. $this->assertSame($expected, $token);
  77. }
  78. public function testAuthenticateReturnsTokenForTheLastMatch()
  79. {
  80. $manager = new AuthenticationProviderManager(array(
  81. $this->getAuthenticationProvider(true, $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
  82. $this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
  83. ));
  84. $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  85. $this->assertSame($expected, $token);
  86. }
  87. public function testEraseCredentialFlag()
  88. {
  89. $manager = new AuthenticationProviderManager(array(
  90. $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar')),
  91. ));
  92. $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  93. $this->assertEquals('', $token->getCredentials());
  94. $manager = new AuthenticationProviderManager(array(
  95. $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar')),
  96. ), false);
  97. $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  98. $this->assertEquals('bar', $token->getCredentials());
  99. }
  100. protected function getAuthenticationProvider($supports, $token = null, $exception = null)
  101. {
  102. $provider = $this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface');
  103. $provider->expects($this->once())
  104. ->method('supports')
  105. ->will($this->returnValue($supports))
  106. ;
  107. if (null !== $token) {
  108. $provider->expects($this->once())
  109. ->method('authenticate')
  110. ->will($this->returnValue($token))
  111. ;
  112. } elseif (null !== $exception) {
  113. $provider->expects($this->once())
  114. ->method('authenticate')
  115. ->will($this->throwException($this->getMock($exception, null, array(), '', false)))
  116. ;
  117. }
  118. return $provider;
  119. }
  120. }