Completed
Push — master ( d1d910...7edec1 )
by amaury
03:34
created

UserAdminBundle/Repository/UserRepositoryTest.php (5 issues)

Upgrade to new PHP Analysis Engine

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()
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');
0 ignored issues
show
The property groupRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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
     * @param $username
182
     * @param $groupName
183
     * @param $countUser
184
     *
185
     * @dataProvider provideUserAndGroup
186
     */
187
    public function testFindByIncludedUsernameWithoutGroup($username, $groupName, $countUser)
188
    {
189
        $group =  static::$kernel->getContainer()->get('open_orchestra_user.repository.group')->findOneByName($groupName);
190
        $users = $this->repository->findByIncludedUsernameWithoutGroup($username, $group);
191
192
        $this->assertCount($countUser, $users);
193
    }
194
195
    public function provideUserAndGroup()
196
    {
197
        return array(
198
            array('de', 'Empty group', 1),
199
            array('admi', 'Empty group', 2),
200
            array('user', 'Empty group', 1),
201
            array('fakeUser', 'Demo group', 0)
202
        );
203
    }
204
205
    /**
206
     * Test remove users
207
     */
208 View Code Duplication
    public function testRemoveUsers()
209
    {
210
        $dm = static::$kernel->getContainer()->get('object_manager');
211
        $userDemo = $this->repository->findOneByUsername('demo');
212
        $userSAdmin = $this->repository->findOneByUsername('s-admin');
213
214
        $userIds = array($userDemo->geTId(), $userSAdmin->getId());
215
216
        $this->repository->removeUsers($userIds);
217
        $this->assertNull($this->repository->findOneByUsername('demo'));
218
        $this->assertNull($this->repository->findOneByUsername('s-admin'));
219
220
        $dm->persist(clone $userDemo);
221
        $dm->persist(clone $userSAdmin);
222
        $dm->flush();
223
    }
224
225
    /**
226
     * Test remove users
227
     */
228
    public function testGetCountsUsersByGroups()
229
    {
230
        $groupDemo = $this->groupRepository->findOneByName('Demo group');
0 ignored issues
show
The property groupRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
231
        $groupAdmin = $this->groupRepository->findOneByName('Site Admin demo');
0 ignored issues
show
The property groupRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
232
233
        $groupIds = array($groupDemo->getId(), $groupAdmin->getId());
234
235
        $count = $this->repository->getCountsUsersByGroups($groupIds);
236
        $this->assertEquals(array($groupAdmin->getId() => 1, $groupDemo->getId() =>1), $count);
237
    }
238
239
    /**
240
     * @param array $sitesId
241
     *
242
     * @return array
243
     */
244
    protected function convertSiteIdInMongoId(array $sitesId)
245
    {
246
        $sitesMongoId = array();
247
248
        foreach ($sitesId as $siteId) {
249
            $sitesMongoId[] = static::$kernel->getContainer()->get('open_orchestra_model.repository.site')->findOneBySiteId($siteId)->getId();
250
        }
251
252
        return $sitesMongoId;
253
    }
254
255
    /**
256
     * @param string $email
257
     *
258
     * @dataProvider provideEmail
259
     */
260
    public function testFindOneByEmail($email)
261
    {
262
        $user = $this->repository->findOneByEmail($email);
263
        $this->assertInstanceOf(UserInterface::class, $user);
264
        $this->assertEquals($user->getEmail(), $email);
265
    }
266
267
    /**
268
     * @return array
269
     */
270
    public function provideEmail()
271
    {
272
        return array(
273
            array('[email protected]'),
274
            array('[email protected]'),
275
            array('[email protected]'),
276
        );
277
    }
278
279
    /**
280
     * test findUsersByGroups
281
     */
282
    public function testFindUsersByGroups()
283
    {
284
        $group = $this->groupRepository->findOneByName('Demo group');
0 ignored issues
show
The property groupRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
285
        $users = $this->repository->findUsersByGroups($group->getId());
286
        $this->assertEquals(1, count($users));
287
    }
288
289
    /**
290
     * test removeGroupFromNotListedUsers
291
     */
292
    public function testRemoveGroupFromNotListedUsers()
293
    {
294
        $dm = static::$kernel->getContainer()->get('object_manager');
295
296
        $fakeGroup = new Group();
297
        $fakeGroup->setName('fakeGroup');
298
        $dm->persist($fakeGroup);
299
        $users = $this->repository->findAll();
300
        $nbrUsers = count($users);
301
        foreach ($users as $user) {
302
            $user->addGroup($fakeGroup);
303
            $dm->persist($user);
304
        }
305
306
        $dm->flush();
307
308
        $group = $this->groupRepository->findOneByName('fakeGroup');
0 ignored issues
show
The property groupRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
309
        $users = $this->repository->findUsersByGroups($group->getId());
310
311
        $this->assertEquals($nbrUsers, count($users));
312
313
        $this->repository->removeGroupFromNotListedUsers($group->getId(), array());
314
        $users = $this->repository->findUsersByGroups($group->getId());
315
316
        $this->assertEquals(0, count($users));
317
318
    }
319
}
320