Completed
Push — dev ( dcd39b...53a432 )
by Arnaud
09:34
created

AdminFactoryTest::testGetAdmins()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\Admin\Factory;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use LAG\AdminBundle\Tests\Base;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class AdminFactoryTest extends Base
11
{
12
    /**
13
     * Init method should create Admin object according to given configuration.
14
     */
15
    public function testInit()
16
    {
17
        // admin factory should work without configuration
18
        $this->mockAdminFactory();
19
        // test admin creation
20
        $configuration = $this->getAdminsConfiguration();
21
        $adminFactory = $this->mockAdminFactory($configuration);
22
        $adminFactory->init();
23
24
        foreach ($configuration as $name => $adminConfiguration) {
25
            $admin = $adminFactory->getAdmin($name);
26
            $this->doTestAdmin($admin, $adminConfiguration, $name);
27
        }
28
    }
29
30
    /**
31
     * Create method should return an Admin according to given configuration.
32
     *
33
     * @throws Exception
34
     */
35
    public function testCreate()
36
    {
37
        // test minimal configuration
38
        $adminConfiguration = $this->getAdminsConfiguration()['minimal_configuration'];
39
        // mock admin factory with empty configuration
40
        $adminFactory = $this->mockAdminFactory();
41
        $admin = $adminFactory->create('admin_test', $adminConfiguration);
42
        $this->doTestAdmin($admin, $adminConfiguration, 'admin_test');
43
44
        // test full configuration
45
        $adminConfiguration = $this->getAdminsConfiguration()['full_configuration'];
46
        $admin = $adminFactory->create('admin_test2', $adminConfiguration);
47
        $this->doTestAdmin($admin, $adminConfiguration, 'admin_test2');
48
    }
49
50
    /**
51
     * GetAdminFromRequest method should return a configured Admin from request parameters.
52
     *
53
     * @throws Exception
54
     */
55
    public function testGetAdminFromRequest()
56
    {
57
        $adminConfiguration = $this->getAdminsConfiguration();
58
        $adminFactory = $this->mockAdminFactory($adminConfiguration);
59
        $adminFactory->init();
60
61
        foreach ($adminConfiguration as $name => $configuration) {
62
            $request = new Request([], [], [
63
                '_route_params' => [
64
                    '_admin' => $name,
65
                    // see Base->mockActionFactory
66
                    '_action' => 'test'
67
                ]
68
            ]);
69
            $admin = $adminFactory->getAdminFromRequest($request);
70
            $this->doTestAdmin($admin, $configuration, $name);
71
        }
72
    }
73
74
    /**
75
     * GetAdmin method should return an configured Admin by its name.
76
     *
77
     * @throws Exception
78
     */
79
    public function testGetAdmin()
80
    {
81
        // test with no configuration
82
        $adminFactory = $this->mockAdminFactory();
83
        // unknow admin not exists, it should throw an exception
84
        $this->assertExceptionRaised('Exception', function () use ($adminFactory) {
85
            $adminFactory->getAdmin('unknown_admin');
86
        });
87
        // test with configurations samples
88
        $adminsConfiguration = $this->getAdminsConfiguration();
89
        $adminFactory = $this->mockAdminFactory($adminsConfiguration);
90
        $adminFactory->init();
91
92
        foreach ($adminsConfiguration as $name => $configuration) {
93
            $admin = $adminFactory->getAdmin($name);
94
            $this->doTestAdmin($admin, $configuration, $name);
95
        }
96
    }
97
98
    /**
99
     * GetAdmins method should return all configured Admin.
100
     *
101
     * @throws Exception
102
     */
103
    public function testGetAdmins()
104
    {
105
        // test with no configuration
106
        $adminFactory = $this->mockAdminFactory();
107
        // unknow admin not exists, it should throw an exception
108
        $this->assertExceptionRaised('Exception', function () use ($adminFactory) {
109
            $adminFactory->getAdmin('unknown_admin');
110
        });
111
        // test with configurations samples
112
        $adminsConfiguration = $this->getAdminsConfiguration();
113
        $adminFactory = $this->mockAdminFactory($adminsConfiguration);
114
        $adminFactory->init();
115
116
        $admins = $adminFactory->getAdmins();
117
118
        foreach ($admins as $admin) {
119
            $this->assertArrayHasKey($admin->getName(), $adminsConfiguration);
120
            $this->doTestAdmin($admin, $adminsConfiguration[$admin->getName()], $admin->getName());
121
        }
122
    }
123
124
    /**
125
     * AddDataProvider method must add a DataProviderInterface to the Admin.
126
     */
127
    public function testAddDataProvider()
128
    {
129
        // test with no configuration
130
        $adminFactory = $this->mockAdminFactory();
131
        // unknow admin not exists, it should throw an exception
132
        $this->assertExceptionRaised('Exception', function () use ($adminFactory) {
133
            $adminFactory->getAdmin('unknown_admin');
134
        });
135
        $dataProvider = $this->mockDataProvider();
136
        $adminFactory->addDataProvider('test', $dataProvider);
0 ignored issues
show
Bug introduced by
It seems like $dataProvider defined by $this->mockDataProvider() on line 135 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LAG\AdminBundle\Admin\Fa...tory::addDataProvider() does only seem to accept object<LAG\AdminBundle\D...\DataProviderInterface>, 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...
137
    }
138
139
    /**
140
     * @param AdminInterface $admin
141
     * @param array $configuration
142
     * @param $adminName
143
     */
144
    protected function doTestAdmin(AdminInterface $admin, array $configuration, $adminName)
145
    {
146
        $this->assertEquals($admin->getName(), $adminName);
147
        $this->assertEquals($admin->getConfiguration()->getFormType(), $configuration['form']);
148
        $this->assertEquals($admin->getConfiguration()->getEntityName(), $configuration['entity']);
149
150
        if (array_key_exists('controller', $configuration)) {
151
            $this->assertEquals($admin->getConfiguration()->getControllerName(), $configuration['controller']);
152
        }
153 View Code Duplication
        if (array_key_exists('max_per_page', $configuration)) {
154
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), $configuration['max_per_page']);
155
        } else {
156
            $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), 25);
157
        }
158
    }
159
}
160