Completed
Push — dev ( 5c06f5...dcd39b )
by Arnaud
09:19
created

AdminTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 7.81 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 4
dl 5
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testAdmin() 0 10 2
B doTestAdmin() 5 29 5
A getFakeAdminsConfiguration() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
10
class AdminTest extends Base
11
{
12
    public function testAdmin()
13
    {
14
        $configurations = $this->getFakeAdminsConfiguration();
15
16
        foreach ($configurations as $adminName => $configuration) {
17
            $adminConfiguration = new AdminConfiguration($configuration);
18
            $admin = $this->mockAdmin($adminName, $adminConfiguration);
19
            $this->doTestAdmin($admin, $configuration, $adminName);
20
        }
21
    }
22
23
    protected function doTestAdmin(AdminInterface $admin, array $configuration, $adminName)
24
    {
25
        $this->assertEquals($admin->getName(), $adminName);
26
        $this->assertEquals($admin->getConfiguration()->getFormType(), $configuration['form']);
27
        $this->assertEquals($admin->getConfiguration()->getEntityName(), $configuration['entity']);
28
29
        if (array_key_exists('controller', $configuration)) {
30
            $this->assertEquals($admin->getConfiguration()->getControllerName(), $configuration['controller']);
31
        }
32 View Code Duplication
        if (array_key_exists('max_per_page', $configuration)) {
33
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), $configuration['max_per_page']);
34
        } else {
35
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), 25);
36
        }
37
        if (!array_key_exists('actions', $configuration)) {
38
            $configuration['actions'] = [
39
                'create' => [],
40
                'edit' => [],
41
                'delete' => [],
42
                'list' => []
43
            ];
44
        }
45
        foreach ($configuration['actions'] as $actionName => $actionConfiguration) {
46
            $action = $this->mockAction($actionName);
47
            $admin->addAction($action);
0 ignored issues
show
Bug introduced by
It seems like $action defined by $this->mockAction($actionName) on line 46 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...
48
        }
49
        $expectedActionNames = array_keys($configuration['actions']);
50
        $this->assertEquals($expectedActionNames, array_keys($admin->getActions()));
51
    }
52
53
    protected function getFakeAdminsConfiguration()
54
    {
55
        return [
56
            'full_entity' => [
57
                'entity' => 'Test\TestBundle\Entity\TestEntity',
58
                'form' => 'test',
59
                'controller' => 'TestTestBundle:Test',
60
                'max_per_page' => 50,
61
                'actions' => [
62
                    'custom_list' => [],
63
                    'custom_edit' => [],
64
                ],
65
                'manager' => 'Test\TestBundle\Manager\TestManager',
66
                'routing_url_pattern' => 'lag.admin.{admin}',
67
                'routing_name_pattern' => 'lag.{admin}.{action}',
68
                'data_provider' => null,
69
                'metadata' => new ClassMetadata('LAG\AdminBundle\Tests\Entity\EntityTest'),
70
            ]
71
        ];
72
    }
73
}
74