Completed
Push — 3.x ( f7534c...26d69b )
by Grégoire
04:05
created

PoolTest::testGetGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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 Sonata\AdminBundle\Admin\Pool;
15
use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
16
17
class PoolTest extends PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var Pool
21
     */
22
    private $pool = null;
23
24
    public function setUp()
25
    {
26
        $this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png', array('foo' => 'bar'));
27
    }
28
29
    public function testGetGroups()
30
    {
31
        $this->pool->setAdminServiceIds(array('sonata.user.admin.group1'));
32
33
        $this->pool->setAdminGroups(array(
34
            'adminGroup1' => array('sonata.user.admin.group1' => array()),
35
        ));
36
37
        $expectedOutput = array(
38
            'adminGroup1' => array(
39
                'sonata.user.admin.group1' => 'sonata_user_admin_group1_AdminClass',
40
            ),
41
        );
42
43
        $this->assertSame($expectedOutput, $this->pool->getGroups());
44
    }
45
46
    public function testHasGroup()
47
    {
48
        $this->pool->setAdminGroups(array(
49
                'adminGroup1' => array(),
50
            ));
51
52
        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
53
        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
54
    }
55
56
    public function testGetDashboardGroups()
57
    {
58
        $admin_group1 = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
59
        $admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true));
60
61
        $admin_group2 = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
62
        $admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false));
63
64
        $admin_group3 = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
65
        $admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false));
66
67
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
68
69
        $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
70
            $admin_group1, $admin_group2, $admin_group3
71
        ));
72
73
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
74
        $pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
75
76
        $pool->setAdminGroups(array(
77
            'adminGroup1' => array(
78
                'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group1')),
79
            ),
80
            'adminGroup2' => array(
81
                'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group2')),
82
            ),
83
            'adminGroup3' => array(
84
                'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group3')),
85
            ),
86
        ));
87
88
        $groups = $pool->getDashboardGroups();
89
90
        $this->assertCount(1, $groups);
91
        $this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']);
92
    }
93
94
    /**
95
     * @expectedException \InvalidArgumentException
96
     */
97
    public function testGetAdminsByGroupWhenGroupNotSet()
98
    {
99
        $this->pool->setAdminGroups(array(
100
                'adminGroup1' => array(),
101
            ));
102
103
        $this->pool->getAdminsByGroup('adminGroup2');
104
    }
105
106
    public function testGetAdminsByGroupWhenGroupIsEmpty()
107
    {
108
        $this->pool->setAdminGroups(array(
109
                'adminGroup1' => array(),
110
            ));
111
112
        $this->assertSame(array(), $this->pool->getAdminsByGroup('adminGroup1'));
113
    }
114
115
    public function testGetAdminsByGroup()
116
    {
117
        $this->pool->setAdminServiceIds(array('sonata.admin1', 'sonata.admin2', 'sonata.admin3'));
118
        $this->pool->setAdminGroups(array(
119
            'adminGroup1' => array(
120
                'items' => array(
121
                    $this->getItemArray('sonata.admin1'),
122
                    $this->getItemArray('sonata.admin2'),
123
                ),
124
            ),
125
            'adminGroup2' => array(
126
                'items' => array($this->getItemArray('sonata.admin3')),
127
            ),
128
        ));
129
130
        $this->assertEquals(array(
131
            'sonata_admin1_AdminClass',
132
            'sonata_admin2_AdminClass',
133
        ), $this->pool->getAdminsByGroup('adminGroup1'));
134
        $this->assertEquals(array('sonata_admin3_AdminClass'), $this->pool->getAdminsByGroup('adminGroup2'));
135
    }
136
137
    public function testGetAdminForClassWhenAdminClassIsNotSet()
138
    {
139
        $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
140
        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
141
        $this->assertNull($this->pool->getAdminByClass('notexists'));
142
    }
143
144
    /**
145
     * @expectedException \RuntimeException
146
     */
147
    public function testGetAdminForClassWithInvalidFormat()
148
    {
149
        $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
150
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
151
152
        $this->pool->getAdminByClass('someclass');
153
    }
154
155
    /**
156
     * @expectedException \RuntimeException
157
     */
158
    public function testGetAdminForClassWithTooManyRegisteredAdmin()
159
    {
160
        $this->pool->setAdminClasses(array(
161
            'someclass' => array('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(array('sonata.user.admin.group1'));
171
        $this->pool->setAdminClasses(array(
172
            'someclass' => array('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
    /**
180
     * @expectedException \InvalidArgumentException
181
     * @expectedExceptionMessage Admin service "sonata.news.admin.post" not found in admin pool.
182
     */
183
    public function testGetInstanceWithUndefinedServiceId()
184
    {
185
        $this->pool->getInstance('sonata.news.admin.post');
186
    }
187
188
    public function testGetAdminByAdminCode()
189
    {
190
        $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
191
192
        $this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
193
    }
194
195
    public function testGetAdminByAdminCodeForChildClass()
196
    {
197
        $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
198
            ->disableOriginalConstructor()
199
            ->getMock();
200
        $adminMock->expects($this->any())
201
            ->method('hasChild')
202
            ->will($this->returnValue(true));
203
        $adminMock->expects($this->once())
204
            ->method('getChild')
205
            ->with($this->equalTo('sonata.news.admin.comment'))
206
            ->will($this->returnValue('commentAdminClass'));
207
208
        $containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
209
        $containerMock->expects($this->any())
210
            ->method('get')
211
            ->will($this->returnValue($adminMock));
212
213
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
214
        $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
215
216
        $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
217
    }
218
219
    public function testGetAdminByAdminCodeForChildInvalidClass()
220
    {
221
        $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
222
            ->disableOriginalConstructor()
223
            ->getMock();
224
        $adminMock->expects($this->any())
225
            ->method('hasChild')
226
            ->will($this->returnValue(false));
227
228
        $containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
229
        $containerMock->expects($this->any())
230
            ->method('get')
231
            ->will($this->returnValue($adminMock));
232
233
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
234
        $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
235
236
        $this->assertSame(false, $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
237
    }
238
239
    public function testGetAdminClasses()
240
    {
241
        $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
242
        $this->assertSame(array('someclass' => 'sonata.user.admin.group1'), $this->pool->getAdminClasses());
243
    }
244
245
    public function testGetAdminGroups()
246
    {
247
        $this->pool->setAdminGroups(array('adminGroup1' => 'sonata.user.admin.group1'));
248
        $this->assertSame(array('adminGroup1' => 'sonata.user.admin.group1'), $this->pool->getAdminGroups());
249
    }
250
251
    public function testGetAdminServiceIds()
252
    {
253
        $this->pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
254
        $this->assertSame(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'), $this->pool->getAdminServiceIds());
255
    }
256
257
    public function testGetContainer()
258
    {
259
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->pool->getContainer());
260
    }
261
262
    public function testTemplates()
263
    {
264
        $this->assertInternalType('array', $this->pool->getTemplates());
265
266
        $this->pool->setTemplates(array('ajax' => 'Foo.html.twig'));
267
268
        $this->assertNull($this->pool->getTemplate('bar'));
269
        $this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax'));
270
    }
271
272
    public function testGetTitleLogo()
273
    {
274
        $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
275
    }
276
277
    public function testGetTitle()
278
    {
279
        $this->assertSame('Sonata Admin', $this->pool->getTitle());
280
    }
281
282
    public function testGetOption()
283
    {
284
        $this->assertSame('bar', $this->pool->getOption('foo'));
285
286
        $this->assertSame(null, $this->pool->getOption('non_existent_option'));
287
    }
288
289
    public function testOptionDefault()
290
    {
291
        $this->assertSame(array(), $this->pool->getOption('nonexistantarray', array()));
292
    }
293
294
    /**
295
     * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
296
     */
297
    private function getContainer()
298
    {
299
        $containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
300
        $containerMock->expects($this->any())
301
            ->method('get')
302
            ->will($this->returnCallback(function ($serviceId) {
303
                return str_replace('.', '_', $serviceId).'_AdminClass';
304
            }));
305
306
        return $containerMock;
307
    }
308
309
    private function getItemArray($serviceId)
310
    {
311
        return array(
312
            'admin' => $serviceId,
313
            'label' => '',
314
            'route' => '',
315
            'route_params' => array(),
316
        );
317
    }
318
}
319