10000 Rewrote tests to use a data provider · symfony/symfony@9f1a345 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f1a345

Browse files
committed
Rewrote tests to use a data provider
1 parent df36835 commit 9f1a345

File tree

2 files changed

+73
-50
lines changed

2 files changed

+73
-50
lines changed

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,68 @@
1717

1818
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
1919
{
20-
protected $voter;
2120
protected $object;
2221
protected $token;
2322

2423
protected function setUp()
2524
{
26-
$this->voter = new AbstractVoterTest_Voter();
2725
$this->object = $this->getMock('AbstractVoterTest_Object');
2826
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
2927
}
3028

31-
public function testAttributeAndClassSupported()
29+
public function getTests()
3230
{
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');
3632

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'),
4236

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'),
4739

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+
);
5150
}
5251

53-
public function testClassNotSupported()
52+
/**
53+
* @dataProvider getTests
54+
*/
55+
public function testVote(array $attributes, $expectedVote, $object, $message)
5456
{
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);
5660
}
5761

58-
public function testNullObject()
62+
/**
63+
* @dataProvider getTests
64+
* @group legacy
65+
*/
66+
public function testVoteLegacy(array $attributes, $expectedVote, $object, $message)
5967
{
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);
6171
}
6272

63-
public function testNoAttributes()
73+
/**
74+
* @group legacy
75+
* @expectedException \BadMethodCallException
76+
*/
77+
public function testNoOverriddenMethodsThrowsException()
6478
{
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'));
6682
}
6783
}
6884

@@ -79,3 +95,36 @@ protected function supports($attribute, $class)
7995
&& in_array($attribute, array('EDIT', 'CREATE'));
8096
}
8197
}
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+
}

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ protected function setUp()
2121

2222
$this->voter = new LegacyAbstractVoterTest_Voter();
2323
}
24-
25-
/**
26-
* @group legacy
27-
* @expectedException \BadMethodCallException
28-
*/
29-
public function testNoOverriddenMethodsThrowsException()
30-
{
31-
$voter = new LegacyAbstractVoterTest_NothingImplementedVoter();
32-
$voter->vote($this->token, $this->object, array('foo'));
33-
}
3424
}
3525

3626
class LegacyAbstractVoterTest_Voter extends AbstractVoter
@@ -51,19 +41,3 @@ protected function isGranted($attribute, $object, $user = null)
5141
}
5242
}
5343

54-
class LegacyAbstractVoterTest_NothingImplementedVoter extends AbstractVoter
55-
{
56-
protected function getSupportedClasses()
57-
{
58-
return array(
59-
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
60-
);
61-
}
62-
63-
protected function getSupportedAttributes()
64-
{
65-
return array('EDIT', 'CREATE');
66-
}
67-
68-
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
69-
}

0 commit comments

Comments
 (0)
0