10000 feature #9782 [Security] added string representation for core Users (… · symfony/symfony@542f604 · GitHub
[go: up one dir, main page]

Skip to content

Commit 542f604

Browse files
committed
feature #9782 [Security] added string representation for core Users (tobiassjosten)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #9782). Discussion ---------- [Security] added string representation for core Users | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | I often use type casting and `__toString()` to print my users as this lets me very easily keep one canonical format for their representation. In functional tests however, it is easier to use in-memory users but this defaults to instances of the core `User` object. Because these don't have a string representation all the nice type casting crashes. Hence I propose to represent the core Users by their username string by default. It would be useful in a lot of cases and I can't see any harm in it? Commits ------- 722c3a7 [Security] added string representation for core Users
2 parents 1882cb1 + 722c3a7 commit 542f604

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Symfony/Component/Security/Core/Tests/User/UserTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,13 @@ public function testEraseCredentials()
123123
$user->eraseCredentials();
124124
$this->assertEquals('superpass', $user->getPassword());
125125
}
126+
127+
/**
128+
* @covers Symfony\Component\Security\Core\User\User::__toString
129+
*/
130+
public function testToString()
131+
{
132+
$user = new User('fabien', 'superpass');
133+
$this->assertEquals('fabien', (string) $user);
134+
}
126135
}

src/Symfony/Component/Security/Core/User/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function __construct($username, $password, array $roles = array(), $enabl
4343
$this->roles = $roles;
4444
}
4545

46+
public function __toString()
47+
{
48+
return $this->getUsername();
49+
}
50+
4651
/**
4752
* {@inheritdoc}
4853
*/

0 commit comments

Comments
 (0)
0