AnonymousAuthenticationProviderTest.php 2.0 KB

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