SecurityContextTest.php 3.0 KB

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