Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

tests/Route/QueryStringBuilderTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Route;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Model\AuditManagerInterface;
19
use Sonata\AdminBundle\Route\QueryStringBuilder;
20
use Sonata\AdminBundle\Route\RouteCollection;
21
22
class QueryStringBuilderTest extends TestCase
23
{
24
    /**
25
     * @dataProvider getBuildTests
26
     */
27
    public function testBuild(array $expectedRoutes, $hasReader, $aclEnabled, $getParent): void
28
    {
29
        $audit = $this->getMockForAbstractClass(AuditManagerInterface::class);
30
        $audit->expects($this->once())->method('hasReader')->willReturn($hasReader);
31
32
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
33
        $admin->expects($this->once())->method('getParent')->willReturn($getParent);
34
        $admin->expects($this->any())->method('getChildren')->willReturn([]);
35
        $admin->expects($this->once())->method('isAclEnabled')->willReturn($aclEnabled);
36
37
        $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
38
39
        $pathBuilder = new QueryStringBuilder($audit);
40
41
        $pathBuilder->build($admin, $routeCollection);
42
43
        $this->assertCount(\count($expectedRoutes), $routeCollection->getElements());
44
45
        foreach ($expectedRoutes as $expectedRoute) {
46
            $this->assertTrue($routeCollection->has($expectedRoute), sprintf('Expected route: "%s" doesn`t exist.', $expectedRoute));
47
        }
48
    }
49
50
    public function getBuildTests()
51
    {
52
        return [
53
            [['list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'history_compare_revisions', 'acl'], true, true, null],
54
            [['list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'acl'], false, true, null],
55
            [['list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'history_compare_revisions'], true, false, null],
56
            [['list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'history_compare_revisions', 'acl'], true, true, $this->createMock(AdminInterface::class)],
57
        ];
58
    }
59
60
    public function testBuildWithChildren(): void
61
    {
62
        $audit = $this->getMockForAbstractClass(AuditManagerInterface::class);
63
        $audit->expects($this->once())->method('hasReader')->willReturn(true);
64
65
        $childRouteCollection1 = new RouteCollection('child1.Code.Route', 'child1RouteName', 'child1RoutePattern', 'child1ControllerName');
66
        $childRouteCollection1->add('foo');
67
        $childRouteCollection1->add('bar');
68
69
        $childRouteCollection2 = new RouteCollection('child2.Code.Route', 'child2RouteName', 'child2RoutePattern', 'child2ControllerName');
70
        $childRouteCollection2->add('baz');
71
72
        $child1 = $this->getMockForAbstractClass(AdminInterface::class);
73
        $child1->expects($this->once())->method('getRoutes')->willReturn($childRouteCollection1);
74
75
        $child2 = $this->getMockForAbstractClass(AdminInterface::class);
76
        $child2->expects($this->once())->method('getRoutes')->willReturn($childRouteCollection2);
77
78
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
79
        $admin->expects($this->once())->method('getParent')->willReturn(null);
80
        $admin->expects($this->once())->method('getChildren')->willReturn([$child1, $child2]);
81
        $admin->expects($this->once())->method('isAclEnabled')->willReturn(true);
82
83
        $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
84
85
        $pathBuilder = new QueryStringBuilder($audit);
0 ignored issues
show
$audit is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...\AuditManagerInterface>.

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...
86
87
        $pathBuilder->build($admin, $routeCollection);
0 ignored issues
show
$admin is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundle\Admin\AdminInterface>.

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...
88
89
        $expectedRoutes = ['list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'history_compare_revisions', 'acl', 'child1.Code.Route.foo', 'child1.Code.Route.bar', 'child2.Code.Route.baz'];
90
        $this->assertCount(\count($expectedRoutes), $routeCollection->getElements());
91
92
        foreach ($expectedRoutes as $expectedRoute) {
93
            $this->assertTrue($routeCollection->has($expectedRoute), sprintf('Expected route: "%s" doesn`t exist.', $expectedRoute));
94
        }
95
    }
96
}
97