Skip to content

[Security] Adding __toString() method to Role class #10179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/Security/Core/Role/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ public function getRole()
{
return $this->role;
}

/**
* Returns a string representation of the role
*
* @return string
*/
public function __toString()
{
return $this->role;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will produce a fatal error if $this->role is null. It is allowed by the RoleInterface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot, will fix and add a test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests to ensure that the __toString() behaves as expected, although it was never possible for it to be null because the role was immutable and cast to a string in the constructor

}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Security/Tests/Core/Role/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ public function testGetRole()

$this->assertEquals('FOO', $role->getRole());
}

/**
* @dataProvider getToStringFixtures
*/
public function testToString($role, $expected)
{
$this->assertEquals($role, $expected);
}

public function getToStringFixtures()
{
return array(
array(new Role(null), ''),
array(new Role('FOO'), 'FOO')
);
}
}