RoleVoterTest.php 1.9 KB

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