|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Authorization\Voter; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 15 | +use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
| 16 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
| 17 | + |
| 18 | +class VoterTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + protected $token; |
| 21 | + |
| 22 | + protected function setUp() |
| 23 | + { |
| 24 | + $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
| 25 | + } |
| 26 | + |
| 27 | + public function getTests() |
| 28 | + { |
| 29 | + return array( |
| 30 | + array(array('EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'), |
| 31 | + array(array('CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'), |
| 32 | + |
| 33 | + array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute is supported and grants access'), |
| 34 | + array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if one attribute is supported and denies access'), |
| 35 | + |
| 36 | + array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute grants access'), |
| 37 | + |
| 38 | + array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'), |
| 39 | + |
| 40 | + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'), |
| 41 | + |
| 42 | + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'), |
| 43 | + |
| 44 | + array(array(), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attributes were provided'), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider getTests |
| 50 | + */ |
| 51 | + public function testVote(array $attributes, $expectedVote, $object, $message) |
| 52 | + { |
| 53 | + $voter = new VoterTest_Voter(); |
| 54 | + |
| 55 | + $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class VoterTest_Voter extends Voter |
| 60 | +{ |
| 61 | + protected function voteOnAttribute($attribute, $object, TokenInterface $token) |
| 62 | + { |
| 63 | + return 'EDIT' === $attribute; |
| 64 | + } |
| 65 | + |
| 66 | + protected function supports($attribute, $object) |
| 67 | + { |
| 68 | + return $object instanceof \stdClass && in_array($attribute, array('EDIT', 'CREATE')); |
| 69 | + } |
| 70 | +} |
0 commit comments