UserManagerTest.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace FOS\UserBundle\Tests\Doctrine;
  3. use FOS\UserBundle\Doctrine\UserManager;
  4. use FOS\UserBundle\Model\User;
  5. class UserManagerTest extends \PHPUnit_Framework_TestCase
  6. {
  7. const USER_CLASS = 'FOS\UserBundle\Tests\Doctrine\DummyUser';
  8. protected $userManager;
  9. protected $om;
  10. protected $repository;
  11. public function setUp()
  12. {
  13. if (!interface_exists('Doctrine\Common\Persistence\ObjectManager')) {
  14. $this->markTestSkipped('Doctrine Common has to be installed for this test to run.');
  15. }
  16. $c = $this->getMock('FOS\UserBundle\Util\CanonicalizerInterface');
  17. $ef = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
  18. $class = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
  19. $this->om = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  20. $this->repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  21. $this->om->expects($this->any())
  22. ->method('getRepository')
  23. ->with($this->equalTo(static::USER_CLASS))
  24. ->will($this->returnValue($this->repository));
  25. $this->om->expects($this->any())
  26. ->method('getClassMetadata')
  27. ->with($this->equalTo(static::USER_CLASS))
  28. ->will($this->returnValue($class));
  29. $class->expects($this->any())
  30. ->method('getName')
  31. ->will($this->returnValue(static::USER_CLASS));
  32. $this->userManager = $this->createUserManager($ef, $c, $this->om, static::USER_CLASS);
  33. }
  34. public function testDeleteUser()
  35. {
  36. $user = $this->getUser();
  37. $this->om->expects($this->once())->method('remove')->with($this->equalTo($user));
  38. $this->om->expects($this->once())->method('flush');
  39. $this->userManager->deleteUser($user);
  40. }
  41. public function testGetClass()
  42. {
  43. $this->assertEquals(static::USER_CLASS, $this->userManager->getClass());
  44. }
  45. public function testFindUserBy()
  46. {
  47. $crit = array("foo" => "bar");
  48. $this->repository->expects($this->once())->method('findOneBy')->with($this->equalTo($crit))->will($this->returnValue(array()));
  49. $this->userManager->findUserBy($crit);
  50. }
  51. public function testFindUsers()
  52. {
  53. $this->repository->expects($this->once())->method('findAll')->will($this->returnValue(array()));
  54. $this->userManager->findUsers();
  55. }
  56. public function testUpdateUser()
  57. {
  58. $user = $this->getUser();
  59. $this->om->expects($this->once())->method('persist')->with($this->equalTo($user));
  60. $this->om->expects($this->once())->method('flush');
  61. $this->userManager->updateUser($user);
  62. }
  63. protected function createUserManager($encoderFactory, $canonicalizer, $objectManager, $userClass)
  64. {
  65. return new UserManager($encoderFactory, $canonicalizer, $canonicalizer, $objectManager, $userClass);
  66. }
  67. protected function getUser()
  68. {
  69. $userClass = static::USER_CLASS;
  70. return new $userClass();
  71. }
  72. }
  73. class DummyUser extends User
  74. {
  75. }