BaseUserTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\UserBundle\Tests\Document;
  11. use Sonata\UserBundle\Document\BaseUser;
  12. class BaseUserTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testDateSetters()
  15. {
  16. // Given
  17. $user = new BaseUser();
  18. $today = new \DateTime();
  19. // When
  20. $user->setCreatedAt($today);
  21. $user->setUpdatedAt($today);
  22. $user->setCredentialsExpireAt($today);
  23. // Then
  24. $this->assertTrue($user->getCreatedAt() instanceof \DateTime, 'Should return a DateTime object');
  25. $this->assertEquals($today->format('U'), $user->getCreatedAt()->format('U') , 'Should contain today\'s date');
  26. $this->assertTrue($user->getUpdatedAt() instanceof \DateTime, 'Should return a DateTime object');
  27. $this->assertEquals($today->format('U'), $user->getUpdatedAt()->format('U') , 'Should contain today\'s date');
  28. $this->assertTrue($user->getCredentialsExpireAt() instanceof \DateTime, 'Should return a DateTime object');
  29. $this->assertEquals($today->format('U'), $user->getCredentialsExpireAt()->format('U') , 'Should contain today\'s date');
  30. }
  31. public function testDateWithPrePersist()
  32. {
  33. // Given
  34. $user = new BaseUser();
  35. $today = new \DateTime();
  36. // When
  37. $user->prePersist();
  38. // Then
  39. $this->assertTrue($user->getCreatedAt() instanceof \DateTime, 'Should contain a DateTime object');
  40. $this->assertEquals($today->format('Y-m-d'), $user->getUpdatedAt()->format('Y-m-d'), 'Should be created today');
  41. $this->assertTrue($user->getUpdatedAt() instanceof \DateTime, 'Should contain a DateTime object');
  42. $this->assertEquals($today->format('Y-m-d'), $user->getUpdatedAt()->format('Y-m-d'), 'Should be updated today');
  43. }
  44. public function testDateWithPreUpdate()
  45. {
  46. // Given
  47. $user = new BaseUser();
  48. $user->setCreatedAt( \DateTime::createFromFormat('Y-m-d', '2012-01-01'));
  49. $today = new \DateTime();
  50. // When
  51. $user->preUpdate();
  52. // Then
  53. $this->assertTrue($user->getCreatedAt() instanceof \DateTime, 'Should contain a DateTime object');
  54. $this->assertEquals('2012-01-01', $user->getCreatedAt()->format('Y-m-d'), 'Should be created at 2012-01-01.');
  55. $this->assertTrue($user->getUpdatedAt() instanceof \DateTime, 'Should contain a DateTime object');
  56. $this->assertEquals($today->format('Y-m-d'), $user->getUpdatedAt()->format('Y-m-d'), 'Should be updated today');
  57. }
  58. public function testSettingMultipleGroups()
  59. {
  60. // Given
  61. $user = new BaseUser();
  62. $group1 = $this->getMock('FOS\UserBundle\Model\GroupInterface');
  63. $group1->expects($this->any())->method('getName')->will($this->returnValue('Group 1'));
  64. $group2 = $this->getMock('FOS\UserBundle\Model\GroupInterface');
  65. $group2->expects($this->any())->method('getName')->will($this->returnValue('Group 2'));
  66. // When
  67. $user->setGroups(array($group1, $group2));
  68. // Then
  69. $this->assertCount(2, $user->getGroups(), 'Should have 2 groups');
  70. $this->assertTrue($user->hasGroup('Group 1'), 'Should have a group named "Group 1"');
  71. $this->assertTrue($user->hasGroup('Group 2'), 'Should have a group named "Group 2"');
  72. }
  73. public function testTwoStepVerificationCode()
  74. {
  75. // Given
  76. $user = new BaseUser();
  77. // When
  78. $user->setTwoStepVerificationCode('123456');
  79. // Then
  80. $this->assertEquals('123456', $user->getTwoStepVerificationCode(), 'Should return the two step verification code');
  81. }
  82. public function testToStringWithName()
  83. {
  84. // Given
  85. $user = new BaseUser();
  86. $user->setUsername('John');
  87. // When
  88. $string = (string)$user;
  89. // Then
  90. $this->assertEquals('John', $string, 'Should return the username as string representation');
  91. }
  92. public function testToStringWithoutName()
  93. {
  94. // Given
  95. $user = new BaseUser();
  96. // When
  97. $string = (string)$user;
  98. // Then
  99. $this->assertEquals('-', $string, 'Should return a string representation');
  100. }
  101. }