UserProviderTest.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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->assertSame($refreshedUser, $this->userProvider->refreshUser($user));
  53. }
  54. /**
  55. * @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException
  56. */
  57. public function testRefreshDeleted()
  58. {
  59. $user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
  60. $this->userManager->expects($this->once())
  61. ->method('findUserBy')
  62. ->will($this->returnValue(null));
  63. $this->userProvider->refreshUser($user);
  64. }
  65. /**
  66. * @expectedException Symfony\Component\Security\Core\Exception\UnsupportedUserException
  67. */
  68. public function testRefreshInvalidUser()
  69. {
  70. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  71. $this->userProvider->refreshUser($user);
  72. }
  73. }