Ver código fonte

[Security] added some missing unit tests

Fabien Potencier 14 anos atrás
pai
commit
a19cdce1bc

+ 3 - 1
src/Symfony/Component/Security/Authentication/Token/Token.php

@@ -71,8 +71,10 @@ abstract class Token implements TokenInterface
     {
         if (!is_object($this->user)) {
             return (string) $this->user;
-        } else {
+        } elseif ($this->user instanceof AccountInterface) {
             return $this->user->getUsername();
+        } else {
+            return 'n/a';
         }
     }
 

+ 47 - 0
tests/Symfony/Tests/Component/Security/Authentication/Token/TokenTest.php

@@ -15,10 +15,57 @@ use Symfony\Component\Security\Role\Role;
 
 class Token extends BaseToken
 {
+    public function setUser($user)
+    {
+        $this->user = $user;
+    }
+
+    public function setCredentials($credentials)
+    {
+        $this->credentials = $credentials;
+    }
 }
 
 class TokenTest extends \PHPUnit_Framework_TestCase
 {
+    public function testMagicToString()
+    {
+        $token = new Token(array('ROLE_FOO'));
+        $token->setUser('fabien');
+        $this->assertEquals('fabien', (string) $token);
+
+        $token->setUser(new \stdClass('fabien'));
+        $this->assertEquals('n/a', (string) $token);
+
+        $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
+        $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
+
+        $token->setUser($user);
+        $this->assertEquals('fabien', (string) $token);
+    }
+
+    public function testEraseCredentials()
+    {
+        $token = new Token(array('ROLE_FOO'));
+
+        $credentials = $this->getMock('Symfony\Component\Security\User\AccountInterface');
+        $credentials->expects($this->once())->method('eraseCredentials');
+        $token->setCredentials($credentials);
+
+        $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
+        $user->expects($this->once())->method('eraseCredentials');
+        $token->setUser($user);
+
+        $token->eraseCredentials();
+    }
+
+    public function testSerialize()
+    {
+        $token = new Token(array('ROLE_FOO'));
+
+        $this->assertEquals($token, unserialize(serialize($token)));
+    }
+
     /**
      * @covers Symfony\Component\Security\Authentication\Token\Token::__construct
      */

+ 6 - 0
tests/Symfony/Tests/Component/Security/Authorization/Voter/AuthenticatedVoterTest.php

@@ -16,6 +16,12 @@ use Symfony\Component\Security\Role\Role;
 
 class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase
 {
+    public function testSupportsClass()
+    {
+        $voter = new AuthenticatedVoter();
+        $this->assertTrue($voter->supportsClass('stdClass'));
+    }
+
     /**
      * @dataProvider getVoteTests
      */