8000 Readd the correct tests · symfony/symfony@df36835 · GitHub
[go: up one dir, main page]

Skip to content

Commit df36835

Browse files
committed
Readd the correct tests
1 parent 5dcdc48 commit df36835

File tree

2 files changed

+60
-96
lines changed

2 files changed

+60
-96
lines changed

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

Lines changed: 29 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,132 +13,69 @@
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1515
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
16+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
1617

17-
/**
18-
* @author Roman Marintšenko <inoryy@gmail.com>
19-
*/
2018
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
2119
{
22-
private $token;
20+
protected $voter;
21+
protected $object;
22+
protected $token;
2323

2424
protected function setUp()
2525
{
26-
$tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
27-
$tokenMock
28-
->expects($this->any())
29-
->method('getUser')
30-
->will($this->returnValue('user'));
31-
32-
$this->token = $tokenMock;
33-
}
34-
35-
/**
36-
* @dataProvider getData
37-
*/
38-
public function testVote($expectedVote, $object, $attributes, $message)
39-
{
40-
$voter = new VoterFixture();
41-
42-
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
43-
}
44-
45-
/**
46-
* @dataProvider getData
47-
* @group legacy
48-
*/
49-
public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message)
50-
{
51-
$voter = new DeprecatedVoterFixture();
52-
53-
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
54-
}
55-
56-
/**
57-
* @group legacy
58-
* @expectedException \BadMethodCallException
59-
*/
60-
public function testNoOverriddenMethodsThrowsException()
61-
{
62-
$voter = new DeprecatedVoterNothingImplementedFixture();
63-
$voter->vote($this->token, new ObjectFixture(), array('foo'));
26+
$this->voter = new AbstractVoterTest_Voter();
27+
$this->object = $this->getMock('AbstractVoterTest_Object');
28+
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
6429
}
6530

66-
public function getData()
31+
public function testAttributeAndClassSupported()
6732
{
68-
return array(
69-
array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'),
70-
array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'),
71-
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'),
72-
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'),
73-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'),
74-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
75-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
76-
array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'),
77-
);
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');
7835
}
79-
}
8036

81-
class VoterFixture extends AbstractVoter
82-
{
83-
protected function getSupportedClasses()
37+
public function testOneAttributeSupported()
8438
{
85-
return array(
86-
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
87-
);
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');
8841
}
8942

90-
protected function getSupportedAttributes()
43+
public function testOneAttributeGrantsAccess()
9144
{
92-
return array('foo', 'bar', 'baz');
45+
$this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED');
9346
}
9447

95-
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
48+
public function testNoAttributeSupported()
9649
{
97-
return $attribute === 'foo';
50+
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN');
9851
}
99-
}
10052

101-
class DeprecatedVoterFixture extends AbstractVoter
102-
{
103-
protected function getSupportedClasses()
53+
public function testClassNotSupported()
10454
{
105-
return array(
106-
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
107-
);
55+
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN');
10856
}
10957

110-
protected function getSupportedAttributes()
58+
public function testNullObject()
11159
{
112-
return array('foo', 'bar', 'baz');
60+
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN');
11361
}
11462

115-
protected function isGranted($attribute, $object, $user = null)
63+
public function testNoAttributes()
11664
{
117-
return $attribute === 'foo';
65+
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN');
11866
}
11967
}
12068

121-
class DeprecatedVoterNothingImplementedFixture extends AbstractVoter
69+
class AbstractVoterTest_Voter extends AbstractVoter
12270
{
123-
protected function getSupportedClasses()
71+
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
12472
{
125-
return array(
126-
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
127-
);
73+
return 'EDIT' === $attribute;
12874
}
12975

130-
protected function getSupportedAttributes()
76+
protected function supports($attribute, $class)
13177
{
132-
return array('foo', 'bar', 'baz');
78+
return $this->isClassInstanceOf($class, 'AbstractVoterTest_Object')
79+
&& in_array($attribute, array('EDIT', 'CREATE'));
13380
}
134-
135-
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
136-
}
137-
138-
class ObjectFixture
139-
{
140-
}
141-
142-
class UnsupportedObjectFixture
143-
{
14481
}

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313

1414
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
1515

16+
class LegacyAbstractVoterTest extends AbstractVoterTest
17+
{
18+
protected function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->voter = new LegacyAbstractVoterTest_Voter();
23+
}
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+
}
34+
}
35+
1636
class LegacyAbstractVoterTest_Voter extends AbstractVoter
1737
{
1838
protected function getSupportedClasses()
@@ -31,12 +51,19 @@ protected function isGranted($attribute, $object, $user = null)
3151
}
3252
}
3353

34-
class LegacyAbstractVoterTest extends AbstractVoterTest
54+
class LegacyAbstractVoterTest_NothingImplementedVoter extends AbstractVoter
3555
{
36-
protected function setUp()
56+
protected function getSupportedClasses()
3757
{
38-
parent::setUp();
58+
return array(
59+
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
60+
);
61+
}
3962

40-
$this->voter = new LegacyAbstractVoterTest_Voter();
63+
protected function getSupportedAttributes()
64+
{
65+
return array('EDIT', 'CREATE');
4166
}
67+
68+
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
4269
}

0 commit comments

Comments
 (0)
0