|
12 | 12 | namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
|
| 15 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
15 | 16 |
|
16 |
| -/** |
17 |
| - * @author Roman Marintšenko <inoryy@gmail.com> |
18 |
| - */ |
19 | 17 | class AbstractVoterTest extends \PHPUnit_Framework_TestCase
|
20 | 18 | {
|
21 |
| - /** |
22 |
| - * @var AbstractVoter |
23 |
| - */ |
24 |
| - private $voter; |
25 |
| - |
26 |
| - private $token; |
| 19 | + protected $token; |
27 | 20 |
|
28 | 21 | protected function setUp()
|
29 | 22 | {
|
30 |
| - $this->voter = new VoterFixture(); |
| 23 | + $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
| 24 | + } |
31 | 25 |
|
32 |
| - $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
33 |
| - $tokenMock |
34 |
| - ->expects($this->any()) |
35 |
| - ->method('getUser') |
36 |
| - ->will($this->returnValue('user')); |
| 26 | + public function getTests() |
| 27 | + { |
| 28 | + return array( |
| 29 | + array(array('EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'), |
| 30 | + array(array('CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'), |
37 | 31 |
|
38 |
| - $this->token = $tokenMock; |
| 32 | + array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute is supported and grants access'), |
| 33 | + array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if one attribute is supported and denies access'), |
| 34 | + |
| 35 | + array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute grants access'), |
| 36 | + |
| 37 | + array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'), |
| 38 | + |
| 39 | + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'), |
| 40 | + |
| 41 | + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'), |
| 42 | + |
| 43 | + array(array(), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attributes were provided'), |
| 44 | + ); |
39 | 45 | }
|
40 | 46 |
|
41 | 47 | /**
|
42 |
| - * @dataProvider getData |
| 48 | + * @dataProvider getTests |
43 | 49 | */
|
44 |
| - public function testVote($expectedVote, $object, $attributes, $message) |
| 50 | + public function testVote(array $attributes, $expectedVote, $object, $message) |
45 | 51 | {
|
46 |
| - $this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message); |
47 |
| - } |
| 52 | + $voter = new AbstractVoterTest_Voter(); |
48 | 53 |
|
49 |
| - public function getData() |
50 |
| - { |
51 |
| - return array( |
52 |
| - array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'), |
53 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'), |
54 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'), |
55 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'), |
56 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'), |
57 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), |
58 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), |
59 |
| - array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'), |
60 |
| - ); |
| 54 | + $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); |
61 | 55 | }
|
62 | 56 | }
|
63 | 57 |
|
64 |
| -class VoterFixture extends AbstractVoter |
| 58 | +class AbstractVoterTest_Voter extends AbstractVoter |
65 | 59 | {
|
66 | 60 | protected function getSupportedClasses()
|
67 | 61 | {
|
68 |
| - return array( |
69 |
| - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', |
70 |
| - ); |
| 62 | + return array('stdClass'); |
71 | 63 | }
|
72 | 64 |
|
73 | 65 | protected function getSupportedAttributes()
|
74 | 66 | {
|
75 |
| - return array('foo', 'bar', 'baz'); |
| 67 | + return array('EDIT', 'CREATE'); |
76 | 68 | }
|
77 | 69 |
|
78 | 70 | protected function isGranted($attribute, $object, $user = null)
|
79 | 71 | {
|
80 |
| - return $attribute === 'foo'; |
| 72 | + return 'EDIT' === $attribute; |
81 | 73 | }
|
82 | 74 | }
|
83 |
| - |
84 |
| -class ObjectFixture |
85 |
| -{ |
86 |
| -} |
87 |
| - |
88 <
41A3
/td> |
| -class UnsupportedObjectFixture |
89 |
| -{ |
90 |
| -} |
0 commit comments