diff options
-rw-r--r-- | Core/Tests/Authorization/Voter/AbstractVoterTest.php | 118 | ||||
-rw-r--r-- | Core/Tests/LegacySecurityContextInterfaceTest.php (renamed from Tests/Core/LegacySecurityContextInterfaceTest.php) | 2 |
2 files changed, 85 insertions, 35 deletions
diff --git a/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/Core/Tests/Authorization/Voter/AbstractVoterTest.php index 23ac6db..44da147 100644 --- a/Core/Tests/Authorization/Voter/AbstractVoterTest.php +++ b/Core/Tests/Authorization/Voter/AbstractVoterTest.php @@ -1,72 +1,122 @@ <?php +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Security\Core\Tests\Authorization\Voter; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; -use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; -class AbstractVoterTest_Voter extends AbstractVoter +/** + * @author Roman Marintšenko <inoryy@gmail.com> + */ +class AbstractVoterTest extends \PHPUnit_Framework_TestCase { - protected function voteOnAttribute($attribute, $object, TokenInterface $token) + /** + * @var AbstractVoter + */ + private $voter; + + private $token; + + protected function setUp() { - return 'EDIT' === $attribute; + $this->voter = new VoterFixture(); + + $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $tokenMock + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue('user')); + + $this->token = $tokenMock; } - protected function supports($attribute, $class) + /** + * @dataProvider getData + */ + public function testVote($expectedVote, $object, $attributes, $message) { - return $this->isClassInstanceOf($class, 'AbstractVoterTest_Object') - && in_array($attribute, array('EDIT', 'CREATE')); + $this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message); } -} - -class AbstractVoterTest extends \PHPUnit_Framework_TestCase -{ - protected $voter; - protected $object; - protected $token; - protected function setUp() + /** + * @dataProvider getData + * @group legacy + */ + public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message) { - $this->voter = new AbstractVoterTest_Voter(); - $this->object = $this->getMock('AbstractVoterTest_Object'); - $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $voter = new DeprecatedVoterFixture(); + + $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); } - public function testAttributeAndClassSupported() + public function getData() { - $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('EDIT')), 'ACCESS_GRANTED if attribute grants access'); - $this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('CREATE')), 'ACESS_DENIED if attribute denies access'); + return array( + array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'), + array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'), + array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'), + array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), + array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'), + ); } +} - public function testOneAttributeSupported() +class VoterFixture extends AbstractVoter +{ + protected function getSupportedClasses() { - $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('DELETE', 'EDIT')), 'ACCESS_GRANTED if supported attribute grants access'); - $this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('DELETE', 'CREATE')), 'ACCESS_DENIED if supported attribute denies access'); + return array( + 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', + ); } - public function testOneAttributeGrantsAccess() + protected function getSupportedAttributes() { - $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED'); + return array('foo', 'bar', 'baz'); } - public function testNoAttributeSupported() + protected function voteOnAttribute($attribute, $object, TokenInterface $token) { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN'); + return $attribute === 'foo'; } +} - public function testClassNotSupported() +class DeprecatedVoterFixture extends AbstractVoter +{ + protected function getSupportedClasses() { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN'); + return array( + 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', + ); } - public function testNullObject() + protected function getSupportedAttributes() { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN'); + return array('foo', 'bar', 'baz'); } - public function testNoAttributes() + protected function isGranted($attribute, $object, $user = null) { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN'); + return $attribute === 'foo'; } } + +class ObjectFixture +{ +} + +class UnsupportedObjectFixture +{ +} diff --git a/Tests/Core/LegacySecurityContextInterfaceTest.php b/Core/Tests/LegacySecurityContextInterfaceTest.php index 5225eb5..a45ecf9 100644 --- a/Tests/Core/LegacySecurityContextInterfaceTest.php +++ b/Core/Tests/LegacySecurityContextInterfaceTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Security\Tests\Core; +namespace Symfony\Component\Security\Core\Tests; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\Security; |