EntityUserProviderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. public function testSupportProxy()
  60. {
  61. $em = $this->createTestEntityManager();
  62. $this->createSchema($em);
  63. $user1 = new CompositeIdentEntity(1, 1, 'user1');
  64. $em->persist($user1);
  65. $em->flush();
  66. $em->clear();
  67. $provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
  68. $user2 = $em->getReference('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', array('id1' => 1, 'id2' => 1));
  69. $this->assertTrue($provider->supportsClass(get_class($user2)));
  70. }
  71. private function createSchema($em)
  72. {
  73. $schemaTool = new SchemaTool($em);
  74. $schemaTool->createSchema(array(
  75. $em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
  76. ));
  77. }
  78. }