RoleVoterTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\RoleVoter;
  11. use Symfony\Component\Security\Authorization\Voter\VoterInterface;
  12. use Symfony\Component\Security\Role\Role;
  13. class RoleVoterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getVoteTests
  17. */
  18. public function testVote($roles, $attributes, $expected)
  19. {
  20. $voter = new RoleVoter();
  21. $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
  22. }
  23. public function getVoteTests()
  24. {
  25. return array(
  26. array(array(), array(), VoterInterface::ACCESS_ABSTAIN),
  27. array(array(), array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  28. array(array(), array('ROLE_FOO'), VoterInterface::ACCESS_DENIED),
  29. array(array('ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
  30. array(array('ROLE_FOO'), array('FOO', 'ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
  31. array(array('ROLE_BAR', 'ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
  32. );
  33. }
  34. protected function getToken(array $roles)
  35. {
  36. foreach ($roles as $i => $role) {
  37. $roles[$i] = new Role($role);
  38. }
  39. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  40. $token->expects($this->once())
  41. ->method('getRoles')
  42. ->will($this->returnValue($roles));
  43. ;
  44. return $token;
  45. }
  46. }