|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Tests\AdminBundle\Action\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use LAG\AdminBundle\Action\Configuration\ActionConfiguration; |
|
7
|
|
|
use LAG\AdminBundle\Action\Event\ActionCreatedEvent; |
|
8
|
|
|
use LAG\AdminBundle\Action\Event\ActionEvents; |
|
9
|
|
|
use LAG\AdminBundle\Action\Factory\ActionFactory; |
|
10
|
|
|
use LAG\AdminBundle\Action\ActionInterface; |
|
11
|
|
|
use LAG\AdminBundle\Action\Factory\ConfigurationFactory; |
|
12
|
|
|
use LAG\AdminBundle\Action\Registry\Registry; |
|
13
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
14
|
|
|
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration; |
|
15
|
|
|
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent; |
|
16
|
|
|
use LAG\AdminBundle\Tests\AdminTestBase; |
|
17
|
|
|
use LogicException; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
|
|
22
|
|
|
class ActionFactoryTest extends AdminTestBase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testInjectConfigurationWithoutActions() |
|
25
|
|
|
{ |
|
26
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
27
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
28
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
29
|
|
|
|
|
30
|
|
|
$actionFactory = new ActionFactory( |
|
31
|
|
|
$configurationFactory, |
|
32
|
|
|
$eventDispatcher, |
|
33
|
|
|
$actionRegistry |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
|
37
|
|
|
$adminConfiguration |
|
38
|
|
|
->expects($this->once()) |
|
39
|
|
|
->method('getParameter') |
|
40
|
|
|
->with('actions') |
|
41
|
|
|
->willReturn([ |
|
42
|
|
|
'list' => [ |
|
43
|
|
|
|
|
44
|
|
|
] |
|
45
|
|
|
]) |
|
46
|
|
|
; |
|
47
|
|
|
|
|
48
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
|
49
|
|
|
$admin |
|
50
|
|
|
->expects($this->once()) |
|
51
|
|
|
->method('getConfiguration') |
|
52
|
|
|
->willReturn($adminConfiguration) |
|
53
|
|
|
; |
|
54
|
|
|
|
|
55
|
|
|
$controller = $this->getMockWithoutConstructor(ActionInterface::class); |
|
56
|
|
|
$controller |
|
57
|
|
|
->expects($this->exactly(2)) |
|
58
|
|
|
->method('getAdmin') |
|
59
|
|
|
->willReturn($admin) |
|
60
|
|
|
; |
|
61
|
|
|
$request = new Request(); |
|
62
|
|
|
|
|
63
|
|
|
$actionFactory->injectConfiguration($controller, $request); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* If the Controller is not an ActionInterface, nothing should be done. |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function testInjectConfigurationWithoutActionInterface() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
72
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
73
|
|
|
|
|
74
|
|
|
// no event should be dispatched |
|
75
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
76
|
|
|
$eventDispatcher |
|
77
|
|
|
->expects($this->never()) |
|
78
|
|
|
->method('dispatch') |
|
79
|
|
|
; |
|
80
|
|
|
|
|
81
|
|
|
$actionFactory = new ActionFactory( |
|
82
|
|
|
$configurationFactory, |
|
83
|
|
|
$eventDispatcher, |
|
84
|
|
|
$actionRegistry |
|
85
|
|
|
); |
|
86
|
|
|
$controller = $this->getMockWithoutConstructor(Controller::class); |
|
87
|
|
|
$request = new Request(); |
|
88
|
|
|
|
|
89
|
|
|
$actionFactory->injectConfiguration($controller, $request); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* If the Controller has no Admin, nothing should be done. |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function testInjectConfigurationWithoutAdmin() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
98
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
99
|
|
|
|
|
100
|
|
|
// no event should be dispatched |
|
101
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
102
|
|
|
$eventDispatcher |
|
103
|
|
|
->expects($this->never()) |
|
104
|
|
|
->method('dispatch') |
|
105
|
|
|
; |
|
106
|
|
|
|
|
107
|
|
|
$actionFactory = new ActionFactory( |
|
108
|
|
|
$configurationFactory, |
|
109
|
|
|
$eventDispatcher, |
|
110
|
|
|
$actionRegistry |
|
111
|
|
|
); |
|
112
|
|
|
$controller = $this->getMockWithoutConstructor(ActionInterface::class); |
|
113
|
|
|
$request = new Request(); |
|
114
|
|
|
|
|
115
|
|
|
$actionFactory->injectConfiguration($controller, $request); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testInjectConfiguration() |
|
119
|
|
|
{ |
|
120
|
|
|
$actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
|
121
|
|
|
|
|
122
|
|
|
$adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
|
123
|
|
|
$adminConfiguration |
|
124
|
|
|
->expects($this->once()) |
|
125
|
|
|
->method('getParameter') |
|
126
|
|
|
->with('actions') |
|
127
|
|
|
->willReturn([ |
|
128
|
|
|
'list' => [ |
|
129
|
|
|
] |
|
130
|
|
|
]) |
|
131
|
|
|
; |
|
132
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
|
133
|
|
|
$admin |
|
134
|
|
|
->expects($this->atLeastOnce()) |
|
135
|
|
|
->method('getConfiguration') |
|
136
|
|
|
->willReturn($adminConfiguration) |
|
137
|
|
|
; |
|
138
|
|
|
$admin |
|
139
|
|
|
->expects($this->atLeastOnce()) |
|
140
|
|
|
->method('getName') |
|
141
|
|
|
->willReturn('my_little_admin') |
|
142
|
|
|
; |
|
143
|
|
|
$actionName = 'list'; |
|
144
|
|
|
|
|
145
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
146
|
|
|
$configurationFactory |
|
147
|
|
|
->expects($this->once()) |
|
148
|
|
|
->method('create') |
|
149
|
|
|
->with($actionName, 'my_little_admin', $adminConfiguration, []) |
|
150
|
|
|
->willReturn($actionConfiguration) |
|
151
|
|
|
; |
|
152
|
|
|
|
|
153
|
|
|
$controller = $this->getMockWithoutConstructor(ActionInterface::class); |
|
154
|
|
|
$controller |
|
155
|
|
|
->expects($this->atLeastOnce()) |
|
156
|
|
|
->method('getAdmin') |
|
157
|
|
|
->willReturn($admin) |
|
158
|
|
|
; |
|
159
|
|
|
|
|
160
|
|
|
$event = new BeforeConfigurationEvent('list', [], $admin); |
|
161
|
|
|
$event2 = new ActionCreatedEvent($controller, $controller->getAdmin()); |
|
162
|
|
|
|
|
163
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
164
|
|
|
$eventDispatcher |
|
165
|
|
|
->expects($this->exactly(2)) |
|
166
|
|
|
->method('dispatch') |
|
167
|
|
|
->willReturnMap([ |
|
168
|
|
|
[ActionEvents::BEFORE_CONFIGURATION, $event, null], |
|
169
|
|
|
[ActionEvents::ACTION_CREATED, $event2, null], |
|
170
|
|
|
]) |
|
171
|
|
|
; |
|
172
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
173
|
|
|
|
|
174
|
|
|
$request = $this->getMockWithoutConstructor(Request::class); |
|
175
|
|
|
$request |
|
176
|
|
|
->expects($this->once()) |
|
177
|
|
|
->method('get') |
|
178
|
|
|
->with('_route_params') |
|
179
|
|
|
->willReturn([ |
|
180
|
|
|
'_action' => $actionName, |
|
181
|
|
|
]) |
|
182
|
|
|
; |
|
183
|
|
|
|
|
184
|
|
|
$actionFactory = new ActionFactory( |
|
185
|
|
|
$configurationFactory, |
|
186
|
|
|
$eventDispatcher, |
|
187
|
|
|
$actionRegistry |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
$actionFactory->injectConfiguration($controller, $request); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function testGetActions() |
|
194
|
|
|
{ |
|
195
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
196
|
|
|
|
|
197
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
198
|
|
|
$actionRegistry |
|
199
|
|
|
->expects($this->exactly(2)) |
|
200
|
|
|
->method('get') |
|
201
|
|
|
->willReturnMap([ |
|
202
|
|
|
['my_own_service', 'test'], |
|
203
|
|
|
['lag.admin.actions.edit', 'test2'], |
|
204
|
|
|
]) |
|
205
|
|
|
; |
|
206
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
207
|
|
|
|
|
208
|
|
|
$actionFactory = new ActionFactory( |
|
209
|
|
|
$configurationFactory, |
|
210
|
|
|
$eventDispatcher, |
|
211
|
|
|
$actionRegistry |
|
212
|
|
|
); |
|
213
|
|
|
$actions = $actionFactory->getActions('test', [ |
|
214
|
|
|
'actions' => [ |
|
215
|
|
|
'list' => [ |
|
216
|
|
|
'service' => 'my_own_service', |
|
217
|
|
|
], |
|
218
|
|
|
'edit' => [ |
|
219
|
|
|
|
|
220
|
|
|
], |
|
221
|
|
|
] |
|
222
|
|
|
]); |
|
223
|
|
|
|
|
224
|
|
|
$this->assertEquals([ |
|
225
|
|
|
'list' => 'test', |
|
226
|
|
|
'edit' => 'test2', |
|
227
|
|
|
], $actions); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function testGetActionsWithInvalidConfiguration() |
|
231
|
|
|
{ |
|
232
|
|
|
$configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
|
233
|
|
|
$actionRegistry = $this->getMockWithoutConstructor(Registry::class); |
|
234
|
|
|
$eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
|
235
|
|
|
|
|
236
|
|
|
$actionFactory = new ActionFactory( |
|
237
|
|
|
$configurationFactory, |
|
238
|
|
|
$eventDispatcher, |
|
239
|
|
|
$actionRegistry |
|
240
|
|
|
); |
|
241
|
|
|
|
|
242
|
|
|
$this->assertExceptionRaised(Exception::class, function () use ($actionFactory) { |
|
243
|
|
|
$actionFactory->getActions('test', [ |
|
244
|
|
|
'lol', |
|
245
|
|
|
]); |
|
246
|
|
|
}); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertExceptionRaised(LogicException::class, function () use ($actionFactory) { |
|
249
|
|
|
$actionFactory->getActions('test', [ |
|
250
|
|
|
'actions' => [ |
|
251
|
|
|
'my-action' => [], |
|
252
|
|
|
], |
|
253
|
|
|
]); |
|
254
|
|
|
}); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
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.