UserManagerTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }