17
17
18
18
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
19
19
{
20
- protected $ voter ;
21
20
protected $ object ;
22
21
protected $ token ;
23
22
24
23
protected function setUp ()
25
24
{
26
- $ this ->voter = new AbstractVoterTest_Voter ();
27
25
$ this ->object = $ this ->getMock ('AbstractVoterTest_Object ' );
28
26
$ this ->token = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' );
29
27
}
30
28
31
- public function testAttributeAndClassSupported ()
29
+ public function getTests ()
32
30
{
33
- $ this ->assertEquals (VoterInterface::ACCESS_GRANTED , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('EDIT ' )), 'ACCESS_GRANTED if attribute grants access ' );
34
- $ this ->assertEquals (VoterInterface::ACCESS_DENIED , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('CREATE ' )), 'ACESS_DENIED if attribute denies access ' );
35
- }
31
+ $ object = $ this ->getMock ('AbstractVoterTest_Object ' );
36
32
37
- public function testOneAttributeSupported ()
38
- {
39
- $ this ->assertEquals (VoterInterface::ACCESS_GRANTED , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('DELETE ' , 'EDIT ' )), 'ACCESS_GRANTED if supported attribute grants access ' );
40
- $ this ->assertEquals (VoterInterface::ACCESS_DENIED , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('DELETE ' , 'CREATE ' )), 'ACCESS_DENIED if supported attribute denies access ' );
41
- }
33
+ return array (
34
+ array (array ('EDIT ' ), VoterInterface::ACCESS_GRANTED , $ object , 'ACCESS_GRANTED if attribute and class are supported and attribute grants access ' ),
35
+ array (array ('CREATE ' ), VoterInterface::ACCESS_DENIED , $ object , 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access ' ),
42
36
43
- public function testOneAttributeGrantsAccess ()
44
- {
45
- $ this ->assertEquals (VoterInterface::ACCESS_GRANTED , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('CREATE ' , 'EDIT ' )), 'ACCESS_GRANTED ' );
46
- }
37
+ array (array ('DELETE ' , 'EDIT ' ), VoterInterface::ACCESS_GRANTED , $ object , 'ACCESS_GRANTED if one attribute is supported and grants access ' ),
38
+ array (array ('DELETE ' , 'CREATE ' ), VoterInterface::ACCESS_DENIED , $ object , 'ACCESS_DENIED if one attribute is supported and denies access ' ),
47
39
48
- public function testNoAttributeSupported ()
49
- {
50
- $ this ->assertEquals (VoterInterface::ACCESS_ABSTAIN , $ this ->voter ->vote ($ this ->token , $ this ->object , array ('DELETE ' )), 'ACCESS_ABSTAIN ' );
40
+ array (array ('CREATE ' , 'EDIT ' ), VoterInterface::ACCESS_GRANTED , $ object , 'ACCESS_GRANTED if one attribute grants access ' ),
41
+
42
+ array (array ('DELETE ' ), VoterInterface::ACCESS_ABSTAIN , $ object , 'ACCESS_ABSTAIN if no attribute is supported ' ),
43
+
44
+ array (array ('EDIT ' ), VoterInterface::ACCESS_ABSTAIN , $ this ->getMock ('AbstractVoterTest_Object1 ' ), 'ACCESS_ABSTAIN if class is not supported ' ),
45
+
46
+ array (array ('EDIT ' ), VoterInterface::ACCESS_ABSTAIN , null , 'ACCESS_ABSTAIN if object is null ' ),
47
+
48
+ array (array (), VoterInterface::ACCESS_ABSTAIN , $ object , 'ACCESS_ABSTAIN if no attributes were provided ' ),
49
+ );
51
50
}
52
51
53
- public function testClassNotSupported ()
52
+ /**
53
+ * @dataProvider getTests
54
+ */
55
+ public function testVote (array $ attributes , $ expectedVote , $ object , $ message )
54
56
{
55
- $ this ->assertEquals (VoterInterface::ACCESS_ABSTAIN , $ this ->voter ->vote ($ this ->token , $ this ->getMock ('AbstractVoterTest_Object1 ' ), array ('EDIT ' )), 'ACCESS_ABSTAIN ' );
57
+ $ voter = new AbstractVoterTest_Voter ();
58
+
59
+ $ this ->assertEquals ($ expectedVote , $ voter ->vote ($ this ->token , $ object , $ attributes ), $ message );
56
60
}
57
61
58
- public function testNullObject ()
62
+ /**
63
+ * @dataProvider getTests
64
+ * @group legacy
65
+ */
66
+ public function testVoteLegacy (array $ attributes , $ expectedVote , $ object , $ message )
59
67
{
60
- $ this ->assertEquals (VoterInterface::ACCESS_ABSTAIN , $ this ->voter ->vote ($ this ->token , null , array ('EDIT ' )), 'ACCESS_ABSTAIN ' );
68
+ $ voter = new AbstractVoterTest_LegacyVoter ();
69
+
70
+ $ this ->assertEquals ($ expectedVote , $ voter ->vote ($ this ->token , $ object , $ attributes ), $ message );
61
71
}
62
72
63
- public function testNoAttributes ()
73
+ /**
74
+ * @group legacy
75
+ * @expectedException \BadMethodCallException
76
+ */
77
+ public function testNoOverriddenMethodsThrowsException ()
64
78
{
65
- $ this ->assertEquals (VoterInterface::ACCESS_ABSTAIN , $ this ->voter ->vote ($ this ->token , $ this ->object , array ()), 'ACCESS_ABSTAIN ' );
79
+ $ voter = new AbstractVoterTest_NothingImplementedVoter ();
80
+
81
+ $ voter ->vote ($ this ->token , $ this ->object , array ('EDIT ' ));
66
82
}
67
83
}
68
84
@@ -79,3 +95,36 @@ protected function supports($attribute, $class)
79
95
&& in_array ($ attribute , array ('EDIT ' , 'CREATE ' ));
80
96
}
81
97
}
98
+
99
+ class AbstractVoterTest_LegacyVoter extends AbstractVoter
100
+ {
101
+ protected function getSupportedClasses ()
102
+ {
103
+ return array ('AbstractVoterTest_Object ' );
104
+ }
105
+
106
+ protected function getSupportedAttributes ()
107
+ {
108
+ return array ('EDIT ' , 'CREATE ' );
109
+ }
110
+
111
+ protected function isGranted ($ attribute , $ object , $ user = null )
112
+ {
113
+ return 'EDIT ' === $ attribute ;
114
+ }
115
+ }
116
+
117
+ class AbstractVoterTest_NothingImplementedVoter extends AbstractVoter
118
+ {
119
+ protected function getSupportedClasses ()
120
+ {
121
+ return array ('AbstractVoterTest_Object ' );
122
+ }
123
+
124
+ protected function getSupportedAttributes ()
125
+ {
126
+ return array ('EDIT ' , 'CREATE ' );
127
+ }
128
+
129
+ // this is a bad voter that hasn't overridden isGranted or voteOnAttribute
130
+ }
0 commit comments