[DoctrineBridge] Enhance exception message in EntityUserProvider

Typo/CS fixes
Avoid checking Repository class as it is not always the default one
Compromise
Change wording
Tests this method
This commit is contained in:
Robin Chalas 2016-08-24 02:30:10 +02:00
parent d30c3184df
commit acc0460100
2 changed files with 60 additions and 1 deletions

View File

@ -51,7 +51,7 @@ class EntityUserProvider implements UserProviderInterface
$user = $repository->findOneBy(array($this->property => $username));
} else {
if (!$repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($repository)));
throw new \InvalidArgumentException(sprintf('You must either make the "%s" entity Doctrine Repository ("%s") implement "Symfony\Component\Security\Core\User\UserProviderInterface" or set the "property" option in the corresponding entity provider configuration.', $this->classOrAlias, get_class($repository)));
}
$user = $repository->loadUserByUsername($username);

View File

@ -38,6 +38,65 @@ class EntityUserProviderTest extends \PHPUnit_Framework_TestCase
$this->assertSame($user1, $provider->refreshUser($user1));
}
public function testLoadUserByUsername()
{
$em = DoctrineTestHelper::createTestEntityManager();
$this->createSchema($em);
$user = new User(1, 1, 'user1');
$em->persist($user);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
$this->assertSame($user, $provider->loadUserByUsername('user1'));
}
public function testLoadUserByUsernameWithUserProviderRepositoryAndWithoutProperty()
{
$user = new User(1, 1, 'user1');
$repository = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')
->disableOriginalConstructor()
->getMock();
$repository
->expects($this->once())
->method('loadUserByUsername')
->with('user1')
->willReturn($user);
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$em
->expects($this->once())
->method('getRepository')
->with('Symfony\Bridge\Doctrine\Tests\Fixtures\User')
->willReturn($repository);
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
$this->assertSame($user, $provider->loadUserByUsername('user1'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Component\Security\Core\User\UserProviderInterface" or set the "property" option in the corresponding entity provider configuration.
*/
public function testLoadUserByUsernameWithNonUserProviderRepositoryAndWithoutProperty()
{
$em = DoctrineTestHelper::createTestEntityManager();
$this->createSchema($em);
$user = new User(1, 1, 'user1');
$em->persist($user);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
$provider->loadUserByUsername('user1');
}
public function testRefreshUserRequiresId()
{
$em = DoctrineTestHelper::createTestEntityManager();