EmailUserProviderTest.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace FOS\UserBundle\Tests\Security;
  3. use FOS\UserBundle\Security\EmailUserProvider;
  4. class EmailUserProviderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @var \PHPUnit_Framework_MockObject_MockObject
  8. */
  9. private $userManager;
  10. /**
  11. * @var EmailUserProvider
  12. */
  13. private $userProvider;
  14. protected function setUp()
  15. {
  16. $this->userManager = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  17. $this->userProvider = new EmailUserProvider($this->userManager);
  18. }
  19. public function testLoadUserByUsername()
  20. {
  21. $user = $this->getMock('FOS\UserBundle\Model\UserInterface');
  22. $this->userManager->expects($this->once())
  23. ->method('findUserByUsernameOrEmail')
  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('findUserByUsernameOrEmail')
  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\UnsupportedUserException
  56. */
  57. public function testRefreshInvalidUser()
  58. {
  59. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  60. $this->userProvider->refreshUser($user);
  61. }
  62. }