|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use LAG\AdminBundle\Admin\ActionInterface; |
|
11
|
|
|
use LAG\AdminBundle\Admin\Admin; |
|
12
|
|
|
use LAG\AdminBundle\Admin\Configuration\ActionConfiguration; |
|
13
|
|
|
use LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration; |
|
14
|
|
|
use LAG\AdminBundle\Admin\Factory\ActionFactory; |
|
15
|
|
|
use LAG\AdminBundle\Admin\Factory\AdminFactory; |
|
16
|
|
|
use LAG\AdminBundle\DataProvider\DataProviderInterface; |
|
17
|
|
|
use LAG\AdminBundle\Message\MessageHandlerInterface; |
|
18
|
|
|
use LAG\DoctrineRepositoryBundle\Repository\RepositoryInterface; |
|
19
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
20
|
|
|
use Symfony\Bridge\Monolog\Logger; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
26
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
27
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
28
|
|
|
|
|
29
|
|
|
class Base extends WebTestCase |
|
30
|
|
|
{ |
|
31
|
|
|
protected static $isDatabaseCreated = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ContainerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Client |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $client; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Initialize an application with a container and a client. Create database if required. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $url |
|
47
|
|
|
* @param null $method |
|
48
|
|
|
* @param array $parameters |
|
49
|
|
|
*/ |
|
50
|
|
|
public function initApplication($url = '/', $method = null, $parameters = []) |
|
51
|
|
|
{ |
|
52
|
|
|
// creating kernel client |
|
53
|
|
|
$this->client = Client::initClient(); |
|
54
|
|
|
// test client initialization |
|
55
|
|
|
$this->assertTrue($this->client != null, 'TestClient successfully initialized'); |
|
56
|
|
|
|
|
57
|
|
|
// initialise database |
|
58
|
|
|
if (!self::$isDatabaseCreated) { |
|
59
|
|
|
// TODO remove database at the end of the tests |
|
60
|
|
|
exec(__DIR__ . '/app/console doctrine:database:create --if-not-exists', $output); |
|
61
|
|
|
exec(__DIR__ . '/app/console doctrine:schema:update --force', $output); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($output as $line) { |
|
|
|
|
|
|
64
|
|
|
// TODO only in verbose mode |
|
65
|
|
|
fwrite(STDOUT, $line . "\n"); |
|
66
|
|
|
} |
|
67
|
|
|
fwrite(STDOUT, "\n"); |
|
68
|
|
|
self::$isDatabaseCreated = true; |
|
69
|
|
|
} |
|
70
|
|
|
// init a request |
|
71
|
|
|
$request = Request::create($url, $method, $parameters); |
|
72
|
|
|
// do request |
|
73
|
|
|
$this->client->doRequest($request); |
|
74
|
|
|
$this->container = $this->client->getContainer(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Assert that an exception is raised in the given code. |
|
79
|
|
|
* |
|
80
|
|
|
* @param $exceptionClass |
|
81
|
|
|
* @param Closure $closure |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function assertExceptionRaised($exceptionClass, Closure $closure) |
|
84
|
|
|
{ |
|
85
|
|
|
$e = null; |
|
86
|
|
|
$isClassValid = false; |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
$closure(); |
|
90
|
|
|
} catch (Exception $e) { |
|
91
|
|
|
if (get_class($e) == $exceptionClass) { |
|
92
|
|
|
$isClassValid = true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
$this->assertTrue($isClassValid, 'Expected ' . $exceptionClass . ', got ' . get_class($e)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return Admin configurations samples |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getAdminsConfiguration() |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
'minimal_configuration' => [ |
|
107
|
|
|
'entity' => 'Test\TestBundle\Entity\TestEntity', |
|
108
|
|
|
'form' => 'test' |
|
109
|
|
|
], |
|
110
|
|
|
'full_configuration' => [ |
|
111
|
|
|
'entity' => 'Test\TestBundle\Entity\TestEntity', |
|
112
|
|
|
'form' => 'test', |
|
113
|
|
|
'controller' => 'TestTestBundle:Test', |
|
114
|
|
|
'max_per_page' => 50, |
|
115
|
|
|
'actions' => [ |
|
116
|
|
|
'custom_list' => [], |
|
117
|
|
|
'custom_edit' => [], |
|
118
|
|
|
], |
|
119
|
|
|
'manager' => 'Test\TestBundle\Manager\TestManager', |
|
120
|
|
|
'routing_url_pattern' => 'lag.admin.{admin}', |
|
121
|
|
|
'routing_name_pattern' => 'lag.{admin}.{action}' |
|
122
|
|
|
] |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $name |
|
128
|
|
|
* @param $configuration |
|
129
|
|
|
* @return Admin |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function mockAdmin($name, $configuration) |
|
132
|
|
|
{ |
|
133
|
|
|
return new Admin( |
|
134
|
|
|
$name, |
|
135
|
|
|
$this->mockDataProvider(), |
|
|
|
|
|
|
136
|
|
|
$configuration, |
|
137
|
|
|
$this->mockMessageHandler() |
|
|
|
|
|
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $name |
|
143
|
|
|
* @return ActionInterface|PHPUnit_Framework_MockObject_MockObject |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function mockAction($name) |
|
146
|
|
|
{ |
|
147
|
|
|
$action = $this |
|
148
|
|
|
->getMockBuilder('LAG\AdminBundle\Admin\ActionInterface') |
|
149
|
|
|
->disableOriginalConstructor() |
|
150
|
|
|
->getMock(); |
|
151
|
|
|
$action |
|
152
|
|
|
->method('getName') |
|
153
|
|
|
->willReturn($name); |
|
154
|
|
|
$action |
|
155
|
|
|
->method('getConfiguration') |
|
156
|
|
|
->willReturn($this->mockActionConfiguration()); |
|
157
|
|
|
|
|
158
|
|
|
return $action; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return ActionConfiguration|PHPUnit_Framework_MockObject_MockObject |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function mockActionConfiguration() |
|
165
|
|
|
{ |
|
166
|
|
|
$configuration = $this |
|
167
|
|
|
->getMockBuilder('LAG\AdminBundle\Admin\Configuration\ActionConfiguration') |
|
168
|
|
|
->disableOriginalConstructor() |
|
169
|
|
|
->getMock(); |
|
170
|
|
|
$configuration |
|
171
|
|
|
->method('getLoadMethod') |
|
172
|
|
|
->willReturn(Admin::LOAD_STRATEGY_MULTIPLE); |
|
173
|
|
|
$configuration |
|
174
|
|
|
->method('getCriteria') |
|
175
|
|
|
->willReturn([]); |
|
176
|
|
|
|
|
177
|
|
|
return $configuration; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param array $configuration |
|
182
|
|
|
* @return AdminFactory |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function mockAdminFactory(array $configuration = []) |
|
185
|
|
|
{ |
|
186
|
|
|
/** @var EventDispatcher $mockEventDispatcher */ |
|
187
|
|
|
$mockEventDispatcher = $this |
|
188
|
|
|
->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
|
189
|
|
|
->getMock(); |
|
190
|
|
|
|
|
191
|
|
|
return new AdminFactory( |
|
192
|
|
|
$mockEventDispatcher, |
|
193
|
|
|
$this->mockEntityManager(), |
|
|
|
|
|
|
194
|
|
|
$this->mockApplicationConfiguration(), |
|
195
|
|
|
$configuration, |
|
196
|
|
|
$this->mockActionFactory(), |
|
|
|
|
|
|
197
|
|
|
$this->mockMessageHandler() |
|
|
|
|
|
|
198
|
|
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return Session|PHPUnit_Framework_MockObject_MockObject |
|
203
|
|
|
*/ |
|
204
|
|
View Code Duplication |
protected function mockSession() |
|
205
|
|
|
{ |
|
206
|
|
|
if ($this->container) { |
|
207
|
|
|
$session = $this |
|
208
|
|
|
->container |
|
209
|
|
|
->get('session'); |
|
210
|
|
|
} else { |
|
211
|
|
|
$session = $this |
|
212
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session') |
|
213
|
|
|
->disableOriginalConstructor() |
|
214
|
|
|
->getMock(); |
|
215
|
|
|
} |
|
216
|
|
|
return $session; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return Logger|PHPUnit_Framework_MockObject_MockObject |
|
221
|
|
|
*/ |
|
222
|
|
View Code Duplication |
protected function mockLogger() |
|
223
|
|
|
{ |
|
224
|
|
|
if ($this->container) { |
|
225
|
|
|
$logger = $this |
|
226
|
|
|
->container |
|
227
|
|
|
->get('logger'); |
|
228
|
|
|
} else { |
|
229
|
|
|
$logger = $this |
|
230
|
|
|
->getMockBuilder('Monolog\Logger') |
|
231
|
|
|
->disableOriginalConstructor() |
|
232
|
|
|
->getMock(); |
|
233
|
|
|
} |
|
234
|
|
|
return $logger; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Return a mock of an entity repository |
|
239
|
|
|
* |
|
240
|
|
|
* @return RepositoryInterface|PHPUnit_Framework_MockObject_MockObject |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function mockEntityRepository() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this |
|
245
|
|
|
->getMockBuilder('LAG\DoctrineRepositoryBundle\Repository\RepositoryInterface') |
|
246
|
|
|
->getMock(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return EntityManager|PHPUnit_Framework_MockObject_MockObject |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function mockEntityManager() |
|
253
|
|
|
{ |
|
254
|
|
|
/** @var EntityManagerInterface|PHPUnit_Framework_MockObject_MockObject $entityManager */ |
|
255
|
|
|
$entityManager = $this |
|
256
|
|
|
->getMockBuilder('Doctrine\ORM\EntityManager') |
|
257
|
|
|
->disableOriginalConstructor() |
|
258
|
|
|
->getMock(); |
|
259
|
|
|
$repository = $this->mockEntityRepository(); |
|
260
|
|
|
$repository |
|
261
|
|
|
->method('getEntityPersister') |
|
262
|
|
|
->willReturn( $this |
|
263
|
|
|
->getMockBuilder('Doctrine\ORM\Persisters\Entity\BasicEntityPersister') |
|
264
|
|
|
->disableOriginalConstructor() |
|
265
|
|
|
->getMock()); |
|
266
|
|
|
|
|
267
|
|
|
$entityManager |
|
268
|
|
|
->method('getRepository') |
|
269
|
|
|
->willReturn($repository); |
|
270
|
|
|
$entityManager |
|
271
|
|
|
->method('getClassMetadata') |
|
272
|
|
|
->willReturn(new ClassMetadata('LAG\AdminBundle\Tests\Entity\TestEntity')); |
|
273
|
|
|
|
|
274
|
|
|
return $entityManager; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return ActionFactory|PHPUnit_Framework_MockObject_MockObject |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function mockActionFactory() |
|
281
|
|
|
{ |
|
282
|
|
|
$actionFactory = $this |
|
283
|
|
|
->getMockBuilder('LAG\AdminBundle\Admin\Factory\ActionFactory') |
|
284
|
|
|
->disableOriginalConstructor() |
|
285
|
|
|
->getMock(); |
|
286
|
|
|
$actionFactory |
|
287
|
|
|
->method('create') |
|
288
|
|
|
->willReturn($this->mockAction('test')); |
|
289
|
|
|
|
|
290
|
|
|
return $actionFactory; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @return TokenStorageInterface|PHPUnit_Framework_MockObject_MockObject |
|
295
|
|
|
*/ |
|
296
|
|
|
protected function mockTokenStorage() |
|
297
|
|
|
{ |
|
298
|
|
|
return $this |
|
299
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') |
|
300
|
|
|
->getMock(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @return ApplicationConfiguration |
|
305
|
|
|
*/ |
|
306
|
|
|
protected function mockApplicationConfiguration() |
|
307
|
|
|
{ |
|
308
|
|
|
$applicationConfiguration = $this |
|
309
|
|
|
->getMockBuilder('LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration') |
|
310
|
|
|
->disableOriginalConstructor() |
|
311
|
|
|
->getMock(); |
|
312
|
|
|
$applicationConfiguration |
|
313
|
|
|
->method('getMaxPerPage') |
|
314
|
|
|
->willReturn(25); |
|
315
|
|
|
|
|
316
|
|
|
return $applicationConfiguration; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return MessageHandlerInterface|PHPUnit_Framework_MockObject_MockObject |
|
321
|
|
|
*/ |
|
322
|
|
|
protected function mockMessageHandler() |
|
323
|
|
|
{ |
|
324
|
|
|
$messageHandler = $this |
|
325
|
|
|
->getMockBuilder('LAG\AdminBundle\Message\MessageHandler') |
|
326
|
|
|
->disableOriginalConstructor() |
|
327
|
|
|
->getMock(); |
|
328
|
|
|
|
|
329
|
|
|
return $messageHandler; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @return TranslatorInterface|PHPUnit_Framework_MockObject_MockObject |
|
334
|
|
|
*/ |
|
335
|
|
|
protected function mockTranslator() |
|
336
|
|
|
{ |
|
337
|
|
|
return $this |
|
338
|
|
|
->getMockBuilder('Symfony\Component\Translation\TranslatorInterface') |
|
339
|
|
|
->getMock(); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @return DataProviderInterface|PHPUnit_Framework_MockObject_MockObject |
|
344
|
|
|
*/ |
|
345
|
|
|
protected function mockDataProvider() |
|
346
|
|
|
{ |
|
347
|
|
|
return $this |
|
348
|
|
|
->getMockBuilder('LAG\AdminBundle\DataProvider\DataProviderInterface') |
|
349
|
|
|
->getMock(); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.