Completed
Push — dev ( a1297d...02942d )
by Arnaud
03:36
created

AdminTest::doTestAdmin()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 21

Duplication

Lines 5
Ratio 17.24 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 5
loc 29
rs 8.439
cc 5
eloc 21
nc 16
nop 3
1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\Admin;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Exception;
7
use LAG\AdminBundle\Admin\Action;
8
use LAG\AdminBundle\Admin\Admin;
9
use LAG\AdminBundle\Admin\AdminInterface;
10
use LAG\AdminBundle\Admin\Configuration\ActionConfiguration;
11
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
12
use LAG\AdminBundle\Tests\Base;
13
use stdClass;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Security\Core\User\User;
17
18
class AdminTest extends Base
19
{
20
    /**
21
     * Test if configuration is properly set.
22
     */
23
    public function testAdmin()
24
    {
25
        $configurations = $this->getFakeAdminsConfiguration();
26
27
        foreach ($configurations as $adminName => $configuration) {
28
            $adminConfiguration = new AdminConfiguration($configuration);
29
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
30
            $this->doTestAdmin($admin, $configuration, $adminName);
31
        }
32
    }
33
34
    /**
35
     * handleRequest method SHOULD throw an exception if the action is not valid.
36
     * handleRequest method SHOULD throw an exception if the action is not valid.
37
     */
38
    public function testHandleRequest()
39
    {
40
        $configurations = $this->getFakeAdminsConfiguration();
41
42
        foreach ($configurations as $adminName => $configuration) {
43
            $adminConfiguration = new AdminConfiguration($configuration);
44
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
45
            $this->doTestAdmin($admin, $configuration, $adminName);
46
47
48
            // with no action, handleRequest method SHOULD throw an exception
49
            $this->assertExceptionRaised('Exception', function () use ($admin) {
50
                $request = new Request();
51
                $admin->handleRequest($request);
52
            });
53
54
            // with a wrong action, handleRequest method SHOULD throw an exception
55
            $this->assertExceptionRaised('Exception', function () use ($admin) {
56
                $request = new Request([], [], [
57
                    '_route_params' => [
58
                        '_action' => 'bad_action'
59
                    ]
60
                ]);
61
                $admin->handleRequest($request);
62
            });
63
64
            // with an existing action, handleRequest method SHOULD NOT throwing an exception
65
            $request = new Request([], [], [
66
                '_route_params' => [
67
                    '_action' => 'custom_list'
68
                ]
69
            ]);
70
            $admin->handleRequest($request);
71
        }
72
73
        // test pagerfanta filter
74
        $configurations = $this->getFakeAdminsConfiguration();
75
        $configuration = $configurations['full_entity'];
76
77
        $adminConfiguration = new AdminConfiguration($configuration);
78
        $admin = $this->mockAdmin('full_entity', $adminConfiguration);
79
80
        $admin->addAction(new Action('custom_list', [
81
            'title' => 'Test action',
82
            'permissions' => [
83
                'ROLE_ADMIN'
84
            ],
85
            'submit_actions' => [],
86
            'batch' => [],
87
        ], new ActionConfiguration([
88
                'load_strategy' => '',
89
                'route' => '',
90
                'parameters' => '',
91
                'export' => '',
92
                'order' => '',
93
                'target' => '',
94
                'icon' => '',
95
                'batch' => '',
96
                'pager' => 'pagerfanta',
97
                'criteria' => [],
98
            ])
99
        ));
100
        $request = new Request([], [], [
101
            '_route_params' => [
102
                '_action' => 'custom_list'
103
            ]
104
        ]);
105
        $admin->handleRequest($request);
106
107
        // test load stregy none
108
        $configurations = $this->getFakeAdminsConfiguration();
109
        $configuration = $configurations['full_entity'];
110
111
        $adminConfiguration = new AdminConfiguration($configuration);
112
        $admin = $this->mockAdmin('full_entity', $adminConfiguration);
113
114
        $admin->addAction(new Action('custom_list', [
115
            'title' => 'Test action',
116
            'permissions' => [
117
                'ROLE_ADMIN'
118
            ],
119
            'submit_actions' => [],
120
            'batch' => [],
121
        ], new ActionConfiguration([
122
                'load_strategy' => Admin::LOAD_STRATEGY_NONE,
123
                'route' => '',
124
                'parameters' => '',
125
                'export' => '',
126
                'order' => '',
127
                'target' => '',
128
                'icon' => '',
129
                'batch' => '',
130
                'pager' => 'pagerfanta',
131
                'criteria' => [],
132
            ])
133
        ));
134
        $request = new Request([], [], [
135
            '_route_params' => [
136
                '_action' => 'custom_list'
137
            ]
138
        ]);
139
        $admin->handleRequest($request);
140
    }
141
142
    /**
143
     * checkPermissions method SHOULd throw an exception if the permissions are invalid.
144
     */
145
    public function testCheckPermissions()
146
    {
147
        $configurations = $this->getFakeAdminsConfiguration();
148
149
        foreach ($configurations as $adminName => $configuration) {
150
            $adminConfiguration = new AdminConfiguration($configuration);
151
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
152
            $this->doTestAdmin($admin, $configuration, $adminName);
153
154
            // with a current action unset, checkPermissions method SHOULD throw an exception
155
            $this->assertExceptionRaised('Exception', function () use ($admin) {
156
                $user = new User('JohnKrovitch', 'john1234');
157
                $admin->checkPermissions($user);
158
            });
159
160
            // with the wrong roles, checkPermissions method SHOULD throw an exception
161
            $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) {
162
                $request = new Request([], [], [
163
                    '_route_params' => [
164
                        '_action' => 'custom_list'
165
                    ]
166
                ]);
167
                $user = new User('JohnKrovitch', 'john1234');
168
                $admin->handleRequest($request);
169
                $admin->checkPermissions($user);
170
            });
171
172
            // with the wrong roles, checkPermissions method SHOULD throw an exception
173
            $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) {
174
                $request = new Request([], [], [
175
                    '_route_params' => [
176
                        '_action' => 'custom_list'
177
                    ]
178
                ]);
179
                $user = new User('JohnKrovitch', 'john1234', [
180
                    'ROLE_USER'
181
                ]);
182
                $admin->handleRequest($request);
183
                $admin->checkPermissions($user);
184
            });
185
186
            // with the right role, checkPermissions method SHOULD NOT throw an exception
187
            $request = new Request([], [], [
188
                '_route_params' => [
189
                    '_action' => 'custom_list'
190
                ]
191
            ]);
192
            $user = new User('JohnKrovitch', 'john1234', [
193
                'ROLE_ADMIN'
194
            ]);
195
            $admin->handleRequest($request);
196
            $admin->checkPermissions($user);
197
        }
198
    }
199
200
    /**
201
     * create method SHOULD call the create method in the data provider.
202
     */
203
    public function testCreate()
204
    {
205
        $dataProvider = $this->mockDataProvider();
206
        $dataProvider
207
            ->expects($this->once())
208
            ->method('create')
209
        ;
210
211
        $admin = new Admin(
212
            'test',
213
            $dataProvider,
214
            new AdminConfiguration($this->getFakeAdminsConfiguration()['full_entity']),
215
            $this->mockMessageHandler()
216
        );
217
        $admin->create();
218
    }
219
220
    /**
221
     * save method SHOULD call the save method in the data provider.
222
     */
223
    public function testSave()
224
    {
225
        $dataProvider = $this->mockDataProvider();
226
        $dataProvider
227
            ->expects($this->once())
228
            ->method('save')
229
        ;
230
        $dataProvider
231
            ->method('create')
232
            ->willReturn(new stdClass())
233
        ;
234
235
        $admin = new Admin(
236
            'test',
237
            $dataProvider,
238
            new AdminConfiguration($this->getFakeAdminsConfiguration()['full_entity']),
239
            $this->mockMessageHandler()
240
        );
241
        $admin->create();
242
        $this->assertTrue($admin->save());
243
244
        // test exception
245
        $dataProvider = $this->mockDataProvider();
246
        $dataProvider
247
            ->expects($this->once())
248
            ->method('save')
249
            ->willThrowException(new Exception())
250
        ;
251
        $dataProvider
252
            ->method('create')
253
            ->willReturn(new stdClass())
254
        ;
255
256
        $admin = new Admin(
257
            'test',
258
            $dataProvider,
259
            new AdminConfiguration($this->getFakeAdminsConfiguration()['full_entity']),
260
            $this->mockMessageHandler()
261
        );
262
        $admin->create();
263
        $this->assertFalse($admin->save());
264
    }
265
266
    /**
267
     * remove method SHOULD call the remove method in the data provider.
268
     */
269
    public function testRemove()
270
    {
271
        $dataProvider = $this->mockDataProvider();
272
        $dataProvider
273
            ->expects($this->once())
274
            ->method('remove')
275
        ;
276
        $dataProvider
277
            ->method('create')
278
            ->willReturn(new stdClass())
279
        ;
280
281
        $admin = new Admin(
282
            'test',
283
            $dataProvider,
284
            new AdminConfiguration($this->getFakeAdminsConfiguration()['full_entity']),
285
            $this->mockMessageHandler()
286
        );
287
        $admin->create();
288
        $this->assertTrue($admin->remove());
289
290
        // test exception
291
        $dataProvider = $this->mockDataProvider();
292
        $dataProvider
293
            ->expects($this->once())
294
            ->method('remove')
295
            ->willThrowException(new Exception())
296
        ;
297
        $dataProvider
298
            ->method('create')
299
            ->willReturn(new stdClass())
300
        ;
301
302
        $admin = new Admin(
303
            'test',
304
            $dataProvider,
305
            new AdminConfiguration($this->getFakeAdminsConfiguration()['full_entity']),
306
            $this->mockMessageHandler()
307
        );
308
        $admin->create();
309
        $this->assertFalse($admin->remove());
310
    }
311
312
    protected function doTestAdmin(AdminInterface $admin, array $configuration, $adminName)
313
    {
314
        $this->assertEquals($admin->getName(), $adminName);
315
        $this->assertEquals($admin->getConfiguration()->getFormType(), $configuration['form']);
316
        $this->assertEquals($admin->getConfiguration()->getEntityName(), $configuration['entity']);
317
318
        if (array_key_exists('controller', $configuration)) {
319
            $this->assertEquals($admin->getConfiguration()->getControllerName(), $configuration['controller']);
320
        }
321
        if (array_key_exists('max_per_page', $configuration)) {
322
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), $configuration['max_per_page']);
323
        } else {
324
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), 25);
325
        }
326
        if (!array_key_exists('actions', $configuration)) {
327
            $configuration['actions'] = [
328
                'create' => [],
329
                'edit' => [],
330
                'delete' => [],
331
                'list' => []
332
            ];
333
        }
334
        foreach ($configuration['actions'] as $actionName => $actionConfiguration) {
335
            $action = $this->mockAction($actionName);
336
            $admin->addAction($action);
337
        }
338
        $expectedActionNames = array_keys($configuration['actions']);
339
        $this->assertEquals($expectedActionNames, array_keys($admin->getActions()));
340
    }
341
342
    protected function getFakeAdminsConfiguration()
343
    {
344
        return [
345
            'full_entity' => [
346
                'entity' => 'Test\TestBundle\Entity\TestEntity',
347
                'form' => 'test',
348
                'controller' => 'TestTestBundle:Test',
349
                'max_per_page' => 50,
350
                'actions' => [
351
                    'custom_list' => [],
352
                    'custom_edit' => [],
353
                ],
354
                'manager' => 'Test\TestBundle\Manager\TestManager',
355
                'routing_url_pattern' => 'lag.admin.{admin}',
356
                'routing_name_pattern' => 'lag.{admin}.{action}',
357
                'data_provider' => null,
358
                'metadata' => new ClassMetadata('LAG\AdminBundle\Tests\Entity\EntityTest'),
359
            ]
360
        ];
361
    }
362
}
363