Completed
Push — refonte ( 64e01a...7173e3 )
by Arnaud
03:31
created

AdminFactoryTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 174
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testInit() 0 99 1
B testInjectAdmin() 0 68 1
1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\Admin\Factory;
4
5
use LAG\AdminBundle\Action\Factory\ActionFactory;
6
use LAG\AdminBundle\Controller\ListAction;
7
use LAG\AdminBundle\Admin\Admin;
8
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
9
use LAG\AdminBundle\Admin\AdminInterface;
10
use LAG\AdminBundle\Admin\Event\AdminEvents;
11
use LAG\AdminBundle\Admin\Event\AdminInjectedEvent;
12
use LAG\AdminBundle\Admin\Factory\AdminFactory;
13
use LAG\AdminBundle\Admin\Factory\ConfigurationFactory;
14
use LAG\AdminBundle\Admin\Registry\Registry;
15
use LAG\AdminBundle\Admin\Request\RequestHandler;
16
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
17
use LAG\AdminBundle\Application\Configuration\ApplicationConfigurationStorage;
18
use LAG\AdminBundle\DataProvider\DataProviderInterface;
19
use LAG\AdminBundle\DataProvider\Factory\DataProviderFactory;
20
use LAG\AdminBundle\Message\MessageHandlerInterface;
21
use LAG\AdminBundle\Tests\AdminTestBase;
22
use LAG\AdminBundle\View\Factory\ViewFactory;
23
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24
use Symfony\Component\EventDispatcher\EventDispatcher;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
27
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
28
29
class AdminFactoryTest extends AdminTestBase
30
{
31
    /**
32
     * Init method should create Admin object according to given configuration.
33
     */
34
    public function testInit()
35
    {
36
        $adminConfiguration = [
37
            'my_admin' => [
38
                'entity' => 'TestClass',
39
                'actions' => [
40
                    'test' => [
41
                        'service' => 'test',
42
                    ],
43
                ]
44
            ],
45
        ];
46
        $entity = 'TestClass';
47
        $dataProviderString = 'data_provider';
48
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class);
49
        $eventDispatcher
50
            ->expects($this->exactly(3))
51
            ->method('dispatch')
52
        ;
53
    
54
        $adminConfigurationObject = $this->getMockWithoutConstructor(AdminConfiguration::class);
55
        $adminConfigurationObject
56
            ->expects($this->atLeastOnce())
57
            ->method('getParameter')
58
            ->willReturnMap([
59
                ['entity', $entity],
60
                ['data_provider', $dataProviderString],
61
            ])
62
        ;
63
    
64
        $applicationConfigurationObject = $this->getMockWithoutConstructor(ApplicationConfiguration::class);
65
        $applicationConfigurationObject
66
            ->expects($this->atLeastOnce())
67
            ->method('getParameter')
68
            ->willReturnMap([
69
                ['admin_class', Admin::class],
70
            ])
71
        ;
72
        
73
        $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class);
74
        $configurationFactory
75
            ->expects($this->once())
76
            ->method('create')
77
            ->with($adminConfiguration['my_admin'])
78
            ->willReturn($adminConfigurationObject)
79
        ;
80
        
81
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
82
        $messageHandler = $this->getMockWithoutConstructor(MessageHandlerInterface::class);
83
        $registry = $this->getMockWithoutConstructor(Registry::class);
84
        
85
    
86
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
87
        
88
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
89
        $dataProviderFactory
90
            ->expects($this->once())
91
            ->method('get')
92
            ->with('data_provider')
93
            ->willReturn($dataProvider)
94
        ;
95
    
96
        $requestHandler = $this->getMockWithoutConstructor(RequestHandler::class);
97
        $authorizationChecker = $this->getMockWithoutConstructor(AuthorizationCheckerInterface::class);
98
        $tokenStorage = $this->getMockWithoutConstructor(TokenStorageInterface::class);
99
    
100
        $actionRegistry = $this->getMockWithoutConstructor(\LAG\AdminBundle\Action\Registry\Registry::class);
101
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
102
        
103
        $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class);
104
        $applicationConfigurationStorage
105
            ->expects($this->once())
106
            ->method('getApplicationConfiguration')
107
            ->willReturn($applicationConfigurationObject)
108
        ;
109
        
110
        $factory = new AdminFactory(
111
            $adminConfiguration,
112
            $eventDispatcher,
113
            $messageHandler,
114
            $registry,
115
            $actionRegistry,
116
            $actionFactory,
117
            $configurationFactory,
118
            $dataProviderFactory,
119
            $viewFactory,
120
            $requestHandler,
121
            $authorizationChecker,
122
            $tokenStorage,
123
            $applicationConfigurationStorage
124
        );
125
    
126
        $factory->init();
127
        $this->assertTrue($factory->isInit());
128
        
129
        // second init should do nothing
130
        $factory->init();
131
        $this->assertTrue($factory->isInit());
132
    }
133
    
134
    public function testInjectAdmin()
135
    {
136
        $request = new Request();
137
        $adminConfiguration = [
138
            'my_admin' => [
139
                'entity' => 'TestClass'
140
            ],
141
        ];
142
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class);
143
        $eventDispatcher
144
            ->expects($this->once())
145
            ->method('dispatch')
146
            ->willReturnCallback(function($name, $event) {
147
                $this->assertEquals(AdminEvents::ADMIN_INJECTED, $name);
148
                $this->assertInstanceOf(AdminInjectedEvent::class, $event);
149
            })
150
        ;
151
        $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class);
152
    
153
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
154
    
155
        $messageHandler = $this->getMockWithoutConstructor(MessageHandlerInterface::class);
156
    
157
        $registry = $this->getMockWithoutConstructor(Registry::class);
158
    
159
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
160
    
161
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
162
        
163
        $requestHandler = $this->getMockWithoutConstructor(RequestHandler::class);
164
        $requestHandler
165
            ->expects($this->once())
166
            ->method('handle')
167
            ->with($request)
168
            ->willReturn($admin)
169
        ;
170
   
171
        $authorizationChecker = $this->getMockWithoutConstructor(AuthorizationCheckerInterface::class);
172
        $tokenStorage = $this->getMockWithoutConstructor(TokenStorageInterface::class);
173
        $actionRegistry = $this->getMockWithoutConstructor(\LAG\AdminBundle\Action\Registry\Registry::class);
174
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
175
        $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class);
176
        
177
        $factory = new AdminFactory(
178
            $adminConfiguration,
179
            $eventDispatcher,
180
            $messageHandler,
181
            $registry,
182
            $actionRegistry,
183
            $actionFactory,
184
            $configurationFactory,
185
            $dataProviderFactory,
186
            $viewFactory,
187
            $requestHandler,
188
            $authorizationChecker,
189
            $tokenStorage,
190
            $applicationConfigurationStorage
191
        );
192
    
193
        $controller = $this->getMockWithoutConstructor(ListAction::class);
194
    
195
        // injectAdmin should inject an Admin in Twig global parameters and dispatch an event
196
        $factory->injectAdmin($controller, $request);
197
    
198
        // with a non AdminAware controller, it should do nothing
199
        $controller = $this->getMockWithoutConstructor(Controller::class);
200
        $factory->injectAdmin($controller, $request);
201
    }
202
}
203