AnonymousAuthenticationProviderTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Provider;
  11. use Symfony\Component\Security\Authentication\Provider\AnonymousAuthenticationProvider;
  12. class AnonymousAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testSupports()
  15. {
  16. $provider = $this->getProvider('foo');
  17. $this->assertTrue($provider->supports($this->getSupportedToken('foo')));
  18. $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  19. }
  20. public function testAuthenticateWhenTokenIsNotSupported()
  21. {
  22. $provider = $this->getProvider('foo');
  23. $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  24. }
  25. /**
  26. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  27. */
  28. public function testAuthenticateWhenKeyIsNotValid()
  29. {
  30. $provider = $this->getProvider('foo');
  31. $this->assertNull($provider->authenticate($this->getSupportedToken('bar')));
  32. }
  33. public function testAuthenticate()
  34. {
  35. $provider = $this->getProvider('foo');
  36. $token = $this->getSupportedToken('foo');
  37. $this->assertSame($token, $provider->authenticate($token));
  38. }
  39. protected function getSupportedToken($key)
  40. {
  41. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\AnonymousToken', array('getKey'), array(), '', false);
  42. $token->expects($this->any())
  43. ->method('getKey')
  44. ->will($this->returnValue($key))
  45. ;
  46. return $token;
  47. }
  48. protected function getProvider($key)
  49. {
  50. return new AnonymousAuthenticationProvider($key);
  51. }
  52. }