Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\AttributesBasedUserProviderInterface;
use Symfony\Component\Security\Core\User\ChainUserProvider;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class ChainUserProviderTest extends TestCase
{
Expand All @@ -45,6 +47,28 @@ public function testLoadUserByIdentifier()
$this->assertSame($account, $provider->loadUserByIdentifier('foo'));
}

public function testLoadUserByIdentifierWithAttributes()
{
$provider1 = $this->createMock(UserProviderInterface::class);
$provider1
->expects($this->once())
->method('loadUserByIdentifier')
->with($this->equalTo('foo'))
->willThrowException(new UserNotFoundException('not found'))
;

$provider2 = $this->createMock(AttributesBasedUserProviderInterface::class);
$provider2
->expects($this->once())
->method('loadUserByIdentifier')
->with($this->equalTo('foo'), $this->equalTo(['attr' => 5]))
->willReturn($account = $this->createMock(UserInterface::class))
;

$provider = new ChainUserProvider([$provider1, $provider2]);
$this->assertSame($account, $provider->loadUserByIdentifier('foo', ['attr' => 5]));
}

public function testLoadUserByIdentifierThrowsUserNotFoundException()
{
$this->expectException(UserNotFoundException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ public function loadUserByUsername(string $username): UserInterface
return $this->loadUserByIdentifier($username);
}

public function loadUserByIdentifier(string $identifier): UserInterface
/**
* @param array $attributes
*/
public function loadUserByIdentifier(string $identifier/* , array $attributes = [] */): UserInterface
{
$attributes = \func_num_args() > 1 ? func_get_arg(1) : [];
foreach ($this->providers as $provider) {
try {
if ($provider instanceof AttributesBasedUserProviderInterface) {
return $provider->loadUserByIdentifier($identifier, $attributes);
}

return $provider->loadUserByIdentifier($identifier);
} catch (UserNotFoundException) {
// try next one
Expand Down
Loading