|
3
|
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\Document; |
|
|
13 |
|
|
|
14 |
class DocumentUserManagerTest extends \PHPUnit_Framework_TestCase |
|
|
15 |
{ |
|
|
16 |
protected $userManager; |
|
|
17 |
|
|
|
18 |
public function testFindUserByUsername() |
|
|
19 |
{ |
|
|
20 |
$this->userManager->expects($this->once()) |
|
|
21 |
->method('findUserBy') |
|
|
22 |
->with($this->equalTo(array('usernameCanonical' => 'jack'))); |
|
|
23 |
$this->userManager->expects($this->once()) |
|
|
24 |
->method('canonicalizeUsername') |
|
|
25 |
->with($this->equalTo('jack')) |
|
|
26 |
->will($this->returnValue('jack')); |
|
|
27 |
|
|
|
28 |
$this->userManager->findUserByUsername('jack'); |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
public function testFindUserByUsernameLowercasesTheUsername() |
|
|
32 |
{ |
|
|
33 |
$this->userManager->expects($this->once()) |
|
|
34 |
->method('findUserBy') |
|
|
35 |
->with($this->equalTo(array('usernameCanonical' => 'jack'))); |
|
|
36 |
$this->userManager->expects($this->once()) |
|
|
37 |
->method('canonicalizeUsername') |
|
|
38 |
->with($this->equalTo('JaCk')) |
|
|
39 |
->will($this->returnValue('jack')); |
|
|
40 |
|
|
|
41 |
$this->userManager->findUserByUsername('JaCk'); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
public function testFindUserByEmail() |
|
|
45 |
{ |
|
|
46 |
$this->userManager->expects($this->once()) |
|
|
47 |
->method('findUserBy') |
|
|
48 |
->with($this->equalTo(array('emailCanonical' => 'jack@email.org'))); |
|
|
49 |
$this->userManager->expects($this->once()) |
|
|
50 |
->method('canonicalizeEmail') |
|
|
51 |
->with($this->equalTo('jack@email.org')) |
|
|
52 |
->will($this->returnValue('jack@email.org')); |
|
|
53 |
|
|
|
54 |
$this->userManager->findUserByEmail('jack@email.org'); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
public function testFindUserByEmailLowercasesTheEmail() |
|
|
58 |
{ |
|
|
59 |
$this->userManager->expects($this->once()) |
|
|
60 |
->method('findUserBy') |
|
|
61 |
->with($this->equalTo(array('emailCanonical' => 'jack@email.org'))); |
|
|
62 |
$this->userManager->expects($this->once()) |
|
|
63 |
->method('canonicalizeEmail') |
|
|
64 |
->with($this->equalTo('JaCk@EmAiL.oRg')) |
|
|
65 |
->will($this->returnValue('jack@email.org')); |
|
|
66 |
|
|
|
67 |
$this->userManager->findUserByEmail('JaCk@EmAiL.oRg'); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
public function testFindUserByUsernameOrEmailWithUsername() |
|
|
71 |
{ |
|
|
72 |
$this->userManager->expects($this->once()) |
|
|
73 |
->method('findUserBy') |
|
|
74 |
->with($this->equalTo(array('usernameCanonical' => 'jack'))); |
|
|
75 |
$this->userManager->expects($this->once()) |
|
|
76 |
->method('canonicalizeUsername') |
|
|
77 |
->with($this->equalTo('JaCk')) |
|
|
78 |
->will($this->returnValue('jack')); |
|
|
79 |
|
|
|
80 |
$this->userManager->findUserByUsernameOrEmail('JaCk'); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
public function testFindUserByUsernameOrEmailWithEmail() |
|
|
84 |
{ |
|
|
85 |
$this->userManager->expects($this->once()) |
|
|
86 |
->method('findUserBy') |
|
|
87 |
->with($this->equalTo(array('emailCanonical' => 'jack@email.org'))); |
|
|
88 |
$this->userManager->expects($this->once()) |
|
|
89 |
->method('canonicalizeEmail') |
|
|
90 |
->with($this->equalTo('JaCk@EmAiL.oRg')) |
|
|
91 |
->will($this->returnValue('jack@email.org')); |
|
|
92 |
|
|
|
93 |
$this->userManager->findUserByUsernameOrEmail('JaCk@EmAiL.oRg'); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
public function testLoadUserByUsernameWithExistingUser() |
|
|
97 |
{ |
|
|
98 |
$userMock = $this->getMock('FOS\UserBundle\Document\User', array(), array('sha1')); |
|
|
99 |
|
|
|
100 |
$manager = $this->getMockBuilder('FOS\UserBundle\Document\UserManager') |
|
|
101 |
->disableOriginalConstructor() |
|
|
102 |
->setMethods(array('findUserByUsername')) |
|
|
103 |
->getMock(); |
|
|
104 |
|
|
|
105 |
$manager->expects($this->once()) |
|
|
106 |
->method('findUserByUsername') |
|
|
107 |
->with($this->equalTo('jack')) |
|
|
108 |
->will($this->returnValue($userMock)); |
|
|
109 |
|
|
|
110 |
$manager->loadUserByUsername('jack'); |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
/** |
|
|
114 |
* @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
|
|
115 |
*/ |
|
|
116 |
public function testLoadUserByUsernameWithMissingUser() |
|
|
117 |
{ |
|
|
118 |
$manager = $this->getMockBuilder('FOS\UserBundle\Document\UserManager') |
|
|
119 |
->disableOriginalConstructor() |
|
|
120 |
->setMethods(array('findUserByUsername')) |
|
|
121 |
->getMock(); |
|
|
122 |
|
|
|
123 |
$manager->expects($this->once()) |
|
|
124 |
->method('findUserByUsername') |
|
|
125 |
->with($this->equalTo('jack')) |
|
|
126 |
->will($this->returnValue(null)); |
|
|
127 |
|
|
|
128 |
$manager->loadUserByUsername('jack'); |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
protected function setUp() |
|
|
132 |
{ |
|
|
133 |
if (!class_exists('\Doctrine\ODM\MongoDB\DocumentManager')) { |
|
|
134 |
$this->markTestSkipped('No ODM installed'); |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
$this->userManager = $this->getManagerMock(); |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
protected function tearDown() |
|
|
141 |
{ |
|
|
142 |
unset($this->userManager); |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
protected function getManagerMock() |
|
|
146 |
{ |
|
|
147 |
return $this->getMockBuilder('FOS\UserBundle\Document\UserManager') |
|
|
148 |
->disableOriginalConstructor() |
|
|
149 |
->setMethods(array('findUserBy', 'canonicalizeUsername', 'canonicalizeEmail')) |
|
|
150 |
->getMock(); |
|
|
151 |
} |
|
|
152 |
} |