These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace OpenOrchestra\FunctionalTests\UserAdminBundle\Repository; |
||
4 | |||
5 | use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractKernelTestCase; |
||
6 | use OpenOrchestra\Pagination\Configuration\PaginateFinderConfiguration; |
||
7 | use OpenOrchestra\UserBundle\Model\UserInterface; |
||
8 | use OpenOrchestra\UserBundle\Repository\UserRepository; |
||
9 | use OpenOrchestra\GroupBundle\Document\Group; |
||
10 | |||
11 | /** |
||
12 | * Class UserRepositoryTest |
||
13 | * |
||
14 | * @group integrationTest |
||
15 | */ |
||
16 | class UserRepositoryTest extends AbstractKernelTestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var UserRepository |
||
20 | */ |
||
21 | protected $repository; |
||
22 | |||
23 | /** |
||
24 | * Set up test |
||
25 | */ |
||
26 | View Code Duplication | protected function setUp() |
|
0 ignored issues
–
show
|
|||
27 | { |
||
28 | parent::setUp(); |
||
29 | |||
30 | static::bootKernel(); |
||
31 | $this->repository = static::$kernel->getContainer()->get('open_orchestra_user.repository.user'); |
||
32 | $this->groupRepository = static::$kernel->getContainer()->get('open_orchestra_user.repository.group'); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param PaginateFinderConfiguration $configuration |
||
37 | * @param int $count |
||
38 | * |
||
39 | * @dataProvider providePaginateAndSearch |
||
40 | */ |
||
41 | public function testFindForPaginate(PaginateFinderConfiguration $configuration, $count) |
||
42 | { |
||
43 | $users = $this->repository->findForPaginate($configuration); |
||
44 | $this->assertCount($count, $users); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | View Code Duplication | public function providePaginateAndSearch() |
|
51 | { |
||
52 | $configurationAll = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array()); |
||
53 | $configurationLimit = PaginateFinderConfiguration::generateFromVariable(array(), 0, 1, array()); |
||
54 | $configurationSearch = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array(), array('search' => 'demo')); |
||
55 | $configurationAllOrder = PaginateFinderConfiguration::generateFromVariable(array('username' => 'desc'), 0, 100, array()); |
||
56 | |||
57 | return array( |
||
58 | 'all' => array($configurationAll, 5), |
||
59 | 'limit' => array($configurationLimit, 1), |
||
60 | 'search' => array($configurationSearch, 2), |
||
61 | 'order' => array($configurationAllOrder, 5), |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param PaginateFinderConfiguration $configuration |
||
67 | * @param array $sitesId |
||
68 | * @param int $count |
||
69 | * |
||
70 | * @dataProvider providePaginateAndSearchWithSitesId |
||
71 | */ |
||
72 | public function testPaginateAndSearchWithSitesId(PaginateFinderConfiguration $configuration, array $sitesId, $count) |
||
73 | { |
||
74 | $sitesId = $this->convertSiteIdInMongoId($sitesId); |
||
75 | $users = $this->repository->findForPaginateFilterBySiteIds($configuration, $sitesId); |
||
76 | $this->assertCount($count, $users); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | View Code Duplication | public function providePaginateAndSearchWithSitesId() |
|
83 | { |
||
84 | $configurationAll = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array()); |
||
85 | $configurationLimit = PaginateFinderConfiguration::generateFromVariable(array(), 0, 1, array()); |
||
86 | $configurationSearch = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array(), array('search' => 'admin')); |
||
87 | $configurationAllOrder = PaginateFinderConfiguration::generateFromVariable(array('username' => 'desc'), 0, 100, array()); |
||
88 | |||
89 | return array( |
||
90 | array($configurationAll, array(), 0), |
||
91 | array($configurationAll, array('2'), 2), |
||
92 | array($configurationLimit, array('2'), 1), |
||
93 | array($configurationAllOrder, array('2'), 2), |
||
94 | array($configurationSearch, array('2'), 2), |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * test count all user |
||
100 | */ |
||
101 | public function testCount() |
||
102 | { |
||
103 | $users = $this->repository->count(); |
||
104 | $this->assertEquals(5, $users); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * test count all filter with site id |
||
109 | */ |
||
110 | public function testCountFilterBySiteId() |
||
111 | { |
||
112 | $sitesId = $this->convertSiteIdInMongoId(array('2')); |
||
113 | $users = $this->repository->countFilterBySiteIds($sitesId); |
||
114 | $this->assertEquals(2, $users); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param PaginateFinderConfiguration $configuration |
||
119 | * @param int $count |
||
120 | * |
||
121 | * @dataProvider provideCountWithFilter |
||
122 | */ |
||
123 | public function testCountWithFilter($configuration, $count) |
||
124 | { |
||
125 | $users = $this->repository->countWithFilter($configuration); |
||
126 | $this->assertEquals($count, $users); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return array |
||
131 | */ |
||
132 | View Code Duplication | public function provideCountWithFilter() |
|
133 | { |
||
134 | $configurationAll = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array()); |
||
135 | $configurationLimit = PaginateFinderConfiguration::generateFromVariable(array(), 0, 1, array()); |
||
136 | $configurationSearch = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array(), array('search' => 'demo')); |
||
137 | $configurationAllOrder = PaginateFinderConfiguration::generateFromVariable(array('username' => 'desc'), 0, 100, array()); |
||
138 | |||
139 | return array( |
||
140 | 'all' => array($configurationAll, 5), |
||
141 | 'limit' => array($configurationLimit, 5), |
||
142 | 'search' => array($configurationSearch, 2), |
||
143 | 'order' => array($configurationAllOrder, 5), |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param PaginateFinderConfiguration $configuration |
||
149 | * @param array $sitesId |
||
150 | * @param int $count |
||
151 | * |
||
152 | * @dataProvider provideCountWithFilterAndSitesId |
||
153 | */ |
||
154 | public function testCountWithFilterAndSitesId($configuration, array $sitesId, $count) |
||
155 | { |
||
156 | $sitesId = $this->convertSiteIdInMongoId($sitesId); |
||
157 | $users = $this->repository->countWithFilterAndSiteIds($configuration, $sitesId); |
||
158 | $this->assertEquals($count, $users); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | View Code Duplication | public function provideCountWithFilterAndSitesId() |
|
165 | { |
||
166 | $configurationAll = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array()); |
||
167 | $configurationLimit = PaginateFinderConfiguration::generateFromVariable(array(), 0, 1, array()); |
||
168 | $configurationSearch = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array(), array('search' => 'admin')); |
||
169 | $configurationAllOrder = PaginateFinderConfiguration::generateFromVariable(array('username' => 'desc'), 0, 100, array()); |
||
170 | |||
171 | return array( |
||
172 | array($configurationAll, array(), 0), |
||
173 | array($configurationAll, array('2'), 2), |
||
174 | array($configurationLimit, array('2'), 2), |
||
175 | array($configurationAllOrder, array('2'), 2), |
||
176 | array($configurationSearch, array('2'), 2), |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Test remove users |
||
182 | */ |
||
183 | View Code Duplication | public function testRemoveUsers() |
|
184 | { |
||
185 | $dm = static::$kernel->getContainer()->get('object_manager'); |
||
186 | $userDemo = $this->repository->findOneByUsername('demo'); |
||
187 | $userSAdmin = $this->repository->findOneByUsername('s-admin'); |
||
188 | |||
189 | $userIds = array($userDemo->geTId(), $userSAdmin->getId()); |
||
190 | |||
191 | $this->repository->removeUsers($userIds); |
||
192 | $this->assertNull($this->repository->findOneByUsername('demo')); |
||
193 | $this->assertNull($this->repository->findOneByUsername('s-admin')); |
||
194 | |||
195 | $dm->persist(clone $userDemo); |
||
196 | $dm->persist(clone $userSAdmin); |
||
197 | $dm->flush(); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Test remove users |
||
202 | */ |
||
203 | public function testGetCountsUsersByGroups() |
||
204 | { |
||
205 | $groupDemo = $this->groupRepository->findOneByName('Demo group'); |
||
206 | $groupAdmin = $this->groupRepository->findOneByName('Site Admin demo'); |
||
207 | |||
208 | $groupIds = array($groupDemo->getId(), $groupAdmin->getId()); |
||
209 | |||
210 | $count = $this->repository->getCountsUsersByGroups($groupIds); |
||
211 | $this->assertEquals(array($groupAdmin->getId() => 1, $groupDemo->getId() =>1), $count); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param array $sitesId |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | protected function convertSiteIdInMongoId(array $sitesId) |
||
220 | { |
||
221 | $sitesMongoId = array(); |
||
222 | |||
223 | foreach ($sitesId as $siteId) { |
||
224 | $sitesMongoId[] = static::$kernel->getContainer()->get('open_orchestra_model.repository.site')->findOneBySiteId($siteId)->getId(); |
||
225 | } |
||
226 | |||
227 | return $sitesMongoId; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param string $email |
||
232 | * |
||
233 | * @dataProvider provideEmail |
||
234 | */ |
||
235 | public function testFindOneByEmail($email) |
||
236 | { |
||
237 | $user = $this->repository->findOneByEmail($email); |
||
238 | $this->assertInstanceOf(UserInterface::class, $user); |
||
239 | $this->assertEquals($user->getEmail(), $email); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @return array |
||
244 | */ |
||
245 | public function provideEmail() |
||
246 | { |
||
247 | return array( |
||
248 | array('[email protected]'), |
||
249 | array('[email protected]'), |
||
250 | array('[email protected]'), |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * test findUsersByGroupsForPaginate |
||
256 | */ |
||
257 | public function testFindUsersByGroupsForPaginate() |
||
258 | { |
||
259 | $configuration = PaginateFinderConfiguration::generateFromVariable(array(), 0, 100, array()); |
||
260 | $group = $this->groupRepository->findOneByName('Demo group'); |
||
261 | $users = $this->repository->findUsersByGroupsForPaginate($configuration, $group->getId()); |
||
262 | $this->assertEquals(1, count($users)); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * test countFilterByGroups |
||
267 | */ |
||
268 | public function testCountFilterByGroups() |
||
269 | { |
||
270 | $group = $this->groupRepository->findOneByName('Demo group'); |
||
271 | $this->assertEquals(1, $this->repository->countFilterByGroups($group->getId())); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * test removeGroupFromNotListedUsers |
||
276 | */ |
||
277 | public function testRemoveGroupFromNotListedUsers() |
||
278 | { |
||
279 | $dm = static::$kernel->getContainer()->get('object_manager'); |
||
280 | |||
281 | $fakeGroup = new Group(); |
||
282 | $fakeGroup->setName('fakeGroup'); |
||
283 | $dm->persist($fakeGroup); |
||
284 | $user = $this->repository->findOneByUsername('demo'); |
||
285 | $user->addGroup($fakeGroup); |
||
286 | $dm->persist($user); |
||
287 | |||
288 | $dm->flush(); |
||
289 | |||
290 | $group = $this->groupRepository->findOneByName('fakeGroup'); |
||
291 | |||
292 | $this->assertEquals(1, $this->repository->countFilterByGroups($group->getId())); |
||
293 | |||
294 | $this->repository->removeGroup($user->getId(), $group->getId()); |
||
295 | |||
296 | $this->assertEquals(0, $this->repository->countFilterByGroups($group->getId())); |
||
297 | } |
||
298 | } |
||
299 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.