1 | <?php |
||
22 | class PoolTest extends TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var Pool |
||
26 | */ |
||
27 | private $pool = null; |
||
28 | |||
29 | public function setUp(): void |
||
33 | |||
34 | public function testGetGroups(): void |
||
35 | { |
||
36 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
37 | |||
38 | $this->pool->setAdminGroups([ |
||
39 | 'adminGroup1' => ['sonata.user.admin.group1' => []], |
||
40 | ]); |
||
41 | |||
42 | $result = $this->pool->getGroups(); |
||
43 | $this->assertArrayHasKey('adminGroup1', $result); |
||
44 | $this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']); |
||
45 | } |
||
46 | |||
47 | public function testHasGroup(): void |
||
48 | { |
||
49 | $this->pool->setAdminGroups([ |
||
50 | 'adminGroup1' => [], |
||
51 | ]); |
||
52 | |||
53 | $this->assertTrue($this->pool->hasGroup('adminGroup1')); |
||
54 | $this->assertFalse($this->pool->hasGroup('adminGroup2')); |
||
55 | } |
||
56 | |||
57 | public function testGetDashboardGroups(): void |
||
58 | { |
||
59 | $admin_group1 = $this->createMock(AdminInterface::class); |
||
60 | $admin_group1->expects($this->once())->method('showIn')->willReturn(true); |
||
61 | |||
62 | $admin_group2 = $this->createMock(AdminInterface::class); |
||
63 | $admin_group2->expects($this->once())->method('showIn')->willReturn(false); |
||
64 | |||
65 | $admin_group3 = $this->createMock(AdminInterface::class); |
||
66 | $admin_group3->expects($this->once())->method('showIn')->willReturn(false); |
||
67 | |||
68 | $container = $this->createMock(ContainerInterface::class); |
||
69 | |||
70 | $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls( |
||
71 | $admin_group1, $admin_group2, $admin_group3 |
||
72 | )); |
||
73 | |||
74 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
75 | $pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
||
76 | |||
77 | $pool->setAdminGroups([ |
||
78 | 'adminGroup1' => [ |
||
79 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')], |
||
80 | ], |
||
81 | 'adminGroup2' => [ |
||
82 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')], |
||
83 | ], |
||
84 | 'adminGroup3' => [ |
||
85 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')], |
||
86 | ], |
||
87 | ]); |
||
88 | |||
89 | $groups = $pool->getDashboardGroups(); |
||
90 | |||
91 | $this->assertCount(1, $groups); |
||
92 | $this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']); |
||
93 | } |
||
94 | |||
95 | public function testGetAdminsByGroupWhenGroupNotSet(): void |
||
96 | { |
||
97 | $this->expectException(\InvalidArgumentException::class); |
||
98 | |||
99 | $this->pool->setAdminGroups([ |
||
100 | 'adminGroup1' => [], |
||
101 | ]); |
||
102 | |||
103 | $this->pool->getAdminsByGroup('adminGroup2'); |
||
104 | } |
||
105 | |||
106 | public function testGetAdminsByGroupWhenGroupIsEmpty(): void |
||
107 | { |
||
108 | $this->pool->setAdminGroups([ |
||
109 | 'adminGroup1' => [], |
||
110 | ]); |
||
111 | |||
112 | $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1')); |
||
113 | } |
||
114 | |||
115 | public function testGetAdminsByGroup(): void |
||
116 | { |
||
117 | $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']); |
||
118 | $this->pool->setAdminGroups([ |
||
119 | 'adminGroup1' => [ |
||
120 | 'items' => [ |
||
121 | $this->getItemArray('sonata.admin1'), |
||
122 | $this->getItemArray('sonata.admin2'), |
||
123 | ], |
||
124 | ], |
||
125 | 'adminGroup2' => [ |
||
126 | 'items' => [$this->getItemArray('sonata.admin3')], |
||
127 | ], |
||
128 | ]); |
||
129 | |||
130 | $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1')); |
||
131 | $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2')); |
||
132 | } |
||
133 | |||
134 | public function testGetAdminForClassWhenAdminClassIsNotSet(): void |
||
135 | { |
||
136 | $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
||
137 | $this->assertFalse($this->pool->hasAdminByClass('notexists')); |
||
138 | $this->assertNull($this->pool->getAdminByClass('notexists')); |
||
139 | } |
||
140 | |||
141 | public function testGetAdminForClassWithInvalidFormat(): void |
||
142 | { |
||
143 | $this->expectException(\RuntimeException::class); |
||
144 | |||
145 | $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
||
146 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
147 | |||
148 | $this->pool->getAdminByClass('someclass'); |
||
149 | } |
||
150 | |||
151 | public function testGetAdminForClassWithTooManyRegisteredAdmin(): void |
||
152 | { |
||
153 | $this->expectException(\RuntimeException::class); |
||
154 | |||
155 | $this->pool->setAdminClasses([ |
||
156 | 'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'], |
||
157 | ]); |
||
158 | |||
159 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
160 | $this->pool->getAdminByClass('someclass'); |
||
161 | } |
||
162 | |||
163 | public function testGetAdminForClassWhenAdminClassIsSet(): void |
||
164 | { |
||
165 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
166 | $this->pool->setAdminClasses([ |
||
167 | 'someclass' => ['sonata.user.admin.group1'], |
||
168 | ]); |
||
169 | |||
170 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
171 | $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByClass('someclass')); |
||
172 | } |
||
173 | |||
174 | public function testGetInstanceWithUndefinedServiceId(): void |
||
175 | { |
||
176 | $this->expectException(\InvalidArgumentException::class); |
||
177 | $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.'); |
||
178 | |||
179 | $this->pool->getInstance('sonata.news.admin.post'); |
||
180 | } |
||
181 | |||
182 | public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void |
||
183 | { |
||
184 | $this->pool->setAdminServiceIds([ |
||
185 | 'sonata.news.admin.post', |
||
186 | 'sonata.news.admin.category', |
||
187 | ]); |
||
188 | |||
189 | $this->expectException(\InvalidArgumentException::class); |
||
190 | $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. ' |
||
191 | .'Did you mean "sonata.news.admin.post" ' |
||
192 | .'or one of those: [sonata.news.admin.category]?'); |
||
193 | |||
194 | $this->pool->getInstance('sonata.news.admin.pos'); |
||
195 | } |
||
196 | |||
197 | public function testGetAdminByAdminCode(): void |
||
198 | { |
||
199 | $this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
||
200 | |||
201 | $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByAdminCode('sonata.news.admin.post')); |
||
202 | } |
||
203 | |||
204 | public function testGetAdminByAdminCodeForChildClass(): void |
||
205 | { |
||
206 | $adminMock = $this->getMockBuilder(AdminInterface::class) |
||
207 | ->disableOriginalConstructor() |
||
208 | ->getMock(); |
||
209 | $adminMock->expects($this->any()) |
||
210 | ->method('hasChild') |
||
211 | ->willReturn(true); |
||
212 | $adminMock->expects($this->once()) |
||
213 | ->method('getChild') |
||
214 | ->with($this->equalTo('sonata.news.admin.comment')) |
||
215 | ->willReturn('commentAdminClass'); |
||
216 | |||
217 | $containerMock = $this->createMock(ContainerInterface::class); |
||
218 | $containerMock->expects($this->any()) |
||
219 | ->method('get') |
||
220 | ->willReturn($adminMock); |
||
221 | |||
222 | $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png'); |
||
223 | $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']); |
||
224 | |||
225 | $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment')); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @group legacy |
||
230 | * |
||
231 | * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since 3.x and will throw an exception in 4.0. |
||
232 | */ |
||
233 | public function testGetAdminByAdminCodeForChildInvalidClass(): void |
||
252 | |||
253 | /** |
||
254 | * @dataProvider getEmptyRootAdminServiceNames |
||
255 | */ |
||
256 | public function testGetAdminByAdminCodeWithInvalidRootCode(string $adminId): void |
||
257 | { |
||
258 | $adminMock = $this->createMock(AdminInterface::class); |
||
259 | $adminMock->expects($this->never()) |
||
260 | ->method('hasChild'); |
||
261 | |||
262 | $containerMock = $this->createMock(ContainerInterface::class); |
||
263 | $containerMock->expects($this->never()) |
||
264 | ->method('get'); |
||
265 | |||
266 | $poolMock = $this->getMockBuilder(Pool::class) |
||
278 | |||
279 | public function getEmptyRootAdminServiceNames() |
||
287 | |||
288 | /** |
||
289 | * @dataProvider getInvalidChildAdminServiceNames |
||
290 | * |
||
291 | * @group legacy |
||
292 | * |
||
293 | * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since 3.x and will throw an exception in 4.0. |
||
294 | */ |
||
295 | public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void |
||
319 | |||
320 | public function getInvalidChildAdminServiceNames() |
||
328 | |||
329 | public function testGetAdminClasses(): void |
||
334 | |||
335 | public function testGetAdminGroups(): void |
||
340 | |||
341 | public function testGetAdminServiceIds(): void |
||
346 | |||
347 | public function testGetContainer(): void |
||
351 | |||
352 | /** |
||
353 | * @group legacy |
||
354 | */ |
||
355 | public function testTemplate(): void |
||
366 | |||
367 | /** |
||
368 | * @group legacy |
||
369 | */ |
||
370 | public function testSetGetTemplates(): void |
||
390 | |||
391 | public function testGetTitleLogo(): void |
||
395 | |||
396 | public function testGetTitle(): void |
||
400 | |||
401 | public function testGetOption(): void |
||
407 | |||
408 | public function testOptionDefault(): void |
||
412 | |||
413 | private function getContainer(): ContainerInterface |
||
424 | |||
425 | private function getItemArray($serviceId): array |
||
434 | } |
||
435 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.