AuthenticatedVoterTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Authorization\Voter;
  10. use Symfony\Component\Security\Authorization\Voter\AuthenticatedVoter;
  11. use Symfony\Component\Security\Authorization\Voter\VoterInterface;
  12. use Symfony\Component\Security\Role\Role;
  13. class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testSupportsClass()
  16. {
  17. $voter = new AuthenticatedVoter();
  18. $this->assertTrue($voter->supportsClass('stdClass'));
  19. }
  20. /**
  21. * @dataProvider getVoteTests
  22. */
  23. public function testVote($authenticated, $attributes, $expected)
  24. {
  25. $voter = new AuthenticatedVoter();
  26. $this->assertSame($expected, $voter->vote($this->getToken($authenticated), null, $attributes));
  27. }
  28. public function getVoteTests()
  29. {
  30. return array(
  31. array(true, array(), VoterInterface::ACCESS_ABSTAIN),
  32. array(true, array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  33. array(false, array(), VoterInterface::ACCESS_ABSTAIN),
  34. array(false, array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  35. array(true, array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
  36. array(false, array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
  37. array(true, array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_GRANTED),
  38. array(false, array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED),
  39. );
  40. }
  41. protected function getToken($authenticated)
  42. {
  43. if ($authenticated) {
  44. return $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  45. } else {
  46. return $this->getMock('Symfony\Component\Security\Authentication\Token\AnonymousToken', null, array('', ''));
  47. }
  48. }
  49. }