EntityUserProviderTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. private function createSchema($em)
  45. {
  46. $schemaTool = new SchemaTool($em);
  47. $schemaTool->createSchema(array(
  48. $em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
  49. ));
  50. }
  51. }