SecurityContextTest.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Authentication\AuthenticationProviderManager;
  12. use Symfony\Component\Security\SecurityContext;
  13. class SecurityContextTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testGetUser()
  16. {
  17. $context = new SecurityContext($this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface'));
  18. $this->assertNull($context->getUser());
  19. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  20. $token->expects($this->once())->method('getUser')->will($this->returnValue('foo'));
  21. $context->setToken($token);
  22. $this->assertEquals('foo', $context->getUser());
  23. }
  24. public function testVoteAuthenticatesTokenIfNecessary()
  25. {
  26. $authManager = $this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface');
  27. $decisionManager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManagerInterface');
  28. $context = new SecurityContext($authManager, $decisionManager);
  29. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  30. $authManager
  31. ->expects($this->once())
  32. ->method('authenticate')
  33. ->with($this->equalTo($token))
  34. ->will($this->returnValue($newToken = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')))
  35. ;
  36. $decisionManager
  37. ->expects($this->once())
  38. ->method('decide')
  39. ->will($this->returnValue(true))
  40. ;
  41. $this->assertTrue($context->vote('foo'));
  42. $this->assertSame($newToken, $context->getToken());
  43. }
  44. public function testVote()
  45. {
  46. $context = new SecurityContext($this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface'));
  47. $this->assertFalse($context->vote('ROLE_FOO'));
  48. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  49. $this->assertFalse($context->vote('ROLE_FOO'));
  50. $manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManagerInterface');
  51. $manager->expects($this->once())->method('decide')->will($this->returnValue(false));
  52. $context = new SecurityContext($this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface'), $manager);
  53. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  54. $token
  55. ->expects($this->once())
  56. ->method('isAuthenticated')
  57. ->will($this->returnValue(true))
  58. ;
  59. $this->assertFalse($context->vote('ROLE_FOO'));
  60. $manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManagerInterface');
  61. $manager->expects($this->once())->method('decide')->will($this->returnValue(true));
  62. $context = new SecurityContext($this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface'), $manager);
  63. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  64. $token
  65. ->expects($this->once())
  66. ->method('isAuthenticated')
  67. ->will($this->returnValue(true))
  68. ;
  69. $this->assertTrue($context->vote('ROLE_FOO'));
  70. }
  71. public function testGetSetToken()
  72. {
  73. $context = new SecurityContext($this->getMock('Symfony\Component\Security\Authentication\AuthenticationManagerInterface'));
  74. $this->assertNull($context->getToken());
  75. $context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
  76. $this->assertSame($token, $context->getToken());
  77. }
  78. }