These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\AdminBundle\Tests\Admin; |
||
15 | |||
16 | use PHPUnit\Framework\TestCase; |
||
17 | use Sonata\AdminBundle\Admin\AdminInterface; |
||
18 | use Sonata\AdminBundle\Admin\Pool; |
||
19 | use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; |
||
20 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
21 | |||
22 | class PoolTest extends TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var Pool |
||
26 | */ |
||
27 | private $pool = null; |
||
28 | |||
29 | public function setUp(): void |
||
30 | { |
||
31 | $this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']); |
||
32 | } |
||
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'); |
||
0 ignored issues
–
show
|
|||
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'); |
||
0 ignored issues
–
show
$containerMock is of type object<PHPUnit\Framework\MockObject\MockObject> , but the function expects a object<Symfony\Component...ion\ContainerInterface> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
223 | $this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
||
224 | |||
225 | $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment')); |
||
226 | } |
||
227 | |||
228 | public function testGetAdminByAdminCodeForChildInvalidClass(): void |
||
229 | { |
||
230 | $adminMock = $this->getMockBuilder(AdminInterface::class) |
||
231 | ->disableOriginalConstructor() |
||
232 | ->getMock(); |
||
233 | $adminMock->expects($this->any()) |
||
234 | ->method('hasChild') |
||
235 | ->willReturn(false); |
||
236 | |||
237 | $containerMock = $this->createMock(ContainerInterface::class); |
||
238 | $containerMock->expects($this->any()) |
||
239 | ->method('get') |
||
240 | ->willReturn($adminMock); |
||
241 | |||
242 | $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png'); |
||
243 | $this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
||
244 | |||
245 | $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid')); |
||
246 | } |
||
247 | |||
248 | public function testGetAdminClasses(): void |
||
249 | { |
||
250 | $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
||
251 | $this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses()); |
||
252 | } |
||
253 | |||
254 | public function testGetAdminGroups(): void |
||
255 | { |
||
256 | $this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']); |
||
257 | $this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups()); |
||
258 | } |
||
259 | |||
260 | public function testGetAdminServiceIds(): void |
||
261 | { |
||
262 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
||
263 | $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds()); |
||
264 | } |
||
265 | |||
266 | public function testGetContainer(): void |
||
267 | { |
||
268 | $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer()); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @group legacy |
||
273 | */ |
||
274 | public function testTemplate(): void |
||
275 | { |
||
276 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
277 | $templateRegistry->getTemplate('ajax') |
||
278 | ->shouldBeCalledTimes(1) |
||
279 | ->willReturn('Foo.html.twig'); |
||
280 | |||
281 | $this->pool->setTemplateRegistry($templateRegistry->reveal()); |
||
282 | |||
283 | $this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax')); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @group legacy |
||
288 | */ |
||
289 | public function testSetGetTemplates(): void |
||
290 | { |
||
291 | $templates = [ |
||
292 | 'ajax' => 'Foo.html.twig', |
||
293 | 'layout' => 'Bar.html.twig', |
||
294 | ]; |
||
295 | |||
296 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
297 | $templateRegistry->setTemplates($templates) |
||
298 | ->shouldBeCalledTimes(1); |
||
299 | $templateRegistry->getTemplates() |
||
300 | ->shouldBeCalledTimes(1) |
||
301 | ->willReturn($templates); |
||
302 | |||
303 | $this->pool->setTemplateRegistry($templateRegistry->reveal()); |
||
304 | |||
305 | $this->pool->setTemplates($templates); |
||
306 | |||
307 | $this->assertSame($templates, $this->pool->getTemplates()); |
||
308 | } |
||
309 | |||
310 | public function testGetTitleLogo(): void |
||
311 | { |
||
312 | $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo()); |
||
313 | } |
||
314 | |||
315 | public function testGetTitle(): void |
||
316 | { |
||
317 | $this->assertSame('Sonata Admin', $this->pool->getTitle()); |
||
318 | } |
||
319 | |||
320 | public function testGetOption(): void |
||
321 | { |
||
322 | $this->assertSame('bar', $this->pool->getOption('foo')); |
||
323 | |||
324 | $this->assertNull($this->pool->getOption('non_existent_option')); |
||
325 | } |
||
326 | |||
327 | public function testOptionDefault(): void |
||
328 | { |
||
329 | $this->assertSame([], $this->pool->getOption('nonexistantarray', [])); |
||
330 | } |
||
331 | |||
332 | private function getContainer(): ContainerInterface |
||
333 | { |
||
334 | $containerMock = $this->createMock(ContainerInterface::class); |
||
335 | $containerMock->expects($this->any()) |
||
336 | ->method('get') |
||
337 | ->willReturnCallback(function () { |
||
338 | return $this->createMock(AdminInterface::class); |
||
339 | }); |
||
340 | |||
341 | return $containerMock; |
||
342 | } |
||
343 | |||
344 | private function getItemArray($serviceId): array |
||
345 | { |
||
346 | return [ |
||
347 | 'admin' => $serviceId, |
||
348 | 'label' => '', |
||
349 | 'route' => '', |
||
350 | 'route_params' => [], |
||
351 | ]; |
||
352 | } |
||
353 | } |
||
354 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: