SecurityContextTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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;
  10. use Symfony\Component\Security\SecurityContext;
  11. class SecurityContextTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testIsAuthenticated()
  14. {
  15. $context = new SecurityContext();
  16. $this->assertFalse($context->isAuthenticated());
  17. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  18. $token->expects($this->once())->method('isAuthenticated')->will($this->returnValue(false));
  19. $context->setToken($token);
  20. $this->assertFalse($context->isAuthenticated());
  21. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  22. $token->expects($this->once())->method('isAuthenticated')->will($this->returnValue(true));
  23. $context->setToken($token);
  24. $this->assertTrue($context->isAuthenticated());
  25. }
  26. public function testGetUser()
  27. {
  28. $context = new SecurityContext();
  29. $this->assertNull($context->getUser());
  30. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  31. $token->expects($this->once())->method('getUser')->will($this->returnValue('foo'));
  32. $context->setToken($token);
  33. $this->assertEquals('foo', $context->getUser());
  34. }
  35. public function testVote()
  36. {
  37. $context = new SecurityContext();
  38. $this->assertFalse($context->vote('ROLE_FOO'));
  39. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  40. $this->assertFalse($context->vote('ROLE_FOO'));
  41. $manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManager');
  42. $manager->expects($this->once())->method('decide')->will($this->returnValue(false));
  43. $context = new SecurityContext($manager);
  44. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  45. $this->assertFalse($context->vote('ROLE_FOO'));
  46. $manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManager');
  47. $manager->expects($this->once())->method('decide')->will($this->returnValue(true));
  48. $context = new SecurityContext($manager);
  49. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  50. $this->assertTrue($context->vote('ROLE_FOO'));
  51. }
  52. public function testGetSetToken()
  53. {
  54. $context = new SecurityContext();
  55. $this->assertNull($context->getToken());
  56. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  57. $this->assertSame($token, $context->getToken());
  58. }
  59. }