EntityUserProviderTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests\Security\User;
  11. use Doctrine\ORM\Tools\SchemaTool;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
  14. use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
  15. use Symfony\Bridge\Doctrine\Tests\Fixtures\User;
  16. class EntityUserProviderTest extends TestCase
  17. {
  18. public function testRefreshUserGetsUserByPrimaryKey()
  19. {
  20. $em = DoctrineTestHelper::createTestEntityManager();
  21. $this->createSchema($em);
  22. $user1 = new User(1, 1, 'user1');
  23. $user2 = new User(1, 2, 'user2');
  24. $em->persist($user1);
  25. $em->persist($user2);
  26. $em->flush();
  27. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
  28. // try to change the user identity
  29. $user1->name = 'user2';
  30. $this->assertSame($user1, $provider->refreshUser($user1));
  31. }
  32. public function testLoadUserByUsername()
  33. {
  34. $em = DoctrineTestHelper::createTestEntityManager();
  35. $this->createSchema($em);
  36. $user = new User(1, 1, 'user1');
  37. $em->persist($user);
  38. $em->flush();
  39. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
  40. $this->assertSame($user, $provider->loadUserByUsername('user1'));
  41. }
  42. public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty()
  43. {
  44. $user = new User(1, 1, 'user1');
  45. $repository = $this->getMockBuilder('Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $repository
  49. ->expects($this->once())
  50. ->method('loadUserByUsername')
  51. ->with('user1')
  52. ->willReturn($user);
  53. $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $em
  57. ->expects($this->once())
  58. ->method('getRepository')
  59. ->with('Symfony\Bridge\Doctrine\Tests\Fixtures\User')
  60. ->willReturn($repository);
  61. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
  62. $this->assertSame($user, $provider->loadUserByUsername('user1'));
  63. }
  64. /**
  65. * @group legacy
  66. * @expectedDeprecation Implementing Symfony\Component\Security\Core\User\UserProviderInterface in a Doctrine repository when using the entity provider is deprecated since Symfony 2.8 and will not be supported in 3.0. Make the repository implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead.
  67. */
  68. public function testLoadUserByUsernameWithUserProviderRepositoryAndWithoutProperty()
  69. {
  70. $user = new User(1, 1, 'user1');
  71. $repository = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $repository
  75. ->expects($this->once())
  76. ->method('loadUserByUsername')
  77. ->with('user1')
  78. ->willReturn($user);
  79. $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $em
  83. ->expects($this->once())
  84. ->method('getRepository')
  85. ->with('Symfony\Bridge\Doctrine\Tests\Fixtures\User')
  86. ->willReturn($repository);
  87. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
  88. $this->assertSame($user, $provider->loadUserByUsername('user1'));
  89. }
  90. /**
  91. * @expectedException \InvalidArgumentException
  92. * @expectedExceptionMessage You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.
  93. */
  94. public function testLoadUserByUsernameWithNonUserLoaderRepositoryAndWithoutProperty()
  95. {
  96. $em = DoctrineTestHelper::createTestEntityManager();
  97. $this->createSchema($em);
  98. $user = new User(1, 1, 'user1');
  99. $em->persist($user);
  100. $em->flush();
  101. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
  102. $provider->loadUserByUsername('user1');
  103. }
  104. public function testRefreshUserRequiresId()
  105. {
  106. $em = DoctrineTestHelper::createTestEntityManager();
  107. $user1 = new User(null, null, 'user1');
  108. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
  109. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
  110. 'InvalidArgumentException',
  111. 'You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine'
  112. );
  113. $provider->refreshUser($user1);
  114. }
  115. public function testRefreshInvalidUser()
  116. {
  117. $em = DoctrineTestHelper::createTestEntityManager();
  118. $this->createSchema($em);
  119. $user1 = new User(1, 1, 'user1');
  120. $em->persist($user1);
  121. $em->flush();
  122. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
  123. $user2 = new User(1, 2, 'user2');
  124. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
  125. 'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
  126. 'User with id {"id1":1,"id2":2} not found'
  127. );
  128. $provider->refreshUser($user2);
  129. }
  130. public function testSupportProxy()
  131. {
  132. $em = DoctrineTestHelper::createTestEntityManager();
  133. $this->createSchema($em);
  134. $user1 = new User(1, 1, 'user1');
  135. $em->persist($user1);
  136. $em->flush();
  137. $em->clear();
  138. $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
  139. $user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\User', array('id1' => 1, 'id2' => 1));
  140. $this->assertTrue($provider->supportsClass(\get_class($user2)));
  141. }
  142. public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided()
  143. {
  144. $repository = $this->getMockBuilder('\Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface')->getMock();
  145. $repository->expects($this->once())
  146. ->method('loadUserByUsername')
  147. ->with('name')
  148. ->willReturn(
  149. $this->getMockBuilder('\Symfony\Component\Security\Core\User\UserInterface')->getMock()
  150. );
  151. $provider = new EntityUserProvider(
  152. $this->getManager($this->getObjectManager($repository)),
  153. 'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
  154. );
  155. $provider->loadUserByUsername('name');
  156. }
  157. /**
  158. * @expectedException \InvalidArgumentException
  159. */
  160. public function testLoadUserByUserNameShouldDeclineInvalidInterface()
  161. {
  162. $repository = $this->getMockBuilder('\Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();
  163. $provider = new EntityUserProvider(
  164. $this->getManager($this->getObjectManager($repository)),
  165. 'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
  166. );
  167. $provider->loadUserByUsername('name');
  168. }
  169. private function getManager($em, $name = null)
  170. {
  171. $manager = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
  172. $manager->expects($this->any())
  173. ->method('getManager')
  174. ->with($this->equalTo($name))
  175. ->will($this->returnValue($em));
  176. return $manager;
  177. }
  178. private function getObjectManager($repository)
  179. {
  180. $em = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
  181. ->setMethods(array('getClassMetadata', 'getRepository'))
  182. ->getMockForAbstractClass();
  183. $em->expects($this->any())
  184. ->method('getRepository')
  185. ->willReturn($repository);
  186. return $em;
  187. }
  188. private function createSchema($em)
  189. {
  190. $schemaTool = new SchemaTool($em);
  191. $schemaTool->createSchema(array(
  192. $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\User'),
  193. ));
  194. }
  195. }