Skip to content

Commit 6bfb42e

Browse files
committed
minor #19720 [DoctrineBridge] Enhance exception message in EntityUserProvider (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [DoctrineBridge] Enhance exception message in EntityUserProvider | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Lots of people use the `UserEntityProvider` without having a custom Repository for the user entity configured on the entity provider and in this case, if the `property` key of the provider isn't set, the exception thrown says: > The Doctrine repository "Doctrine\ORM\EntityRepository" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface "Doctrine\ORM\EntityRepository" doesn't feel relevant. Plus, we can't guess that the exception is thrown first because there is no `property` configured on the corresponding provider, that is useful to have in the trace IMHO. If accepted, `"Symfony\Component\Security\Core\User\UserProviderInterface"` will need to be replaced by `"Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface"` when merging in newer branches. Commits ------- acc0460 [DoctrineBridge] Enhance exception message in EntityUserProvider
2 parents 4030c03 + acc0460 commit 6bfb42e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function loadUserByUsername($username)
5151
$user = $repository->findOneBy(array($this->property => $username));
5252
} else {
5353
if (!$repository instanceof UserProviderInterface) {
54-
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($repository)));
54+
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)));
5555
}
5656

5757
$user = $repository->loadUserByUsername($username);

src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,65 @@ public function testRefreshUserGetsUserByPrimaryKey()
3838
$this->assertSame($user1, $provider->refreshUser($user1));
3939
}
4040

41+
public function testLoadUserByUsername()
42+
{
43+
$em = DoctrineTestHelper::createTestEntityManager();
44+
$this->createSchema($em);
45+
46+
$user = new User(1, 1, 'user1');
47+
48+
$em->persist($user);
49+
$em->flush();
50+
51+
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
52+
53+
$this->assertSame($user, $provider->loadUserByUsername('user1'));
54+
}
55+
56+
public function testLoadUserByUsernameWithUserProviderRepositoryAndWithoutProperty()
57+
{
58+
$user = new User(1, 1, 'user1');
59+
60+
$repository = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')
61+
->disableOriginalConstructor()
62+
->getMock();
63+
$repository
64+
->expects($this->once())
65+
->method('loadUserByUsername')
66+
->with('user1')
67+
->willReturn($user);
68+
69+
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
70+
->disableOriginalConstructor()
71+
->getMock();
72+
$em
73+
->expects($this->once())
74+
->method('getRepository')
75+
->with('Symfony\Bridge\Doctrine\Tests\Fixtures\User')
76+
->willReturn($repository);
77+
78+
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
79+
$this->assertSame($user, $provider->loadUserByUsername('user1'));
80+
}
81+
82+
/**
83+
* @expectedException \InvalidArgumentException
84+
* @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.
85+
*/
86+
public function testLoadUserByUsernameWithNonUserProviderRepositoryAndWithoutProperty()
87+
{
88+
$em = DoctrineTestHelper::createTestEntityManager();
89+
$this->createSchema($em);
90+
91+
$user = new User(1, 1, 'user1');
92+
93+
$em->persist($user);
94+
$em->flush();
95+
96+
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
97+
$provider->loadUserByUsername('user1');
98+
}
99+
41100
public function testRefreshUserRequiresId()
42101
{
43102
$em = DoctrineTestHelper::createTestEntityManager();

0 commit comments

Comments
 (0)