Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

testGetInstanceWithUndefinedServiceIdAndExistsOther()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        $expectedOutput = [
43
            'adminGroup1' => [
44
                'sonata.user.admin.group1' => 'sonata_user_admin_group1_AdminClass',
45
            ],
46
        ];
47
48
        $this->assertSame($expectedOutput, $this->pool->getGroups());
49
    }
50
51
    public function testHasGroup(): void
52
    {
53
        $this->pool->setAdminGroups([
54
                'adminGroup1' => [],
55
            ]);
56
57
        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
58
        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
59
    }
60
61
    public function testGetDashboardGroups(): void
62
    {
63
        $admin_group1 = $this->createMock(AdminInterface::class);
64
        $admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true));
65
66
        $admin_group2 = $this->createMock(AdminInterface::class);
67
        $admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false));
68
69
        $admin_group3 = $this->createMock(AdminInterface::class);
70
        $admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false));
71
72
        $container = $this->createMock(ContainerInterface::class);
73
74
        $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
75
            $admin_group1, $admin_group2, $admin_group3
76
        ));
77
78
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
79
        $pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
80
81
        $pool->setAdminGroups([
82
            'adminGroup1' => [
83
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')],
84
            ],
85
            'adminGroup2' => [
86
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')],
87
            ],
88
            'adminGroup3' => [
89
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')],
90
            ],
91
        ]);
92
93
        $groups = $pool->getDashboardGroups();
94
95
        $this->assertCount(1, $groups);
96
        $this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']);
97
    }
98
99
    public function testGetAdminsByGroupWhenGroupNotSet(): void
100
    {
101
        $this->expectException(\InvalidArgumentException::class);
102
103
        $this->pool->setAdminGroups([
104
                'adminGroup1' => [],
105
            ]);
106
107
        $this->pool->getAdminsByGroup('adminGroup2');
108
    }
109
110
    public function testGetAdminsByGroupWhenGroupIsEmpty(): void
111
    {
112
        $this->pool->setAdminGroups([
113
                'adminGroup1' => [],
114
            ]);
115
116
        $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1'));
117
    }
118
119
    public function testGetAdminsByGroup(): void
120
    {
121
        $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']);
122
        $this->pool->setAdminGroups([
123
            'adminGroup1' => [
124
                'items' => [
125
                    $this->getItemArray('sonata.admin1'),
126
                    $this->getItemArray('sonata.admin2'),
127
                ],
128
            ],
129
            'adminGroup2' => [
130
                'items' => [$this->getItemArray('sonata.admin3')],
131
            ],
132
        ]);
133
134
        $this->assertEquals([
135
            'sonata_admin1_AdminClass',
136
            'sonata_admin2_AdminClass',
137
        ], $this->pool->getAdminsByGroup('adminGroup1'));
138
        $this->assertEquals(['sonata_admin3_AdminClass'], $this->pool->getAdminsByGroup('adminGroup2'));
139
    }
140
141
    public function testGetAdminForClassWhenAdminClassIsNotSet(): void
142
    {
143
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
144
        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
145
        $this->assertNull($this->pool->getAdminByClass('notexists'));
146
    }
147
148
    public function testGetAdminForClassWithInvalidFormat(): void
149
    {
150
        $this->expectException(\RuntimeException::class);
151
152
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
153
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
154
155
        $this->pool->getAdminByClass('someclass');
156
    }
157
158
    public function testGetAdminForClassWithTooManyRegisteredAdmin(): void
159
    {
160
        $this->expectException(\RuntimeException::class);
161
162
        $this->pool->setAdminClasses([
163
            'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'],
164
        ]);
165
166
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
167
        $this->pool->getAdminByClass('someclass');
168
    }
169
170
    public function testGetAdminForClassWhenAdminClassIsSet(): void
171
    {
172
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
173
        $this->pool->setAdminClasses([
174
            'someclass' => ['sonata.user.admin.group1'],
175
        ]);
176
177
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
178
        $this->assertSame('sonata_user_admin_group1_AdminClass', $this->pool->getAdminByClass('someclass'));
179
    }
180
181
    public function testGetInstanceWithUndefinedServiceId(): void
182
    {
183
        $this->expectException(\InvalidArgumentException::class);
184
        $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.');
185
186
        $this->pool->getInstance('sonata.news.admin.post');
187
    }
188
189
    public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void
190
    {
191
        $this->pool->setAdminServiceIds([
192
            'sonata.news.admin.post',
193
            'sonata.news.admin.category',
194
        ]);
195
196
        $this->expectException(\InvalidArgumentException::class);
197
        $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. '
198
            .'Did you mean "sonata.news.admin.post" '
199
            .'or one of those: [sonata.news.admin.category]?');
200
201
        $this->pool->getInstance('sonata.news.admin.pos');
202
    }
203
204
    public function testGetAdminByAdminCode(): void
205
    {
206
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
207
208
        $this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
209
    }
210
211
    public function testGetAdminByAdminCodeForChildClass(): void
212
    {
213
        $adminMock = $this->getMockBuilder(AdminInterface::class)
214
            ->disableOriginalConstructor()
215
            ->getMock();
216
        $adminMock->expects($this->any())
217
            ->method('hasChild')
218
            ->will($this->returnValue(true));
219
        $adminMock->expects($this->once())
220
            ->method('getChild')
221
            ->with($this->equalTo('sonata.news.admin.comment'))
222
            ->will($this->returnValue('commentAdminClass'));
223
224
        $containerMock = $this->createMock(ContainerInterface::class);
225
        $containerMock->expects($this->any())
226
            ->method('get')
227
            ->will($this->returnValue($adminMock));
228
229
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
230
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
231
232
        $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
233
    }
234
235
    public function testGetAdminByAdminCodeForChildInvalidClass(): void
236
    {
237
        $adminMock = $this->getMockBuilder(AdminInterface::class)
238
            ->disableOriginalConstructor()
239
            ->getMock();
240
        $adminMock->expects($this->any())
241
            ->method('hasChild')
242
            ->will($this->returnValue(false));
243
244
        $containerMock = $this->createMock(ContainerInterface::class);
245
        $containerMock->expects($this->any())
246
            ->method('get')
247
            ->will($this->returnValue($adminMock));
248
249
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
250
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
251
252
        $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
253
    }
254
255
    public function testGetAdminClasses(): void
256
    {
257
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
258
        $this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses());
259
    }
260
261
    public function testGetAdminGroups(): void
262
    {
263
        $this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']);
264
        $this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups());
265
    }
266
267
    public function testGetAdminServiceIds(): void
268
    {
269
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
270
        $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds());
271
    }
272
273
    public function testGetContainer(): void
274
    {
275
        $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer());
276
    }
277
278
    /**
279
     * @group legacy
280
     */
281
    public function testTemplate(): void
282
    {
283
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
284
        $templateRegistry->getTemplate('ajax')
285
            ->shouldBeCalledTimes(1)
286
            ->willReturn('Foo.html.twig');
287
288
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
289
290
        $this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin\Pool::getTemplate() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead

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.

Loading history...
291
    }
292
293
    /**
294
     * @group legacy
295
     */
296
    public function testSetGetTemplates(): void
297
    {
298
        $templates = [
299
            'ajax' => 'Foo.html.twig',
300
            'layout' => 'Bar.html.twig',
301
        ];
302
303
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
304
        $templateRegistry->setTemplates($templates)
305
            ->shouldBeCalledTimes(1);
306
        $templateRegistry->getTemplates()
307
            ->shouldBeCalledTimes(1)
308
            ->willReturn($templates);
309
310
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
311
312
        $this->pool->setTemplates($templates);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin\Pool::setTemplates() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead

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.

Loading history...
313
314
        $this->assertSame($templates, $this->pool->getTemplates());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin\Pool::getTemplates() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead

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.

Loading history...
315
    }
316
317
    public function testGetTitleLogo(): void
318
    {
319
        $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
320
    }
321
322
    public function testGetTitle(): void
323
    {
324
        $this->assertSame('Sonata Admin', $this->pool->getTitle());
325
    }
326
327
    public function testGetOption(): void
328
    {
329
        $this->assertSame('bar', $this->pool->getOption('foo'));
330
331
        $this->assertNull($this->pool->getOption('non_existent_option'));
332
    }
333
334
    public function testOptionDefault(): void
335
    {
336
        $this->assertSame([], $this->pool->getOption('nonexistantarray', []));
337
    }
338
339
    /**
340
     * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
341
     */
342
    private function getContainer()
343
    {
344
        $containerMock = $this->createMock(ContainerInterface::class);
345
        $containerMock->expects($this->any())
346
            ->method('get')
347
            ->will($this->returnCallback(function ($serviceId) {
348
                return str_replace('.', '_', $serviceId).'_AdminClass';
349
            }));
350
351
        return $containerMock;
352
    }
353
354
    private function getItemArray($serviceId)
355
    {
356
        return [
357
            'admin' => $serviceId,
358
            'label' => '',
359
            'route' => '',
360
            'route_params' => [],
361
        ];
362
    }
363
}
364