Completed
Push — 3.x ( 799626...732596 )
by Grégoire
04:11
created

PoolTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 1
dl 0
loc 327
rs 10
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAdminForClassWhenAdminClassIsSet() 0 10 1
A testGetAdminsByGroupWhenGroupIsEmpty() 0 8 1
A testGetAdminsByGroup() 0 21 1
A testGetAdminForClassWhenAdminClassIsNotSet() 0 6 1
A setUp() 0 4 1
A testGetGroups() 0 16 1
A testHasGroup() 0 9 1
B testGetDashboardGroups() 0 37 1
A testGetAdminsByGroupWhenGroupNotSet() 0 10 1
A testGetAdminForClassWithInvalidFormat() 0 9 1
A testGetAdminForClassWithTooManyRegisteredAdmin() 0 11 1
A testGetInstanceWithUndefinedServiceId() 0 7 1
A testGetAdminByAdminCode() 0 6 1
A testGetAdminByAdminCodeForChildClass() 0 23 1
A testGetAdminByAdminCodeForChildInvalidClass() 0 19 1
A testGetAdminClasses() 0 5 1
A testGetAdminGroups() 0 5 1
A testGetAdminServiceIds() 0 5 1
A testGetContainer() 0 4 1
A testTemplate() 0 11 1
A testSetGetTemplates() 0 20 1
A testGetTitleLogo() 0 4 1
A testGetTitle() 0 4 1
A testGetOption() 0 6 1
A testOptionDefault() 0 4 1
A getContainer() 0 11 1
A getItemArray() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Admin;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
class PoolTest extends TestCase
21
{
22
    /**
23
     * @var Pool
24
     */
25
    private $pool = null;
26
27
    public function setUp()
28
    {
29
        $this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']);
30
    }
31
32
    public function testGetGroups()
33
    {
34
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
35
36
        $this->pool->setAdminGroups([
37
            'adminGroup1' => ['sonata.user.admin.group1' => []],
38
        ]);
39
40
        $expectedOutput = [
41
            'adminGroup1' => [
42
                'sonata.user.admin.group1' => 'sonata_user_admin_group1_AdminClass',
43
            ],
44
        ];
45
46
        $this->assertSame($expectedOutput, $this->pool->getGroups());
47
    }
48
49
    public function testHasGroup()
50
    {
51
        $this->pool->setAdminGroups([
52
                'adminGroup1' => [],
53
            ]);
54
55
        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
56
        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
57
    }
58
59
    public function testGetDashboardGroups()
60
    {
61
        $admin_group1 = $this->createMock(AdminInterface::class);
62
        $admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true));
63
64
        $admin_group2 = $this->createMock(AdminInterface::class);
65
        $admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false));
66
67
        $admin_group3 = $this->createMock(AdminInterface::class);
68
        $admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false));
69
70
        $container = $this->createMock(ContainerInterface::class);
71
72
        $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
73
            $admin_group1, $admin_group2, $admin_group3
74
        ));
75
76
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
77
        $pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
78
79
        $pool->setAdminGroups([
80
            'adminGroup1' => [
81
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')],
82
            ],
83
            'adminGroup2' => [
84
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')],
85
            ],
86
            'adminGroup3' => [
87
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')],
88
            ],
89
        ]);
90
91
        $groups = $pool->getDashboardGroups();
92
93
        $this->assertCount(1, $groups);
94
        $this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']);
95
    }
96
97
    public function testGetAdminsByGroupWhenGroupNotSet()
98
    {
99
        $this->expectException(\InvalidArgumentException::class);
100
101
        $this->pool->setAdminGroups([
102
                'adminGroup1' => [],
103
            ]);
104
105
        $this->pool->getAdminsByGroup('adminGroup2');
106
    }
107
108
    public function testGetAdminsByGroupWhenGroupIsEmpty()
109
    {
110
        $this->pool->setAdminGroups([
111
                'adminGroup1' => [],
112
            ]);
113
114
        $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1'));
115
    }
116
117
    public function testGetAdminsByGroup()
118
    {
119
        $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']);
120
        $this->pool->setAdminGroups([
121
            'adminGroup1' => [
122
                'items' => [
123
                    $this->getItemArray('sonata.admin1'),
124
                    $this->getItemArray('sonata.admin2'),
125
                ],
126
            ],
127
            'adminGroup2' => [
128
                'items' => [$this->getItemArray('sonata.admin3')],
129
            ],
130
        ]);
131
132
        $this->assertEquals([
133
            'sonata_admin1_AdminClass',
134
            'sonata_admin2_AdminClass',
135
        ], $this->pool->getAdminsByGroup('adminGroup1'));
136
        $this->assertEquals(['sonata_admin3_AdminClass'], $this->pool->getAdminsByGroup('adminGroup2'));
137
    }
138
139
    public function testGetAdminForClassWhenAdminClassIsNotSet()
140
    {
141
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
142
        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
143
        $this->assertNull($this->pool->getAdminByClass('notexists'));
144
    }
145
146
    public function testGetAdminForClassWithInvalidFormat()
147
    {
148
        $this->expectException(\RuntimeException::class);
149
150
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
151
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
152
153
        $this->pool->getAdminByClass('someclass');
154
    }
155
156
    public function testGetAdminForClassWithTooManyRegisteredAdmin()
157
    {
158
        $this->expectException(\RuntimeException::class);
159
160
        $this->pool->setAdminClasses([
161
            'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'],
162
        ]);
163
164
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
165
        $this->pool->getAdminByClass('someclass');
166
    }
167
168
    public function testGetAdminForClassWhenAdminClassIsSet()
169
    {
170
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
171
        $this->pool->setAdminClasses([
172
            'someclass' => ['sonata.user.admin.group1'],
173
        ]);
174
175
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
176
        $this->assertSame('sonata_user_admin_group1_AdminClass', $this->pool->getAdminByClass('someclass'));
177
    }
178
179
    public function testGetInstanceWithUndefinedServiceId()
180
    {
181
        $this->expectException(\InvalidArgumentException::class);
182
        $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.');
183
184
        $this->pool->getInstance('sonata.news.admin.post');
185
    }
186
187
    public function testGetAdminByAdminCode()
188
    {
189
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
190
191
        $this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
192
    }
193
194
    public function testGetAdminByAdminCodeForChildClass()
195
    {
196
        $adminMock = $this->getMockBuilder(AdminInterface::class)
197
            ->disableOriginalConstructor()
198
            ->getMock();
199
        $adminMock->expects($this->any())
200
            ->method('hasChild')
201
            ->will($this->returnValue(true));
202
        $adminMock->expects($this->once())
203
            ->method('getChild')
204
            ->with($this->equalTo('sonata.news.admin.comment'))
205
            ->will($this->returnValue('commentAdminClass'));
206
207
        $containerMock = $this->createMock(ContainerInterface::class);
208
        $containerMock->expects($this->any())
209
            ->method('get')
210
            ->will($this->returnValue($adminMock));
211
212
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
213
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
214
215
        $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
216
    }
217
218
    public function testGetAdminByAdminCodeForChildInvalidClass()
219
    {
220
        $adminMock = $this->getMockBuilder(AdminInterface::class)
221
            ->disableOriginalConstructor()
222
            ->getMock();
223
        $adminMock->expects($this->any())
224
            ->method('hasChild')
225
            ->will($this->returnValue(false));
226
227
        $containerMock = $this->createMock(ContainerInterface::class);
228
        $containerMock->expects($this->any())
229
            ->method('get')
230
            ->will($this->returnValue($adminMock));
231
232
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
233
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
234
235
        $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
236
    }
237
238
    public function testGetAdminClasses()
239
    {
240
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
241
        $this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses());
242
    }
243
244
    public function testGetAdminGroups()
245
    {
246
        $this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']);
247
        $this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups());
248
    }
249
250
    public function testGetAdminServiceIds()
251
    {
252
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
253
        $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds());
254
    }
255
256
    public function testGetContainer()
257
    {
258
        $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer());
259
    }
260
261
    /**
262
     * @group legacy
263
     */
264
    public function testTemplate()
265
    {
266
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
267
        $templateRegistry->getTemplate('ajax')
268
            ->shouldBeCalledTimes(1)
269
            ->willReturn('Foo.html.twig');
270
271
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
272
273
        $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.x, 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...
274
    }
275
276
    /**
277
     * @group legacy
278
     */
279
    public function testSetGetTemplates()
280
    {
281
        $templates = [
282
            'ajax' => 'Foo.html.twig',
283
            'layout' => 'Bar.html.twig',
284
        ];
285
286
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
287
        $templateRegistry->setTemplates($templates)
288
            ->shouldBeCalledTimes(1);
289
        $templateRegistry->getTemplates()
290
            ->shouldBeCalledTimes(1)
291
            ->willReturn($templates);
292
293
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
294
295
        $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.x, 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...
296
297
        $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.x, 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...
298
    }
299
300
    public function testGetTitleLogo()
301
    {
302
        $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
303
    }
304
305
    public function testGetTitle()
306
    {
307
        $this->assertSame('Sonata Admin', $this->pool->getTitle());
308
    }
309
310
    public function testGetOption()
311
    {
312
        $this->assertSame('bar', $this->pool->getOption('foo'));
313
314
        $this->assertNull($this->pool->getOption('non_existent_option'));
315
    }
316
317
    public function testOptionDefault()
318
    {
319
        $this->assertSame([], $this->pool->getOption('nonexistantarray', []));
320
    }
321
322
    /**
323
     * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
324
     */
325
    private function getContainer()
326
    {
327
        $containerMock = $this->createMock(ContainerInterface::class);
328
        $containerMock->expects($this->any())
329
            ->method('get')
330
            ->will($this->returnCallback(function ($serviceId) {
331
                return str_replace('.', '_', $serviceId).'_AdminClass';
332
            }));
333
334
        return $containerMock;
335
    }
336
337
    private function getItemArray($serviceId)
338
    {
339
        return [
340
            'admin' => $serviceId,
341
            'label' => '',
342
            'route' => '',
343
            'route_params' => [],
344
        ];
345
    }
346
}
347