summaryrefslogtreecommitdiffstats
path: root/Tests/Core/Authentication/Voter/AbstractVoterTest.php
diff options
context:
space:
mode:
authorRoman Marintšenko <inoryy@gmail.com>2014-06-20 14:24:18 +0300
committerFabien Potencier <fabien.potencier@gmail.com>2014-09-23 11:51:18 +0200
commit18d45ff47273f1bb9a11b917f653621cf6cdad03 (patch)
treede6a954cd3a955f7154bf0ebe32d117c98c31213 /Tests/Core/Authentication/Voter/AbstractVoterTest.php
parent729bef85463490cebd41160e3a111954f63a1638 (diff)
downloadsymfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.zip
symfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.tar.gz
symfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.tar.bz2
[Security] add an AbstractVoter implementation
Diffstat (limited to 'Tests/Core/Authentication/Voter/AbstractVoterTest.php')
-rw-r--r--Tests/Core/Authentication/Voter/AbstractVoterTest.php90
1 files changed, 90 insertions, 0 deletions
diff --git a/Tests/Core/Authentication/Voter/AbstractVoterTest.php b/Tests/Core/Authentication/Voter/AbstractVoterTest.php
new file mode 100644
index 0000000..955b610
--- /dev/null
+++ b/Tests/Core/Authentication/Voter/AbstractVoterTest.php
@@ -0,0 +1,90 @@
+<?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\Tests\Core\Authentication\Voter;
+
+use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
+
+/**
+ * @author Roman Marintšenko <inoryy@gmail.com>
+ */
+class AbstractVoterTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var AbstractVoter
+ */
+ private $voter;
+
+ private $token;
+
+ public function setUp()
+ {
+ $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;
+ }
+
+ /**
+ * @dataProvider getData
+ */
+ public function testVote($expectedVote, $object, $attributes, $message)
+ {
+ $this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message);
+ }
+
+ public function getData()
+ {
+ 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'),
+ );
+ }
+}
+
+class VoterFixture extends AbstractVoter
+{
+ protected function getSupportedClasses()
+ {
+ return array(
+ 'Symfony\Component\Security\Tests\Core\Authentication\Voter\ObjectFixture',
+ );
+ }
+
+ protected function getSupportedAttributes()
+ {
+ return array( 'foo', 'bar', 'baz');
+ }
+
+ protected function isGranted($attribute, $object, $user = null)
+ {
+ return $attribute === 'foo';
+ }
+}
+
+class ObjectFixture
+{
+}
+
+class UnsupportedObjectFixture
+{
+}