Prechádzať zdrojové kódy

[Security] removed __toString() from AccountInterface

Johannes Schmitt 14 rokov pred
rodič
commit
66fbbd6b17

+ 6 - 6
src/Symfony/Component/Security/Core/Authentication/Token/Token.php

@@ -85,13 +85,13 @@ abstract class Token implements TokenInterface
      */
     public function __toString()
     {
-        if (!is_object($this->user)) {
-            return (string) $this->user;
-        } elseif ($this->user instanceof AccountInterface) {
+        if (is_string($this->user)) {
+            return $this->user;
+        } else if ($this->user instanceof AccountInterface) {
             return $this->user->getUsername();
-        } else {
-            return 'n/a';
         }
+
+        return (string) $this->user;
     }
 
     /**
@@ -141,7 +141,7 @@ abstract class Token implements TokenInterface
 
         if (!is_string($user) && !is_object($user)) {
             throw new \InvalidArgumentException('$user must be an object, or a primitive string.');
-        } else if (is_object($user) && !method_exists($user, '__toString')) {
+        } else if (is_object($user) && !$user instanceof AccountInterface && !method_exists($user, '__toString')) {
             throw new \InvalidArgumentException('If $user is an object, it must implement __toString().');
         }
 

+ 0 - 7
src/Symfony/Component/Security/Core/User/AccountInterface.php

@@ -18,13 +18,6 @@ namespace Symfony\Component\Security\Core\User;
  */
 interface AccountInterface
 {
-    /**
-     * Returns a string representation of the User.
-     *
-     * @return string A string return of the User
-     */
-    function __toString();
-
     /**
      * Returns the roles granted to the user.
      *

+ 1 - 1
tests/Symfony/Tests/Component/Security/Core/Authentication/Token/TokenTest.php

@@ -46,7 +46,7 @@ class TokenTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('fabien', (string) $token);
 
         $token->setUser(new TestUser('fabien'));
-        $this->assertEquals('n/a', (string) $token);
+        $this->assertEquals('fabien', (string) $token);
 
         $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
         $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));