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 | protected function setUp() |
||
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 | var_dump(get_class($this->repository)); |
||
184 | $site = $this->siteRepository->findOneBySiteId('3'); |
||
185 | $this->repository->softDeleteGroupsBySite($site); |
||
0 ignored issues
–
show
|
|||
186 | |||
187 | $groups = $this->repository->findAllWithSiteId($site->getId()); |
||
188 | |||
189 | foreach ($groups as $group) { |
||
190 | $this->assertTrue($group->isDeleted()); |
||
191 | $group->setDeleted(false); |
||
192 | } |
||
193 | $dm->flush(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Generate columns of content with search value |
||
198 | * |
||
199 | * @param string $searchName |
||
200 | * @param string $globalSearch |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function generateSearchProvider($searchName = '', $globalSearch = '') |
||
205 | { |
||
206 | $search = array(); |
||
207 | if (!empty($searchName)) { |
||
208 | $search['columns'] = array('name' => $searchName); |
||
209 | } |
||
210 | if (!empty($globalSearch)) { |
||
211 | $search['global'] = $globalSearch; |
||
212 | } |
||
213 | |||
214 | return $search; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param array $siteIds |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function generateMongoIdForSite(array $siteIds) |
||
223 | { |
||
224 | foreach ($siteIds as $key => $siteId) { |
||
225 | $site = $this->siteRepository->findOneBySiteId($siteId); |
||
226 | if (null !== $site) { |
||
227 | $siteIds[$key] = $site->getId(); |
||
228 | } else { |
||
229 | unset($siteIds[$key]); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $siteIds; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Generate relation between columns names and entities attributes |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function getDescriptionColumnEntity() |
||
242 | { |
||
243 | |||
244 | return array( |
||
245 | 'name' => array('key' => 'name', 'field' => 'name', 'type' => 'string') |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | } |
||
250 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: