|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the FOSUserBundle package. |
|
5 * |
|
6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace FOS\UserBundle\Tests\Model; |
|
13 |
|
14 use FOS\UserBundle\Model\User; |
|
15 |
|
16 class UserTest extends \PHPUnit_Framework_TestCase |
|
17 { |
|
18 public function testUsername() |
|
19 { |
|
20 $user = $this->getUser(); |
|
21 $this->assertNull($user->getUsername()); |
|
22 |
|
23 $user->setUsername('tony'); |
|
24 $this->assertEquals('tony', $user->getUsername()); |
|
25 } |
|
26 |
|
27 public function testEmail() |
|
28 { |
|
29 $user = $this->getUser(); |
|
30 $this->assertNull($user->getEmail()); |
|
31 |
|
32 $user->setEmail('tony@mail.org'); |
|
33 $this->assertEquals('tony@mail.org', $user->getEmail()); |
|
34 } |
|
35 |
|
36 public function testIsPasswordRequestNonExpired() |
|
37 { |
|
38 $user = $this->getUser(); |
|
39 $passwordRequestedAt = new \DateTime('-10 seconds'); |
|
40 |
|
41 $user->setPasswordRequestedAt($passwordRequestedAt); |
|
42 |
|
43 $this->assertSame($passwordRequestedAt, $user->getPasswordRequestedAt()); |
|
44 $this->assertTrue($user->isPasswordRequestNonExpired(15)); |
|
45 $this->assertFalse($user->isPasswordRequestNonExpired(5)); |
|
46 } |
|
47 |
|
48 public function testTrueHasRole() |
|
49 { |
|
50 $user = $this->getUser(); |
|
51 $defaultrole = User::ROLE_DEFAULT; |
|
52 $newrole = 'ROLE_X'; |
|
53 $this->assertTrue($user->hasRole($defaultrole)); |
|
54 $user->addRole($defaultrole); |
|
55 $this->assertTrue($user->hasRole($defaultrole)); |
|
56 $user->addRole($newrole); |
|
57 $this->assertTrue($user->hasRole($newrole)); |
|
58 } |
|
59 |
|
60 public function testFalseHasRole() |
|
61 { |
|
62 $user = $this->getUser(); |
|
63 $newrole = 'ROLE_X'; |
|
64 $this->assertFalse($user->hasRole($newrole)); |
|
65 $user->addRole($newrole); |
|
66 $this->assertTrue($user->hasRole($newrole)); |
|
67 } |
|
68 |
|
69 public function testForEqualUsers() |
|
70 { |
|
71 $user1 = $this->getMockBuilder('FOS\UserBundle\Model\User')->setMethods(array('getSalt'))->getMock(); |
|
72 $user2 = $this->getMockBuilder('FOS\UserBundle\Model\User')->setMethods(array('getSalt'))->getMock(); |
|
73 $user3 = $this->getMockBuilder('FOS\UserBundle\Model\User')->setMethods(array('getSalt'))->getMock(); |
|
74 |
|
75 $salt1 = $salt3 = 'xxxx'; |
|
76 $salt2 = 'yyyy'; |
|
77 |
|
78 $user2->expects($this->once()) |
|
79 ->method('getSalt') |
|
80 ->will($this->returnValue($salt2)); |
|
81 |
|
82 $user1->expects($this->any()) |
|
83 ->method('getSalt') |
|
84 ->will($this->returnValue($salt1)); |
|
85 |
|
86 $user3->expects($this->once()) |
|
87 ->method('getSalt') |
|
88 ->will($this->returnValue($salt3)); |
|
89 |
|
90 $this->assertFalse($user1->equals($user2)); |
|
91 $this->assertTrue($user1->equals($user1)); |
|
92 $this->assertTrue($user1->equals($user3)); |
|
93 } |
|
94 |
|
95 protected function getUser() |
|
96 { |
|
97 return $this->getMockForAbstractClass('FOS\UserBundle\Model\User'); |
|
98 } |
|
99 } |