UserTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Tests\Model;
  11. use FOS\UserBundle\Model\User;
  12. class UserTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testUsername()
  15. {
  16. $user = $this->getUser();
  17. $this->assertNull($user->getUsername());
  18. $user->setUsername('tony');
  19. $this->assertEquals('tony', $user->getUsername());
  20. }
  21. public function testEmail()
  22. {
  23. $user = $this->getUser();
  24. $this->assertNull($user->getEmail());
  25. $user->setEmail('tony@mail.org');
  26. $this->assertEquals('tony@mail.org', $user->getEmail());
  27. }
  28. public function testIsPasswordRequestNonExpired()
  29. {
  30. $user = $this->getUser();
  31. $passwordRequestedAt = new \DateTime('-10 seconds');
  32. $user->setPasswordRequestedAt($passwordRequestedAt);
  33. $this->assertSame($passwordRequestedAt, $user->getPasswordRequestedAt());
  34. $this->assertTrue($user->isPasswordRequestNonExpired(15));
  35. $this->assertFalse($user->isPasswordRequestNonExpired(5));
  36. }
  37. public function testIsPasswordRequestAtCleared()
  38. {
  39. $user = $this->getUser();
  40. $passwordRequestedAt = new \DateTime('-10 seconds');
  41. $user->setPasswordRequestedAt($passwordRequestedAt);
  42. $user->setPasswordRequestedAt(null);
  43. $this->assertFalse($user->isPasswordRequestNonExpired(15));
  44. $this->assertFalse($user->isPasswordRequestNonExpired(5));
  45. }
  46. public function testTrueHasRole()
  47. {
  48. $user = $this->getUser();
  49. $defaultrole = User::ROLE_DEFAULT;
  50. $newrole = 'ROLE_X';
  51. $this->assertTrue($user->hasRole($defaultrole));
  52. $user->addRole($defaultrole);
  53. $this->assertTrue($user->hasRole($defaultrole));
  54. $user->addRole($newrole);
  55. $this->assertTrue($user->hasRole($newrole));
  56. }
  57. public function testFalseHasRole()
  58. {
  59. $user = $this->getUser();
  60. $newrole = 'ROLE_X';
  61. $this->assertFalse($user->hasRole($newrole));
  62. $user->addRole($newrole);
  63. $this->assertTrue($user->hasRole($newrole));
  64. }
  65. /**
  66. * @return User
  67. */
  68. protected function getUser()
  69. {
  70. return $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
  71. }
  72. }