UserProviderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace FOS\UserBundle\Tests\Security;
  3. use FOS\UserBundle\Security\UserProvider;
  4. class UserProviderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @var \PHPUnit_Framework_MockObject_MockObject
  8. */
  9. private $userManager;
  10. /**
  11. * @var UserProvider
  12. */
  13. private $userProvider;
  14. protected function setUp()
  15. {
  16. $this->userManager = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  17. $this->userProvider = new UserProvider($this->userManager);
  18. }
  19. public function testLoadUserByUsername()
  20. {
  21. $user = $this->getMock('FOS\UserBundle\Model\UserInterface');
  22. $this->userManager->expects($this->once())
  23. ->method('findUserByUsername')
  24. ->with('foobar')
  25. ->will($this->returnValue($user));
  26. $this->assertSame($user, $this->userProvider->loadUserByUsername('foobar'));
  27. }
  28. /**
  29. * @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException
  30. */
  31. public function testLoadUserByInvalidUsername()
  32. {
  33. $this->userManager->expects($this->once())
  34. ->method('findUserByUsername')
  35. ->with('foobar')
  36. ->will($this->returnValue(null));
  37. $this->userProvider->loadUserByUsername('foobar');
  38. }
  39. public function testRefreshUserBy()
  40. {
  41. $user = $this->getMockBuilder('FOS\UserBundle\Model\User')
  42. ->setMethods(array('getId'))
  43. ->getMock();
  44. $user->expects($this->once())
  45. ->method('getId')
  46. ->will($this->returnValue('123'));
  47. $refreshedUser = $this->getMock('FOS\UserBundle\Model\UserInterface');
  48. $this->userManager->expects($this->once())
  49. ->method('findUserBy')
  50. ->with(array('id' => '123'))
  51. ->will($this->returnValue($refreshedUser));
  52. $this->userManager->expects($this->atLeastOnce())
  53. ->method('getClass')
  54. ->will($this->returnValue(get_class($user)));
  55. $this->assertSame($refreshedUser, $this->userProvider->refreshUser($user));
  56. }
  57. /**
  58. * @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException
  59. */
  60. public function testRefreshDeleted()
  61. {
  62. $user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
  63. $this->userManager->expects($this->once())
  64. ->method('findUserBy')
  65. ->will($this->returnValue(null));
  66. $this->userManager->expects($this->atLeastOnce())
  67. ->method('getClass')
  68. ->will($this->returnValue(get_class($user)));
  69. $this->userProvider->refreshUser($user);
  70. }
  71. /**
  72. * @expectedException Symfony\Component\Security\Core\Exception\UnsupportedUserException
  73. */
  74. public function testRefreshInvalidUser()
  75. {
  76. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  77. $this->userManager->expects($this->any())
  78. ->method('getClass')
  79. ->will($this->returnValue(get_class($user)));
  80. $this->userProvider->refreshUser($user);
  81. }
  82. /**
  83. * @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
  84. */
  85. public function testRefreshInvalidUserClass()
  86. {
  87. $user = $this->getMock('FOS\UserBundle\Model\User');
  88. $providedUser = $this->getMock('FOS\UserBundle\Tests\TestUser');
  89. $this->userManager->expects($this->atLeastOnce())
  90. ->method('getClass')
  91. ->will($this->returnValue(get_class($user)));
  92. $this->userProvider->refreshUser($providedUser);
  93. }
  94. }