Completed
Push — master ( dd5bf6...b2992c )
by Javier
16s queued 10s
created

PoolTest::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
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\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Symfony\Component\DependencyInjection\Container;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
class PoolTest extends TestCase
24
{
25
    /**
26
     * @var Container
27
     */
28
    private $container;
29
30
    /**
31
     * @var Pool
32
     */
33
    private $pool;
34
35
    public function setUp(): void
36
    {
37
        $this->container = new Container();
38
        $this->pool = new Pool($this->container, 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']);
39
    }
40
41
    public function testGetGroups(): void
42
    {
43
        $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class));
44
45
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
46
47
        $this->pool->setAdminGroups([
48
            'adminGroup1' => ['sonata.user.admin.group1' => []],
49
        ]);
50
51
        $result = $this->pool->getGroups();
52
        $this->assertArrayHasKey('adminGroup1', $result);
53
        $this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']);
54
    }
55
56
    public function testHasGroup(): void
57
    {
58
        $this->pool->setAdminGroups([
59
            'adminGroup1' => [],
60
        ]);
61
62
        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
63
        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
64
    }
65
66
    public function testGetDashboardGroups(): void
67
    {
68
        $adminGroup1 = $this->createMock(AdminInterface::class);
69
        $adminGroup1->expects($this->once())->method('showIn')->willReturn(true);
70
71
        $adminGroup2 = $this->createMock(AdminInterface::class);
72
        $adminGroup2->expects($this->once())->method('showIn')->willReturn(false);
73
74
        $adminGroup3 = $this->createMock(AdminInterface::class);
75
        $adminGroup3->expects($this->once())->method('showIn')->willReturn(false);
76
77
        $this->container->set('sonata.user.admin.group1', $adminGroup1);
78
        $this->container->set('sonata.user.admin.group2', $adminGroup2);
79
        $this->container->set('sonata.user.admin.group3', $adminGroup3);
80
81
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
82
83
        $this->pool->setAdminGroups([
84
            'adminGroup1' => [
85
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')],
86
            ],
87
            'adminGroup2' => [
88
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')],
89
            ],
90
            'adminGroup3' => [
91
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')],
92
            ],
93
        ]);
94
95
        $groups = $this->pool->getDashboardGroups();
96
97
        $this->assertCount(1, $groups);
98
        $this->assertSame($adminGroup1, $groups['adminGroup1']['items']['itemKey']);
99
    }
100
101
    public function testGetAdminsByGroupWhenGroupNotSet(): void
102
    {
103
        $this->expectException(\InvalidArgumentException::class);
104
105
        $this->pool->setAdminGroups([
106
                'adminGroup1' => [],
107
            ]);
108
109
        $this->pool->getAdminsByGroup('adminGroup2');
110
    }
111
112
    public function testGetAdminsByGroupWhenGroupIsEmpty(): void
113
    {
114
        $this->pool->setAdminGroups([
115
                'adminGroup1' => [],
116
            ]);
117
118
        $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1'));
119
    }
120
121
    public function testGetAdminsByGroup(): void
122
    {
123
        $this->container->set('sonata.admin1', $this->createMock(AdminInterface::class));
124
        $this->container->set('sonata.admin2', $this->createMock(AdminInterface::class));
125
        $this->container->set('sonata.admin3', $this->createMock(AdminInterface::class));
126
127
        $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']);
128
        $this->pool->setAdminGroups([
129
            'adminGroup1' => [
130
                'items' => [
131
                    $this->getItemArray('sonata.admin1'),
132
                    $this->getItemArray('sonata.admin2'),
133
                ],
134
            ],
135
            'adminGroup2' => [
136
                'items' => [$this->getItemArray('sonata.admin3')],
137
            ],
138
        ]);
139
140
        $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
141
        $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
142
    }
143
144
    public function testGetAdminForClassWhenAdminClassIsNotSet(): void
145
    {
146
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
147
        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
148
        $this->assertNull($this->pool->getAdminByClass('notexists'));
149
    }
150
151
    public function testGetAdminForClassWithInvalidFormat(): void
152
    {
153
        $this->expectException(\RuntimeException::class);
154
155
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
156
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
157
158
        $this->pool->getAdminByClass('someclass');
159
    }
160
161
    public function testGetAdminForClassWithTooManyRegisteredAdmin(): void
162
    {
163
        $this->expectException(\RuntimeException::class);
164
165
        $this->pool->setAdminClasses([
166
            'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'],
167
        ]);
168
169
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
170
        $this->pool->getAdminByClass('someclass');
171
    }
172
173
    public function testGetAdminForClassWhenAdminClassIsSet(): void
174
    {
175
        $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class));
176
177
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
178
        $this->pool->setAdminClasses([
179
            'someclass' => ['sonata.user.admin.group1'],
180
        ]);
181
182
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
183
        $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByClass('someclass'));
184
    }
185
186
    public function testGetInstanceWithUndefinedServiceId(): void
187
    {
188
        $this->expectException(\InvalidArgumentException::class);
189
        $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.');
190
191
        $this->pool->getInstance('sonata.news.admin.post');
192
    }
193
194
    public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void
195
    {
196
        $this->pool->setAdminServiceIds([
197
            'sonata.news.admin.post',
198
            'sonata.news.admin.category',
199
        ]);
200
201
        $this->expectException(\InvalidArgumentException::class);
202
        $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. '
203
            .'Did you mean "sonata.news.admin.post" '
204
            .'or one of those: [sonata.news.admin.category]?');
205
206
        $this->pool->getInstance('sonata.news.admin.pos');
207
    }
208
209
    public function testGetAdminByAdminCode(): void
210
    {
211
        $this->container->set('sonata.news.admin.post', $this->createMock(AdminInterface::class));
212
213
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
214
215
        $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
216
    }
217
218
    public function testGetAdminByAdminCodeForChildClass(): void
219
    {
220
        $adminMock = $this->createMock(AdminInterface::class);
221
        $adminMock
222
            ->method('hasChild')
223
            ->willReturn(true);
224
225
        $childAdmin = $this->createMock(AdminInterface::class);
226
227
        $adminMock->expects($this->once())
228
            ->method('getChild')
229
            ->with($this->equalTo('sonata.news.admin.comment'))
230
            ->willReturn($childAdmin);
231
232
        $this->container->set('sonata.news.admin.post', $adminMock);
233
234
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']);
235
236
        $this->assertSame($childAdmin, $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
237
    }
238
239
    public function testGetAdminByAdminCodeWithInvalidCode(): void
240
    {
241
        $adminMock = $this->createMock(AdminInterface::class);
242
        $adminMock
243
            ->method('hasChild')
244
            ->willReturn(false);
245
246
        $this->container->set('sonata.news.admin.post', $adminMock);
247
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
248
249
        $this->expectException(\InvalidArgumentException::class);
250
        $this->expectExceptionMessage('Argument 1 passed to Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() must contain a valid admin reference, "sonata.news.admin.invalid" found at "sonata.news.admin.post|sonata.news.admin.invalid".');
251
252
        $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid');
253
    }
254
255
    public function testGetAdminByAdminCodeWithCodeNotChild(): void
256
    {
257
        $adminMock = $this->getMockBuilder(AdminInterface::class)
258
            ->disableOriginalConstructor()
259
            ->getMock();
260
        $adminMock->expects($this->any())
261
            ->method('hasChild')
262
            ->willReturn(false);
263
264
        $containerMock = $this->createMock(ContainerInterface::class);
265
        $containerMock->expects($this->any())
266
            ->method('get')
267
            ->willReturn($adminMock);
268
269
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
270
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.valid']);
271
272
        $this->expectException(\InvalidArgumentException::class);
273
        $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid');
274
    }
275
276
    /**
277
     * @dataProvider getNonStringAdminServiceNames
278
     */
279
    public function testGetAdminByAdminCodeWithNonStringCode($adminId): void
280
    {
281
        $this->expectException(\TypeError::class);
282
283
        $this->pool->getAdminByAdminCode($adminId);
284
    }
285
286
    public function getNonStringAdminServiceNames(): array
287
    {
288
        return [
289
            [null],
290
            [false],
291
            [1],
292
            [['some_value']],
293
            [new \stdClass()],
294
        ];
295
    }
296
297
    /**
298
     * @dataProvider getEmptyRootAdminServiceNames
299
     */
300
    public function testGetAdminByAdminCodeWithInvalidRootCode(string $adminId): void
301
    {
302
        $adminMock = $this->createMock(AdminInterface::class);
303
        $adminMock->expects($this->never())
304
            ->method('hasChild');
305
306
        /** @var MockObject|Pool $poolMock */
307
        $poolMock = $this->getMockBuilder(Pool::class)
308
            ->setConstructorArgs([$this->container, 'Sonata', '/path/to/logo.png'])
309
            ->disableOriginalClone()
310
            ->setMethodsExcept(['getAdminByAdminCode'])
311
            ->getMock();
312
        $poolMock->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
            ->method('getInstance');
314
315
        $this->expectException(\InvalidArgumentException::class);
316
        $this->expectExceptionMessage('Root admin code must contain a valid admin reference, empty string given.');
317
        $poolMock->getAdminByAdminCode($adminId);
318
    }
319
320
    public function getEmptyRootAdminServiceNames()
321
    {
322
        return [
323
            [''],
324
            ['   '],
325
            ['|sonata.news.admin.child_of_empty_code'],
326
        ];
327
    }
328
329
    /**
330
     * @dataProvider getInvalidChildAdminServiceNames
331
     */
332
    public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void
333
    {
334
        $adminMock = $this->createMock(AdminInterface::class);
335
        $adminMock
336
            ->method('hasChild')
337
            ->willReturn(false);
338
        $adminMock->expects($this->never())
339
            ->method('getChild');
340
341
        /** @var MockObject|Pool $poolMock */
342
        $poolMock = $this->getMockBuilder(Pool::class)
343
            ->setConstructorArgs([$this->container, 'Sonata', '/path/to/logo.png'])
344
            ->disableOriginalClone()
345
            ->setMethodsExcept(['getAdminByAdminCode'])
346
            ->getMock();
347
        $poolMock
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
348
            ->method('getInstance')
349
            ->willReturn($adminMock);
350
351
        $this->expectException(\InvalidArgumentException::class);
352
        $this->expectExceptionMessageRegExp(sprintf(
353
            '{^Argument 1 passed to Sonata\\\AdminBundle\\\Admin\\\Pool::getAdminByAdminCode\(\) must contain a valid admin reference, "[^"]+" found at "%s"\.$}',
354
            $adminId
355
        ));
356
357
        $poolMock->getAdminByAdminCode($adminId);
358
    }
359
360
    public function getInvalidChildAdminServiceNames()
361
    {
362
        return [
363
            ['admin1|'],
364
            ['admin1|nonexistent_code'],
365
            ['admin1||admin3'],
366
        ];
367
    }
368
369
    /**
370
     * @dataProvider getAdminServiceNamesToCheck
371
     */
372
    public function testHasAdminByAdminCode(string $adminId): void
373
    {
374
        $adminMock = $this->createMock(AdminInterface::class);
375
376
        if (false !== strpos($adminId, '|')) {
377
            $childAdminMock = $this->createMock(AdminInterface::class);
378
            $adminMock
379
                ->method('hasChild')
380
                ->willReturn(true);
381
            $adminMock->expects($this->once())
382
                ->method('getChild')
383
                ->with($this->equalTo('sonata.news.admin.comment'))
384
                ->willReturn($childAdminMock);
385
        } else {
386
            $adminMock->expects($this->never())
387
                ->method('hasChild');
388
            $adminMock->expects($this->never())
389
                ->method('getChild');
390
        }
391
392
        $this->container->set('sonata.news.admin.post', $adminMock);
393
394
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']);
395
396
        $this->assertTrue($this->pool->hasAdminByAdminCode($adminId));
397
    }
398
399
    public function getAdminServiceNamesToCheck()
400
    {
401
        return [
402
            ['sonata.news.admin.post'],
403
            ['sonata.news.admin.post|sonata.news.admin.comment'],
404
        ];
405
    }
406
407
    /**
408
     * @dataProvider getNonStringAdminServiceNames
409
     */
410
    public function testHasAdminByAdminCodeWithNonStringCode($adminId): void
411
    {
412
        $this->expectException(\TypeError::class);
413
        $this->pool->hasAdminByAdminCode($adminId);
414
    }
415
416
    /**
417
     * @dataProvider getInvalidAdminServiceNamesToCheck
418
     */
419
    public function testHasAdminByAdminCodeWithInvalidCodes(string $adminId): void
420
    {
421
        $adminMock = $this->createMock(AdminInterface::class);
422
        $adminMock
423
            ->method('hasChild')
424
            ->willReturn(false);
425
        $adminMock->expects($this->never())
426
            ->method('getChild');
427
428
        $this->assertFalse($this->pool->hasAdminByAdminCode($adminId));
429
    }
430
431
    public function getInvalidAdminServiceNamesToCheck()
432
    {
433
        return [
434
            [''],
435
            ['   '],
436
            ['|sonata.news.admin.child_of_empty_code'],
437
        ];
438
    }
439
440
    public function testHasAdminByAdminCodeWithNonExistentCode(): void
441
    {
442
        $this->assertFalse($this->pool->hasAdminByAdminCode('sonata.news.admin.nonexistent_code'));
443
    }
444
445
    /**
446
     * @dataProvider getInvalidChildAdminServiceNamesToCheck
447
     */
448
    public function testHasAdminByAdminCodeWithInvalidChildCodes(string $adminId): void
449
    {
450
        $adminMock = $this->createMock(AdminInterface::class);
451
        $adminMock
452
            ->method('hasChild')
453
            ->willReturn(false);
454
        $adminMock->expects($this->never())
455
            ->method('getChild');
456
457
        $this->container->set('sonata.news.admin.post', $adminMock);
458
459
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
460
461
        $this->assertFalse($this->pool->hasAdminByAdminCode($adminId));
462
    }
463
464
    public function getInvalidChildAdminServiceNamesToCheck(): array
465
    {
466
        return [
467
            ['sonata.news.admin.post|'],
468
            ['sonata.news.admin.post|nonexistent_code'],
469
            ['sonata.news.admin.post||admin3'],
470
        ];
471
    }
472
473
    public function testGetAdminClasses(): void
474
    {
475
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
476
        $this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses());
477
    }
478
479
    public function testGetAdminGroups(): void
480
    {
481
        $this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']);
482
        $this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups());
483
    }
484
485
    public function testGetAdminServiceIds(): void
486
    {
487
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
488
        $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds());
489
    }
490
491
    public function testGetContainer(): void
492
    {
493
        $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer());
494
    }
495
496
    public function testGetTitleLogo(): void
497
    {
498
        $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
499
    }
500
501
    public function testGetTitle(): void
502
    {
503
        $this->assertSame('Sonata Admin', $this->pool->getTitle());
504
    }
505
506
    public function testGetOption(): void
507
    {
508
        $this->assertSame('bar', $this->pool->getOption('foo'));
509
510
        $this->assertNull($this->pool->getOption('non_existent_option'));
511
    }
512
513
    public function testOptionDefault(): void
514
    {
515
        $this->assertSame([], $this->pool->getOption('nonexistantarray', []));
516
    }
517
518
    private function getItemArray(string $serviceId): array
519
    {
520
        return [
521
            'admin' => $serviceId,
522
            'label' => '',
523
            'route' => '',
524
            'route_params' => [],
525
        ];
526
    }
527
}
528