RoleVoterTest.php 1.9 KB

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