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\GroupBundle\Repository; |
||
4 | |||
5 | use OpenOrchestra\Backoffice\Repository\GroupRepositoryInterface; |
||
6 | use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractKernelTestCase; |
||
7 | use OpenOrchestra\ModelBundle\Repository\SiteRepository; |
||
8 | use OpenOrchestra\Pagination\Configuration\PaginateFinderConfiguration; |
||
9 | use OpenOrchestra\GroupBundle\Document\Group; |
||
10 | |||
11 | /** |
||
12 | * Class GroupRepositoryTest |
||
13 | * |
||
14 | * @group integrationTest |
||
15 | */ |
||
16 | class GroupRepositoryTest extends AbstractKernelTestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var GroupRepositoryInterface |
||
20 | */ |
||
21 | protected $repository; |
||
22 | |||
23 | /** |
||
24 | * @var SiteRepository |
||
25 | */ |
||
26 | protected $siteRepository; |
||
27 | |||
28 | /** |
||
29 | * Set up test |
||
30 | */ |
||
31 | View Code Duplication | protected function setUp() |
|
0 ignored issues
–
show
|
|||
32 | { |
||
33 | parent::setUp(); |
||
34 | |||
35 | static::bootKernel(); |
||
36 | $this->repository = static::$kernel->getContainer()->get('open_orchestra_user.repository.group'); |
||
37 | $this->siteRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.site'); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param PaginateFinderConfiguration $configuration |
||
42 | * @param array $siteIds |
||
43 | * @param int $count |
||
44 | * |
||
45 | * @dataProvider provideConfigurationAndSites |
||
46 | */ |
||
47 | public function testFindForPaginate(PaginateFinderConfiguration $configuration, array $siteIds, $count) |
||
48 | { |
||
49 | $siteIds = $this->generateMongoIdForSite($siteIds); |
||
50 | $groups = $this->repository->findForPaginate($configuration, $siteIds); |
||
51 | |||
52 | $this->assertCount($count, $groups); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * test count all user |
||
57 | * |
||
58 | * @param PaginateFinderConfiguration $configuration |
||
59 | * @param array $siteIds |
||
60 | * @param int $count |
||
61 | * |
||
62 | * @dataProvider provideConfigurationAndSites |
||
63 | */ |
||
64 | public function testCount(PaginateFinderConfiguration $configuration, array $siteIds, $count) |
||
65 | { |
||
66 | $siteIds = $this->generateMongoIdForSite($siteIds); |
||
67 | $groups = $this->repository->count($siteIds); |
||
68 | |||
69 | $this->assertEquals($count, $groups); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | public function provideConfigurationAndSites() |
||
76 | { |
||
77 | $configuration = new PaginateFinderConfiguration(); |
||
78 | $configuration->setPaginateConfiguration(null, 0, 100, array('label' => 'labels')); |
||
79 | return array( |
||
80 | array($configuration, array(), 0), |
||
81 | array($configuration, array('2'), 2), |
||
82 | array($configuration, array('2', '3'), 3), |
||
83 | array($configuration, array('test'), 0), |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * test findAllWithSite |
||
89 | */ |
||
90 | public function testFindAllWithSite() |
||
91 | { |
||
92 | $groups = $this->repository->findAllWithSite(); |
||
93 | $this->assertCount(3, $groups); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * test findAllWithSiteId |
||
98 | * |
||
99 | * @param string $siteId |
||
100 | * @param int $expectedGroupCount |
||
101 | * |
||
102 | * @dataProvider provideSiteId |
||
103 | */ |
||
104 | public function testFindAllWithSiteId($siteId, $expectedGroupCount) |
||
105 | { |
||
106 | $site = $this->siteRepository->findOneBySiteId($siteId); |
||
107 | $groups = $this->repository->findAllWithSiteId($site->getId()); |
||
108 | |||
109 | $this->assertCount($expectedGroupCount, $groups); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Provite site mongoId |
||
114 | */ |
||
115 | public function provideSiteId() |
||
116 | { |
||
117 | return array( |
||
118 | 'Empty site' => array('3', 1), |
||
119 | 'Demo site' => array('2', 2) |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param PaginateFinderConfiguration $configuration |
||
125 | * @param array $siteIds |
||
126 | * @param int $count |
||
127 | * |
||
128 | * @dataProvider provideColumnsAndSearchAndCount |
||
129 | */ |
||
130 | public function testCountWithFilter(PaginateFinderConfiguration $configuration, array $siteIds, $count) |
||
131 | { |
||
132 | $siteIds = $this->generateMongoIdForSite($siteIds); |
||
133 | $groups = $this->repository->countWithFilter($configuration, $siteIds); |
||
134 | |||
135 | $this->assertEquals($count, $groups); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | public function provideColumnsAndSearchAndCount(){ |
||
142 | |||
143 | $configuration = new PaginateFinderConfiguration(); |
||
144 | $configuration->setPaginateConfiguration(null, 0, 100, array('label' => 'labels')); |
||
145 | |||
146 | $configuration->setSearch(array('language' => 'en', 'label' => 'site')); |
||
147 | |||
148 | return array( |
||
149 | array($configuration, array(), 0), |
||
150 | array($configuration, array('2'), 1), |
||
151 | array($configuration, array('2', '3'), 1), |
||
152 | array($configuration, array('test'), 0), |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Test remove users |
||
158 | */ |
||
159 | public function testRemoveGroups() |
||
160 | { |
||
161 | $dm = static::$kernel->getContainer()->get('object_manager'); |
||
162 | |||
163 | $groupTest = new Group(); |
||
164 | $groupTest->setName('test'); |
||
165 | $dm->persist($groupTest); |
||
166 | $dm->flush(); |
||
167 | $dm->clear(); |
||
168 | |||
169 | $groupTest = $this->repository->findOneByName('test'); |
||
170 | |||
171 | $groupIds = array($groupTest->getId()); |
||
172 | |||
173 | $this->repository->removeGroups($groupIds); |
||
174 | $this->assertNull($this->repository->findOneByName('test')); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Test remove users |
||
179 | */ |
||
180 | public function testSoftDeleteGroupBySite() |
||
181 | { |
||
182 | $dm = static::$kernel->getContainer()->get('object_manager'); |
||
183 | $site = $this->siteRepository->findOneBySiteId('3'); |
||
184 | $this->repository->softDeleteGroupsBySite($site); |
||
185 | |||
186 | $groups = $this->repository->findAllWithSiteId($site->getId()); |
||
187 | |||
188 | foreach ($groups as $group) { |
||
189 | $this->assertTrue($group->isDeleted()); |
||
190 | $group->setDeleted(false); |
||
191 | } |
||
192 | $dm->flush(); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Generate columns of content with search value |
||
197 | * |
||
198 | * @param string $searchName |
||
199 | * @param string $globalSearch |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | protected function generateSearchProvider($searchName = '', $globalSearch = '') |
||
204 | { |
||
205 | $search = array(); |
||
206 | if (!empty($searchName)) { |
||
207 | $search['columns'] = array('name' => $searchName); |
||
208 | } |
||
209 | if (!empty($globalSearch)) { |
||
210 | $search['global'] = $globalSearch; |
||
211 | } |
||
212 | |||
213 | return $search; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param array $siteIds |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function generateMongoIdForSite(array $siteIds) |
||
222 | { |
||
223 | foreach ($siteIds as $key => $siteId) { |
||
224 | $site = $this->siteRepository->findOneBySiteId($siteId); |
||
225 | if (null !== $site) { |
||
226 | $siteIds[$key] = $site->getId(); |
||
227 | } else { |
||
228 | unset($siteIds[$key]); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return $siteIds; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Generate relation between columns names and entities attributes |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function getDescriptionColumnEntity() |
||
241 | { |
||
242 | |||
243 | return array( |
||
244 | 'name' => array('key' => 'name', 'field' => 'name', 'type' => 'string') |
||
245 | ); |
||
246 | } |
||
247 | |||
248 | } |
||
249 |
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.