Completed
Push — 3.x ( 647698...abeb07 )
by Oskar
04:38
created

getInvalidChildAdminServiceNamesToCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
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 Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
class PoolTest extends TestCase
24
{
25
    /**
26
     * @var Pool
27
     */
28
    private $pool = null;
29
30
    public function setUp(): void
31
    {
32
        $this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']);
33
    }
34
35
    public function testGetGroups(): void
36
    {
37
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
38
39
        $this->pool->setAdminGroups([
40
            'adminGroup1' => ['sonata.user.admin.group1' => []],
41
        ]);
42
43
        $result = $this->pool->getGroups();
44
        $this->assertArrayHasKey('adminGroup1', $result);
45
        $this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']);
46
    }
47
48
    public function testHasGroup(): void
49
    {
50
        $this->pool->setAdminGroups([
51
                'adminGroup1' => [],
52
            ]);
53
54
        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
55
        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
56
    }
57
58
    public function testGetDashboardGroups(): void
59
    {
60
        $admin_group1 = $this->createMock(AdminInterface::class);
61
        $admin_group1->expects($this->once())->method('showIn')->willReturn(true);
62
63
        $admin_group2 = $this->createMock(AdminInterface::class);
64
        $admin_group2->expects($this->once())->method('showIn')->willReturn(false);
65
66
        $admin_group3 = $this->createMock(AdminInterface::class);
67
        $admin_group3->expects($this->once())->method('showIn')->willReturn(false);
68
69
        $container = $this->createMock(ContainerInterface::class);
70
71
        $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
72
            $admin_group1, $admin_group2, $admin_group3
73
        ));
74
75
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
0 ignored issues
show
Documentation introduced by
$container 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...
76
        $pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
77
78
        $pool->setAdminGroups([
79
            'adminGroup1' => [
80
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')],
81
            ],
82
            'adminGroup2' => [
83
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')],
84
            ],
85
            'adminGroup3' => [
86
                'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')],
87
            ],
88
        ]);
89
90
        $groups = $pool->getDashboardGroups();
91
92
        $this->assertCount(1, $groups);
0 ignored issues
show
Documentation introduced by
$groups is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
93
        $this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']);
94
    }
95
96
    public function testGetAdminsByGroupWhenGroupNotSet(): void
97
    {
98
        $this->expectException(\InvalidArgumentException::class);
99
100
        $this->pool->setAdminGroups([
101
                'adminGroup1' => [],
102
            ]);
103
104
        $this->pool->getAdminsByGroup('adminGroup2');
105
    }
106
107
    public function testGetAdminsByGroupWhenGroupIsEmpty(): void
108
    {
109
        $this->pool->setAdminGroups([
110
                'adminGroup1' => [],
111
            ]);
112
113
        $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1'));
114
    }
115
116
    public function testGetAdminsByGroup(): void
117
    {
118
        $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']);
119
        $this->pool->setAdminGroups([
120
            'adminGroup1' => [
121
                'items' => [
122
                    $this->getItemArray('sonata.admin1'),
123
                    $this->getItemArray('sonata.admin2'),
124
                ],
125
            ],
126
            'adminGroup2' => [
127
                'items' => [$this->getItemArray('sonata.admin3')],
128
            ],
129
        ]);
130
131
        $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
0 ignored issues
show
Documentation introduced by
$this->pool->getAdminsByGroup('adminGroup1') is of type array<integer,object<Son...\Admin\AdminInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
132
        $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
0 ignored issues
show
Documentation introduced by
$this->pool->getAdminsByGroup('adminGroup2') is of type array<integer,object<Son...\Admin\AdminInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
133
    }
134
135
    public function testGetAdminForClassWhenAdminClassIsNotSet(): void
136
    {
137
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
138
        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
139
        $this->assertNull($this->pool->getAdminByClass('notexists'));
140
    }
141
142
    public function testGetAdminForClassWithInvalidFormat(): void
143
    {
144
        $this->expectException(\RuntimeException::class);
145
146
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
147
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
148
149
        $this->pool->getAdminByClass('someclass');
150
    }
151
152
    public function testGetAdminForClassWithTooManyRegisteredAdmin(): void
153
    {
154
        $this->expectException(\RuntimeException::class);
155
156
        $this->pool->setAdminClasses([
157
            'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'],
158
        ]);
159
160
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
161
        $this->pool->getAdminByClass('someclass');
162
    }
163
164
    public function testGetAdminForClassWhenAdminClassIsSet(): void
165
    {
166
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1']);
167
        $this->pool->setAdminClasses([
168
            'someclass' => ['sonata.user.admin.group1'],
169
        ]);
170
171
        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
172
        $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByClass('someclass'));
173
    }
174
175
    public function testGetInstanceWithUndefinedServiceId(): void
176
    {
177
        $this->expectException(\InvalidArgumentException::class);
178
        $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.');
179
180
        $this->pool->getInstance('sonata.news.admin.post');
181
    }
182
183
    public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void
184
    {
185
        $this->pool->setAdminServiceIds([
186
            'sonata.news.admin.post',
187
            'sonata.news.admin.category',
188
        ]);
189
190
        $this->expectException(\InvalidArgumentException::class);
191
        $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. '
192
            .'Did you mean "sonata.news.admin.post" '
193
            .'or one of those: [sonata.news.admin.category]?');
194
195
        $this->pool->getInstance('sonata.news.admin.pos');
196
    }
197
198
    public function testGetAdminByAdminCode(): void
199
    {
200
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
201
202
        $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
203
    }
204
205
    public function testGetAdminByAdminCodeForChildClass(): void
206
    {
207
        $adminMock = $this->createMock(AdminInterface::class);
208
        $adminMock->expects($this->any())
209
            ->method('hasChild')
210
            ->willReturn(true);
211
212
        $childAdmin = $this->createMock(AdminInterface::class);
213
214
        $adminMock->expects($this->once())
215
            ->method('getChild')
216
            ->with($this->equalTo('sonata.news.admin.comment'))
217
            ->willReturn($childAdmin);
218
219
        $containerMock = $this->createMock(ContainerInterface::class);
220
        $containerMock->expects($this->any())
221
            ->method('get')
222
            ->willReturn($adminMock);
223
224
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
225
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']);
226
227
        $this->assertSame($childAdmin, $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
228
    }
229
230
    /**
231
     * @group legacy
232
     *
233
     * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.
234
     */
235
    public function testGetAdminByAdminCodeWithInvalidCode(): void
236
    {
237
        $adminMock = $this->createMock(AdminInterface::class);
238
        $adminMock->expects($this->any())
239
            ->method('hasChild')
240
            ->willReturn(false);
241
242
        $containerMock = $this->createMock(ContainerInterface::class);
243
        $containerMock->expects($this->any())
244
            ->method('get')
245
            ->willReturn($adminMock);
246
247
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
248
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
249
250
        // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line
251
        // $this->expectException(\InvalidArgumentException::class);
252
        $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
253
    }
254
255
    /**
256
     * @dataProvider getNonStringAdminServiceNames
257
     *
258
     * @group legacy
259
     *
260
     * @expectedDeprecation Passing a non string value as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.51 and will cause a \TypeError in 4.0.
261
     */
262
    public function testGetAdminByAdminCodeWithNonStringCode($adminId): void
263
    {
264
        // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line
265
        // $this->expectException(\TypeError::class);
266
        $this->assertFalse($this->pool->getAdminByAdminCode($adminId));
267
    }
268
269
    public function getNonStringAdminServiceNames()
270
    {
271
        return [
272
            [null],
273
            [false],
274
            [1],
275
            [['some_value']],
276
            [new \stdClass()],
277
        ];
278
    }
279
280
    /**
281
     * @group legacy
282
     *
283
     * @expectedDeprecation Passing an invalid admin hierarchy inside argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.51 and will throw an exception in 4.0.
284
     */
285
    public function testGetAdminByAdminCodeWithCodeNotChild(): void
286
    {
287
        $adminMock = $this->createMock(AdminInterface::class);
288
        $adminMock->expects($this->any())
289
            ->method('hasChild')
290
            ->willReturn(false);
291
292
        $containerMock = $this->createMock(ContainerInterface::class);
293
        $containerMock->expects($this->any())
294
            ->method('get')
295
            ->willReturn($adminMock);
296
297
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
298
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.valid']);
299
        $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
300
301
        // NEXT_MAJOR: remove the "@group" and "@expectedDeprecation" annotations, the previous assertion and uncomment the following lines
302
        // $this->expectException(\InvalidArgumentException::class);
303
        // $this->expectExceptionMessage('Argument 1 passed to Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() must contain a valid admin hierarchy, "sonata.news.admin.valid" is not a valid child for "sonata.news.admin.post"');
304
        //
305
        // $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid');
306
    }
307
308
    /**
309
     * @dataProvider getEmptyRootAdminServiceNames
310
     */
311
    public function testGetAdminByAdminCodeWithInvalidRootCode(string $adminId): void
312
    {
313
        $adminMock = $this->createMock(AdminInterface::class);
314
        $adminMock->expects($this->never())
315
            ->method('hasChild');
316
317
        $containerMock = $this->createMock(ContainerInterface::class);
318
        $containerMock->expects($this->never())
319
            ->method('get');
320
321
        /** @var MockObject|Pool $poolMock */
322
        $poolMock = $this->getMockBuilder(Pool::class)
323
            ->setConstructorArgs([$containerMock, 'Sonata', '/path/to/logo.png'])
324
            ->disableOriginalClone()
325
            ->setMethodsExcept(['getAdminByAdminCode'])
326
            ->getMock();
327
        $poolMock->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Sonata\AdminBundle\Admin\Pool.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
328
            ->method('getInstance');
329
330
        $this->expectException(\InvalidArgumentException::class);
331
        $this->expectExceptionMessage('Root admin code must contain a valid admin reference, empty string given.');
332
        $poolMock->getAdminByAdminCode($adminId);
0 ignored issues
show
Bug introduced by
The method getAdminByAdminCode does only exist in Sonata\AdminBundle\Admin\Pool, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
333
    }
334
335
    public function getEmptyRootAdminServiceNames()
336
    {
337
        return [
338
            [''],
339
            ['   '],
340
            ['|sonata.news.admin.child_of_empty_code'],
341
        ];
342
    }
343
344
    /**
345
     * @dataProvider getInvalidChildAdminServiceNames
346
     *
347
     * @group legacy
348
     *
349
     * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.
350
     */
351
    public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void
352
    {
353
        $adminMock = $this->createMock(AdminInterface::class);
354
        $adminMock->expects($this->any())
355
            ->method('hasChild')
356
            ->willReturn(false);
357
        $adminMock->expects($this->never())
358
            ->method('getChild');
359
360
        $containerMock = $this->createMock(ContainerInterface::class);
361
        $containerMock->expects($this->never())
362
            ->method('get');
363
364
        /** @var MockObject|Pool $poolMock */
365
        $poolMock = $this->getMockBuilder(Pool::class)
366
            ->setConstructorArgs([$containerMock, 'Sonata', '/path/to/logo.png'])
367
            ->disableOriginalClone()
368
            ->setMethodsExcept(['getAdminByAdminCode'])
369
            ->getMock();
370
        $poolMock->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Sonata\AdminBundle\Admin\Pool.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
371
            ->method('getInstance')
372
            ->willReturn($adminMock);
373
374
        // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line
375
        // $this->expectException(\InvalidArgumentException::class);
376
        $this->assertFalse($poolMock->getAdminByAdminCode($adminId));
0 ignored issues
show
Bug introduced by
The method getAdminByAdminCode does only exist in Sonata\AdminBundle\Admin\Pool, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
377
    }
378
379
    public function getInvalidChildAdminServiceNames()
380
    {
381
        return [
382
            ['admin1|'],
383
            ['admin1|nonexistent_code'],
384
            ['admin1||admin3'],
385
        ];
386
    }
387
388
    /**
389
     * @dataProvider getAdminServiceNamesToCheck
390
     */
391
    public function testHasAdminByAdminCode(string $adminId): void
392
    {
393
        $adminMock = $this->createMock(AdminInterface::class);
394
395
        if (false !== strpos($adminId, '|')) {
396
            $childAdminMock = $this->createMock(AdminInterface::class);
397
            $adminMock->expects($this->any())
398
                ->method('hasChild')
399
                ->willReturn(true);
400
            $adminMock->expects($this->once())
401
                ->method('getChild')
402
                ->with($this->equalTo('sonata.news.admin.comment'))
403
                ->willReturn($childAdminMock);
404
        } else {
405
            $adminMock->expects($this->never())
406
                ->method('hasChild');
407
            $adminMock->expects($this->never())
408
                ->method('getChild');
409
        }
410
411
        $containerMock = $this->createMock(ContainerInterface::class);
412
        $containerMock->expects($this->any())
413
            ->method('get')
414
            ->willReturn($adminMock);
415
416
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
417
        $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']);
418
419
        $this->assertTrue($this->pool->hasAdminByAdminCode($adminId));
420
    }
421
422
    public function getAdminServiceNamesToCheck()
423
    {
424
        return [
425
            ['sonata.news.admin.post'],
426
            ['sonata.news.admin.post|sonata.news.admin.comment'],
427
        ];
428
    }
429
430
    /**
431
     * @dataProvider getNonStringAdminServiceNames
432
     */
433
    public function testHasAdminByAdminCodeWithNonStringCode($adminId): void
434
    {
435
        $this->expectException(\TypeError::class);
436
        $this->pool->hasAdminByAdminCode($adminId);
437
    }
438
439
    /**
440
     * @dataProvider getInvalidAdminServiceNamesToCheck
441
     */
442
    public function testHasAdminByAdminCodeWithInvalidCodes(string $adminId): void
443
    {
444
        $adminMock = $this->createMock(AdminInterface::class);
445
        $adminMock->expects($this->any())
446
            ->method('hasChild')
447
            ->willReturn(false);
448
        $adminMock->expects($this->never())
449
            ->method('getChild');
450
451
        $containerMock = $this->createMock(ContainerInterface::class);
452
        $containerMock->expects($this->never())
453
            ->method('get');
454
455
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
456
457
        $this->assertFalse($this->pool->hasAdminByAdminCode($adminId));
458
    }
459
460
    public function getInvalidAdminServiceNamesToCheck()
461
    {
462
        return [
463
            [''],
464
            ['   '],
465
            ['|sonata.news.admin.child_of_empty_code'],
466
        ];
467
    }
468
469
    public function testHasAdminByAdminCodeWithNonExistentCode(): void
470
    {
471
        $containerMock = $this->createMock(ContainerInterface::class);
472
        $containerMock->expects($this->never())
473
            ->method('get');
474
475
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
476
477
        $this->assertFalse($this->pool->hasAdminByAdminCode('sonata.news.admin.nonexistent_code'));
478
    }
479
480
    /**
481
     * @dataProvider getInvalidChildAdminServiceNamesToCheck
482
     *
483
     * @group legacy
484
     *
485
     * @expectedDeprecation Passing an invalid admin %s argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.%s and will throw an exception in 4.0.
486
     */
487
    public function testHasAdminByAdminCodeWithInvalidChildCodes(string $adminId): void
488
    {
489
        $adminMock = $this->createMock(AdminInterface::class);
490
        $adminMock->expects($this->any())
491
            ->method('hasChild')
492
            ->willReturn(false);
493
        $adminMock->expects($this->never())
494
            ->method('getChild');
495
496
        $containerMock = $this->createMock(ContainerInterface::class);
497
        $containerMock->expects($this->once())
498
            ->method('get')
499
            ->willReturn($adminMock);
500
501
        $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
0 ignored issues
show
Documentation introduced by
$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...
502
        $this->pool->setAdminServiceIds(['sonata.news.admin.post']);
503
504
        $this->assertFalse($this->pool->hasAdminByAdminCode($adminId));
505
    }
506
507
    public function getInvalidChildAdminServiceNamesToCheck()
508
    {
509
        return [
510
            ['sonata.news.admin.post|'],
511
            ['sonata.news.admin.post|nonexistent_code'],
512
            ['sonata.news.admin.post||admin3'],
513
        ];
514
    }
515
516
    public function testGetAdminClasses(): void
517
    {
518
        $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']);
519
        $this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses());
520
    }
521
522
    public function testGetAdminGroups(): void
523
    {
524
        $this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']);
525
        $this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups());
526
    }
527
528
    public function testGetAdminServiceIds(): void
529
    {
530
        $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']);
531
        $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds());
532
    }
533
534
    public function testGetContainer(): void
535
    {
536
        $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer());
537
    }
538
539
    /**
540
     * @group legacy
541
     */
542
    public function testTemplate(): void
543
    {
544
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
545
        $templateRegistry->getTemplate('ajax')
546
            ->shouldBeCalledTimes(1)
547
            ->willReturn('Foo.html.twig');
548
549
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
550
551
        $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...
552
    }
553
554
    /**
555
     * @group legacy
556
     */
557
    public function testSetGetTemplates(): void
558
    {
559
        $templates = [
560
            'ajax' => 'Foo.html.twig',
561
            'layout' => 'Bar.html.twig',
562
        ];
563
564
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
565
        $templateRegistry->setTemplates($templates)
566
            ->shouldBeCalledTimes(1);
567
        $templateRegistry->getTemplates()
568
            ->shouldBeCalledTimes(1)
569
            ->willReturn($templates);
570
571
        $this->pool->setTemplateRegistry($templateRegistry->reveal());
572
573
        $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...
574
575
        $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...
576
    }
577
578
    public function testGetTitleLogo(): void
579
    {
580
        $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
581
    }
582
583
    public function testGetTitle(): void
584
    {
585
        $this->assertSame('Sonata Admin', $this->pool->getTitle());
586
    }
587
588
    public function testGetOption(): void
589
    {
590
        $this->assertSame('bar', $this->pool->getOption('foo'));
591
592
        $this->assertNull($this->pool->getOption('non_existent_option'));
593
    }
594
595
    public function testOptionDefault(): void
596
    {
597
        $this->assertSame([], $this->pool->getOption('nonexistantarray', []));
598
    }
599
600
    private function getContainer(): ContainerInterface
601
    {
602
        $containerMock = $this->createMock(ContainerInterface::class);
603
        $containerMock->expects($this->any())
604
            ->method('get')
605
            ->willReturnCallback(function () {
606
                return $this->createMock(AdminInterface::class);
607
            });
608
609
        return $containerMock;
610
    }
611
612
    private function getItemArray($serviceId): array
613
    {
614
        return [
615
            'admin' => $serviceId,
616
            'label' => '',
617
            'route' => '',
618
            'route_params' => [],
619
        ];
620
    }
621
}
622