summaryrefslogtreecommitdiffstats
path: root/Core/Tests/Authentication/Token/AbstractTokenTest.php
diff options
context:
space:
mode:
authorBernhard Schussek <bschussek@gmail.com>2013-09-16 10:03:00 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2013-09-18 09:16:41 +0200
commit5a6aaab2c35213f5ca7e57f061fbb2675e2ece35 (patch)
tree461816fef8160401dc113d3fef190fb437d01cc7 /Core/Tests/Authentication/Token/AbstractTokenTest.php
parent513a354be10f0ed87933adcb788e48660f8e6ed4 (diff)
downloadsymfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.zip
symfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.tar.gz
symfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.tar.bz2
[Security] Split the component into 3 sub-components Core, ACL, HTTP
Diffstat (limited to 'Core/Tests/Authentication/Token/AbstractTokenTest.php')
-rw-r--r--Core/Tests/Authentication/Token/AbstractTokenTest.php244
1 files changed, 244 insertions, 0 deletions
diff --git a/Core/Tests/Authentication/Token/AbstractTokenTest.php b/Core/Tests/Authentication/Token/AbstractTokenTest.php
new file mode 100644
index 0000000..928ee40
--- /dev/null
+++ b/Core/Tests/Authentication/Token/AbstractTokenTest.php
@@ -0,0 +1,244 @@
+<?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\Authentication\Token;
+
+use Symfony\Component\Security\Core\Role\Role;
+
+class TestUser
+{
+ protected $name;
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ public function __toString()
+ {
+ return $this->name;
+ }
+}
+
+class AbstractTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testGetUsername()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $token->setUser('fabien');
+ $this->assertEquals('fabien', $token->getUsername());
+
+ $token->setUser(new TestUser('fabien'));
+ $this->assertEquals('fabien', $token->getUsername());
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
+ $token->setUser($user);
+ $this->assertEquals('fabien', $token->getUsername());
+ }
+
+ public function testEraseCredentials()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->once())->method('eraseCredentials');
+ $token->setUser($user);
+
+ $token->eraseCredentials();
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::unserialize
+ */
+ public function testSerialize()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $token->setAttributes(array('foo' => 'bar'));
+
+ $uToken = unserialize(serialize($token));
+
+ $this->assertEquals($token->getRoles(), $uToken->getRoles());
+ $this->assertEquals($token->getAttributes(), $uToken->getAttributes());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct
+ */
+ public function testConstructor()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+
+ $token = $this->getToken(array(new Role('ROLE_FOO')));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+
+ $token = $this->getToken(array(new Role('ROLE_FOO'), 'ROLE_BAR'));
+ $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_BAR')), $token->getRoles());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::isAuthenticated
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAuthenticated
+ */
+ public function testAuthenticatedFlag()
+ {
+ $token = $this->getToken();
+ $this->assertFalse($token->isAuthenticated());
+
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setAuthenticated(false);
+ $this->assertFalse($token->isAuthenticated());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttributes
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttributes
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::hasAttribute
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttribute
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttribute
+ */
+ public function testAttributes()
+ {
+ $attributes = array('foo' => 'bar');
+ $token = $this->getToken();
+ $token->setAttributes($attributes);
+
+ $this->assertEquals($attributes, $token->getAttributes(), '->getAttributes() returns the token attributes');
+ $this->assertEquals('bar', $token->getAttribute('foo'), '->getAttribute() returns the value of an attribute');
+ $token->setAttribute('foo', 'foo');
+ $this->assertEquals('foo', $token->getAttribute('foo'), '->setAttribute() changes the value of an attribute');
+ $this->assertTrue($token->hasAttribute('foo'), '->hasAttribute() returns true if the attribute is defined');
+ $this->assertFalse($token->hasAttribute('oof'), '->hasAttribute() returns false if the attribute is not defined');
+
+ try {
+ $token->getAttribute('foobar');
+ $this->fail('->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ $this->assertEquals('This token has no "foobar" attribute.', $e->getMessage(), '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ }
+ }
+
+ /**
+ * @dataProvider getUsers
+ */
+ public function testSetUser($user)
+ {
+ $token = $this->getToken();
+ $token->setUser($user);
+ $this->assertSame($user, $token->getUser());
+ }
+
+ public function getUsers()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
+
+ return array(
+ array($advancedUser),
+ array($user),
+ array(new TestUser('foo')),
+ array('foo'),
+ );
+ }
+
+ /**
+ * @dataProvider getUserChanges
+ */
+ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
+ {
+ $token = $this->getToken();
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($firstUser);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($secondUser);
+ $this->assertFalse($token->isAuthenticated());
+ }
+
+ public function getUserChanges()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
+
+ return array(
+ array(
+ 'foo', 'bar',
+ ),
+ array(
+ 'foo', new TestUser('bar'),
+ ),
+ array(
+ 'foo', $user,
+ ),
+ array(
+ 'foo', $advancedUser
+ ),
+ array(
+ $user, 'foo'
+ ),
+ array(
+ $advancedUser, 'foo'
+ ),
+ array(
+ $user, new TestUser('foo'),
+ ),
+ array(
+ $advancedUser, new TestUser('foo'),
+ ),
+ array(
+ new TestUser('foo'), new TestUser('bar'),
+ ),
+ array(
+ new TestUser('foo'), 'bar',
+ ),
+ array(
+ new TestUser('foo'), $user,
+ ),
+ array(
+ new TestUser('foo'), $advancedUser,
+ ),
+ array(
+ $user, $advancedUser
+ ),
+ array(
+ $advancedUser, $user
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getUsers
+ */
+ public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
+ {
+ $token = $this->getToken();
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($user);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($user);
+ $this->assertTrue($token->isAuthenticated());
+ }
+
+ protected function getToken(array $roles = array())
+ {
+ return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Token\AbstractToken', array($roles));
+ }
+}