Completed
Push — dev ( a083dd...a1297d )
by Arnaud
02:53
created

AdminTest::getFakeAdminsConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\Admin;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
8
use LAG\AdminBundle\Tests\Base;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Symfony\Component\Security\Core\User\User;
12
13
class AdminTest extends Base
14
{
15
    /**
16
     * Test if configuration is properly set.
17
     */
18
    public function testAdmin()
19
    {
20
        $configurations = $this->getFakeAdminsConfiguration();
21
22
        foreach ($configurations as $adminName => $configuration) {
23
            $adminConfiguration = new AdminConfiguration($configuration);
24
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
25
            $this->doTestAdmin($admin, $configuration, $adminName);
26
        }
27
    }
28
29
    /**
30
     * handleRequest method SHOULD throw an exception if the action is not valid.
31
     */
32
    public function testHandleRequest()
33
    {
34
        $configurations = $this->getFakeAdminsConfiguration();
35
36
        foreach ($configurations as $adminName => $configuration) {
37
            $adminConfiguration = new AdminConfiguration($configuration);
38
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
39
            $this->doTestAdmin($admin, $configuration, $adminName);
40
41
42
            // with no action, handleRequest method SHOULD throw an exception
43
            $this->assertExceptionRaised('Exception', function () use ($admin) {
44
                $request = new Request();
45
                $admin->handleRequest($request);
46
            });
47
48
            // with a wrong action, handleRequest method SHOULD throw an exception
49
            $this->assertExceptionRaised('Exception', function () use ($admin) {
50
                $request = new Request([], [], [
51
                    '_route_params' => [
52
                        '_action' => 'bad_action'
53
                    ]
54
                ]);
55
                $admin->handleRequest($request);
56
            });
57
58
            // with an existing action, handleRequest method SHOULD NOT throwing an exception
59
            $request = new Request([], [], [
60
                '_route_params' => [
61
                    '_action' => 'custom_list'
62
                ]
63
            ]);
64
            $admin->handleRequest($request);
65
        }
66
    }
67
68
    public function testCheckPermissions()
69
    {
70
        $configurations = $this->getFakeAdminsConfiguration();
71
72
        foreach ($configurations as $adminName => $configuration) {
73
            $adminConfiguration = new AdminConfiguration($configuration);
74
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
75
            $this->doTestAdmin($admin, $configuration, $adminName);
76
77
            // with a current action unset, checkPermissions method SHOULD throw an exception
78
            $this->assertExceptionRaised('Exception', function () use ($admin) {
79
                $user = new User('JohnKrovitch', 'john1234');
80
                $admin->checkPermissions($user);
81
            });
82
83
            // with the wrong roles, checkPermissions method SHOULD throw an exception
84 View Code Duplication
            $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
                $request = new Request([], [], [
86
                    '_route_params' => [
87
                        '_action' => 'custom_list'
88
                    ]
89
                ]);
90
                $user = new User('JohnKrovitch', 'john1234');
91
                $admin->handleRequest($request);
92
                $admin->checkPermissions($user);
93
            });
94
95
            // with the wrong roles, checkPermissions method SHOULD throw an exception
96 View Code Duplication
            $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
                $request = new Request([], [], [
98
                    '_route_params' => [
99
                        '_action' => 'custom_list'
100
                    ]
101
                ]);
102
                $user = new User('JohnKrovitch', 'john1234', [
103
                    'ROLE_USER'
104
                ]);
105
                $admin->handleRequest($request);
106
                $admin->checkPermissions($user);
107
            });
108
109
            // with the right role, checkPermissions method SHOULD NOT throw an exception
110
            $request = new Request([], [], [
111
                '_route_params' => [
112
                    '_action' => 'custom_list'
113
                ]
114
            ]);
115
            $user = new User('JohnKrovitch', 'john1234', [
116
                'ROLE_ADMIN'
117
            ]);
118
            $admin->handleRequest($request);
119
            $admin->checkPermissions($user);
120
        }
121
    }
122
123
    protected function doTestAdmin(AdminInterface $admin, array $configuration, $adminName)
124
    {
125
        $this->assertEquals($admin->getName(), $adminName);
126
        $this->assertEquals($admin->getConfiguration()->getFormType(), $configuration['form']);
127
        $this->assertEquals($admin->getConfiguration()->getEntityName(), $configuration['entity']);
128
129
        if (array_key_exists('controller', $configuration)) {
130
            $this->assertEquals($admin->getConfiguration()->getControllerName(), $configuration['controller']);
131
        }
132 View Code Duplication
        if (array_key_exists('max_per_page', $configuration)) {
133
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), $configuration['max_per_page']);
134
        } else {
135
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), 25);
136
        }
137
        if (!array_key_exists('actions', $configuration)) {
138
            $configuration['actions'] = [
139
                'create' => [],
140
                'edit' => [],
141
                'delete' => [],
142
                'list' => []
143
            ];
144
        }
145
        foreach ($configuration['actions'] as $actionName => $actionConfiguration) {
146
            $action = $this->mockAction($actionName);
147
            $admin->addAction($action);
0 ignored issues
show
Bug introduced by
It seems like $action defined by $this->mockAction($actionName) on line 146 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LAG\AdminBundle\Admin\AdminInterface::addAction() does only seem to accept object<LAG\AdminBundle\Admin\ActionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
148
        }
149
        $expectedActionNames = array_keys($configuration['actions']);
150
        $this->assertEquals($expectedActionNames, array_keys($admin->getActions()));
151
    }
152
153
    protected function getFakeAdminsConfiguration()
154
    {
155
        return [
156
            'full_entity' => [
157
                'entity' => 'Test\TestBundle\Entity\TestEntity',
158
                'form' => 'test',
159
                'controller' => 'TestTestBundle:Test',
160
                'max_per_page' => 50,
161
                'actions' => [
162
                    'custom_list' => [],
163
                    'custom_edit' => [],
164
                ],
165
                'manager' => 'Test\TestBundle\Manager\TestManager',
166
                'routing_url_pattern' => 'lag.admin.{admin}',
167
                'routing_name_pattern' => 'lag.{admin}.{action}',
168
                'data_provider' => null,
169
                'metadata' => new ClassMetadata('LAG\AdminBundle\Tests\Entity\EntityTest'),
170
            ]
171
        ];
172
    }
173
}
174