From df36835e7f3c4206acec26a038da9abc1bf78a7b Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sat, 26 Sep 2015 23:12:58 +0200 Subject: [PATCH 1/2] Readd the correct tests --- .../Authorization/Voter/AbstractVoterTest.php | 121 +++++------------- .../Voter/LegacyAbstractVoterTest.php | 35 ++++- 2 files changed, 60 insertions(+), 96 deletions(-) diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php index ea72e75954492..e1270bb394ea6 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php @@ -13,132 +13,69 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; -/** - * @author Roman Marintšenko - */ class AbstractVoterTest extends \PHPUnit_Framework_TestCase { - private $token; + protected $voter; + protected $object; + protected $token; protected function setUp() { - $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - $tokenMock - ->expects($this->any()) - ->method('getUser') - ->will($this->returnValue('user')); - - $this->token = $tokenMock; - } - - /** - * @dataProvider getData - */ - public function testVote($expectedVote, $object, $attributes, $message) - { - $voter = new VoterFixture(); - - $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); - } - - /** - * @dataProvider getData - * @group legacy - */ - public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message) - { - $voter = new DeprecatedVoterFixture(); - - $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); - } - - /** - * @group legacy - * @expectedException \BadMethodCallException - */ - public function testNoOverriddenMethodsThrowsException() - { - $voter = new DeprecatedVoterNothingImplementedFixture(); - $voter->vote($this->token, new ObjectFixture(), array('foo')); + $this->voter = new AbstractVoterTest_Voter(); + $this->object = $this->getMock('AbstractVoterTest_Object'); + $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); } - public function getData() + public function testAttributeAndClassSupported() { - 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'), - ); + $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'); } -} -class VoterFixture extends AbstractVoter -{ - protected function getSupportedClasses() + public function testOneAttributeSupported() { - return array( - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', - ); + $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'); } - protected function getSupportedAttributes() + public function testOneAttributeGrantsAccess() { - return array('foo', 'bar', 'baz'); + $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED'); } - protected function voteOnAttribute($attribute, $object, TokenInterface $token) + public function testNoAttributeSupported() { - return $attribute === 'foo'; + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN'); } -} -class DeprecatedVoterFixture extends AbstractVoter -{ - protected function getSupportedClasses() + public function testClassNotSupported() { - return array( - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', - ); + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN'); } - protected function getSupportedAttributes() + public function testNullObject() { - return array('foo', 'bar', 'baz'); + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN'); } - protected function isGranted($attribute, $object, $user = null) + public function testNoAttributes() { - return $attribute === 'foo'; + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN'); } } -class DeprecatedVoterNothingImplementedFixture extends AbstractVoter +class AbstractVoterTest_Voter extends AbstractVoter { - protected function getSupportedClasses() + protected function voteOnAttribute($attribute, $object, TokenInterface $token) { - return array( - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', - ); + return 'EDIT' === $attribute; } - protected function getSupportedAttributes() + protected function supports($attribute, $class) { - return array('foo', 'bar', 'baz'); + return $this->isClassInstanceOf($class, 'AbstractVoterTest_Object') + && in_array($attribute, array('EDIT', 'CREATE')); } - - // this is a bad voter that hasn't overridden isGranted or voteOnAttribute -} - -class ObjectFixture -{ -} - -class UnsupportedObjectFixture -{ } diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php index b49f23f5bb122..dd033b3de4053 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php @@ -13,6 +13,26 @@ use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; +class LegacyAbstractVoterTest extends AbstractVoterTest +{ + protected function setUp() + { + parent::setUp(); + + $this->voter = new LegacyAbstractVoterTest_Voter(); + } + + /** + * @group legacy + * @expectedException \BadMethodCallException + */ + public function testNoOverriddenMethodsThrowsException() + { + $voter = new LegacyAbstractVoterTest_NothingImplementedVoter(); + $voter->vote($this->token, $this->object, array('foo')); + } +} + class LegacyAbstractVoterTest_Voter extends AbstractVoter { protected function getSupportedClasses() @@ -31,12 +51,19 @@ protected function isGranted($attribute, $object, $user = null) } } -class LegacyAbstractVoterTest extends AbstractVoterTest +class LegacyAbstractVoterTest_NothingImplementedVoter extends AbstractVoter { - protected function setUp() + protected function getSupportedClasses() { - parent::setUp(); + return array( + 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', + ); + } - $this->voter = new LegacyAbstractVoterTest_Voter(); + protected function getSupportedAttributes() + { + return array('EDIT', 'CREATE'); } + + // this is a bad voter that hasn't overridden isGranted or voteOnAttribute } From 9f1a345696c9163708d17d942174a09d25c4b094 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 27 Sep 2015 12:36:59 +0200 Subject: [PATCH 2/2] Rewrote tests to use a data provider --- .../Authorization/Voter/AbstractVoterTest.php | 97 ++++++++++++++----- .../Voter/LegacyAbstractVoterTest.php | 26 ----- 2 files changed, 73 insertions(+), 50 deletions(-) diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php index e1270bb394ea6..a3906ed0cc695 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php @@ -17,52 +17,68 @@ class AbstractVoterTest extends \PHPUnit_Framework_TestCase { - protected $voter; protected $object; protected $token; protected function setUp() { - $this->voter = new AbstractVoterTest_Voter(); $this->object = $this->getMock('AbstractVoterTest_Object'); $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); } - public function testAttributeAndClassSupported() + public function getTests() { - $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'); - } + $object = $this->getMock('AbstractVoterTest_Object'); - public function testOneAttributeSupported() - { - $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( + array(array('EDIT'), VoterInterface::ACCESS_GRANTED, $object, 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'), + array(array('CREATE'), VoterInterface::ACCESS_DENIED, $object, 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'), - public function testOneAttributeGrantsAccess() - { - $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED'); - } + array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, $object, 'ACCESS_GRANTED if one attribute is supported and grants access'), + array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, $object, 'ACCESS_DENIED if one attribute is supported and denies access'), - public function testNoAttributeSupported() - { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN'); + array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, $object, 'ACCESS_GRANTED if one attribute grants access'), + + array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, $object, 'ACCESS_ABSTAIN if no attribute is supported'), + + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this->getMock('AbstractVoterTest_Object1'), 'ACCESS_ABSTAIN if class is not supported'), + + array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'), + + array(array(), VoterInterface::ACCESS_ABSTAIN, $object, 'ACCESS_ABSTAIN if no attributes were provided'), + ); } - public function testClassNotSupported() + /** + * @dataProvider getTests + */ + public function testVote(array $attributes, $expectedVote, $object, $message) { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN'); + $voter = new AbstractVoterTest_Voter(); + + $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); } - public function testNullObject() + /** + * @dataProvider getTests + * @group legacy + */ + public function testVoteLegacy(array $attributes, $expectedVote, $object, $message) { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN'); + $voter = new AbstractVoterTest_LegacyVoter(); + + $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); } - public function testNoAttributes() + /** + * @group legacy + * @expectedException \BadMethodCallException + */ + public function testNoOverriddenMethodsThrowsException() { - $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN'); + $voter = new AbstractVoterTest_NothingImplementedVoter(); + + $voter->vote($this->token, $this->object, array('EDIT')); } } @@ -79,3 +95,36 @@ protected function supports($attribute, $class) && in_array($attribute, array('EDIT', 'CREATE')); } } + +class AbstractVoterTest_LegacyVoter extends AbstractVoter +{ + protected function getSupportedClasses() + { + return array('AbstractVoterTest_Object'); + } + + protected function getSupportedAttributes() + { + return array('EDIT', 'CREATE'); + } + + protected function isGranted($attribute, $object, $user = null) + { + return 'EDIT' === $attribute; + } +} + +class AbstractVoterTest_NothingImplementedVoter extends AbstractVoter +{ + protected function getSupportedClasses() + { + return array('AbstractVoterTest_Object'); + } + + protected function getSupportedAttributes() + { + return array('EDIT', 'CREATE'); + } + + // this is a bad voter that hasn't overridden isGranted or voteOnAttribute +} diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php index dd033b3de4053..f72d17eaf7152 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/LegacyAbstractVoterTest.php @@ -21,16 +21,6 @@ protected function setUp() $this->voter = new LegacyAbstractVoterTest_Voter(); } - - /** - * @group legacy - * @expectedException \BadMethodCallException - */ - public function testNoOverriddenMethodsThrowsException() - { - $voter = new LegacyAbstractVoterTest_NothingImplementedVoter(); - $voter->vote($this->token, $this->object, array('foo')); - } } class LegacyAbstractVoterTest_Voter extends AbstractVoter @@ -51,19 +41,3 @@ protected function isGranted($attribute, $object, $user = null) } } -class LegacyAbstractVoterTest_NothingImplementedVoter extends AbstractVoter -{ - protected function getSupportedClasses() - { - return array( - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', - ); - } - - protected function getSupportedAttributes() - { - return array('EDIT', 'CREATE'); - } - - // this is a bad voter that hasn't overridden isGranted or voteOnAttribute -}