AuthenticationProviderManagerTest.php 5.9 KB

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