EntityUserProviderTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Tests\Bridge\Doctrine\Security\User;
  11. require_once __DIR__.'/../../DoctrineOrmTestCase.php';
  12. require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
  13. use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
  14. use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity;
  15. use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
  16. use Doctrine\ORM\Tools\SchemaTool;
  17. class EntityUserProviderTest extends DoctrineOrmTestCase
  18. {
  19. public function testRefreshUserGetsUserByPrimaryKey()
  20. {
  21. $em = $this->createTestEntityManager();
  22. $this->createSchema($em);
  23. $user1 = new CompositeIdentEntity(1, 1, 'user1');
  24. $user2 = new CompositeIdentEntity(1, 2, 'user2');
  25. $em->persist($user1);
  26. $em->persist($user2);
  27. $em->flush();
  28. $provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
  29. // try to change the user identity
  30. $user1->name = 'user2';
  31. $this->assertSame($user1, $provider->refreshUser($user1));
  32. }
  33. public function testRefreshUserRequiresId()
  34. {
  35. $em = $this->createTestEntityManager();
  36. $user1 = new CompositeIdentEntity(null, null, 'user1');
  37. $provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
  38. $this->setExpectedException(
  39. 'InvalidArgumentException',
  40. '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'
  41. );
  42. $provider->refreshUser($user1);
  43. }
  44. public function testRefreshInvalidUser()
  45. {
  46. $em = $this->createTestEntityManager();
  47. $this->createSchema($em);
  48. $user1 = new CompositeIdentEntity(1, 1, 'user1');
  49. $em->persist($user1);
  50. $em->flush();
  51. $provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
  52. $user2 = new CompositeIdentEntity(1, 2, 'user2');
  53. $this->setExpectedException(
  54. 'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
  55. 'User with id {"id1":1,"id2":2} not found'
  56. );
  57. $provider->refreshUser($user2);
  58. }
  59. private function createSchema($em)
  60. {
  61. $schemaTool = new SchemaTool($em);
  62. $schemaTool->createSchema(array(
  63. $em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
  64. ));
  65. }
  66. }