These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminListBundle\Tests\AdminList\Configurator; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\ORM\EntityManagerInterface; |
||
7 | use Doctrine\ORM\PersistentCollection; |
||
8 | use Doctrine\ORM\UnitOfWork; |
||
9 | use Kunstmaan\AdminListBundle\AdminList\BulkAction\BulkActionInterface; |
||
10 | use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractAdminListConfigurator; |
||
11 | use Kunstmaan\AdminListBundle\AdminList\FilterBuilder; |
||
12 | use Kunstmaan\AdminListBundle\AdminList\ItemAction\ItemActionInterface; |
||
13 | use Kunstmaan\AdminListBundle\AdminList\ListAction\ListActionInterface; |
||
14 | use PHPUnit\Framework\TestCase; |
||
15 | use Symfony\Component\HttpFoundation\Request; |
||
16 | use Symfony\Component\HttpFoundation\Session\Session; |
||
17 | |||
18 | class AbstractAdminListConfiguratorTest extends TestCase |
||
19 | { |
||
20 | /** @var AbstractAdminListConfigurator */ |
||
21 | private $abstractAdminListConfMock; |
||
22 | |||
23 | /** |
||
24 | * Sets up the fixture, for example, opens a network connection. |
||
25 | * This method is called before a test is executed. |
||
26 | */ |
||
27 | protected function setUp() |
||
28 | { |
||
29 | $this->abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class); |
||
30 | $this->abstractAdminListConfMock |
||
31 | ->expects($this->any()) |
||
32 | ->method('getBundleName') |
||
33 | ->willReturn('Bundle') |
||
34 | ; |
||
35 | $this->abstractAdminListConfMock |
||
36 | ->expects($this->any()) |
||
37 | ->method('getEntityName') |
||
38 | ->willReturn('MyEntity') |
||
39 | ; |
||
40 | } |
||
41 | |||
42 | public function testGetRepositoryName() |
||
43 | { |
||
44 | $this->assertEquals('Bundle:MyEntity', $this->abstractAdminListConfMock->getRepositoryName()); |
||
45 | } |
||
46 | |||
47 | public function testBuildExportFields() |
||
48 | { |
||
49 | $this->abstractAdminListConfMock |
||
50 | ->expects($this->once()) |
||
51 | ->method('buildFields') |
||
52 | ->will($this->returnValue(['hello', 'world'])) |
||
53 | ; |
||
54 | |||
55 | $this->assertNull($this->abstractAdminListConfMock->buildExportFields()); |
||
56 | } |
||
57 | |||
58 | public function testResetBuilds() |
||
59 | { |
||
60 | $this->abstractAdminListConfMock->addField('name', 'header', true); |
||
61 | $this->abstractAdminListConfMock->resetBuilds(); |
||
62 | $this->assertCount(0, $this->abstractAdminListConfMock->getFields()); |
||
63 | } |
||
64 | |||
65 | public function testGetAddUrlFor() |
||
66 | { |
||
67 | $addUrl = $this->abstractAdminListConfMock->getAddUrlFor(['paramTest']); |
||
68 | $this->assertArrayHasKey('My Entity', $addUrl); |
||
69 | $this->assertArrayHasKey('path', $addUrl['My Entity']); |
||
70 | $this->assertArrayHasKey('params', $addUrl['My Entity']); |
||
71 | $this->assertEquals('bundle_admin_myentity_add', $addUrl['My Entity']['path']); |
||
72 | $this->assertContains('paramTest', $addUrl['My Entity']['params']); |
||
73 | } |
||
74 | |||
75 | public function testGetExportUrlFor() |
||
76 | { |
||
77 | $exportUrl = $this->abstractAdminListConfMock->getExportUrl(); |
||
78 | $this->assertArrayHasKey('path', $exportUrl); |
||
79 | $this->assertArrayHasKey('params', $exportUrl); |
||
80 | $this->assertEquals('bundle_admin_myentity_export', $exportUrl['path']); |
||
81 | $this->assertArrayHasKey('_format', $exportUrl['params']); |
||
82 | $this->assertEquals('csv', $exportUrl['params']['_format']); |
||
83 | } |
||
84 | |||
85 | public function testGetViewUrlFor() |
||
86 | { |
||
87 | //from array |
||
88 | $item = ['id' => 999]; |
||
89 | $viewUrl = $this->abstractAdminListConfMock->getViewUrlFor($item); |
||
90 | $this->assertArrayHasKey('path', $viewUrl); |
||
91 | $this->assertArrayHasKey('params', $viewUrl); |
||
92 | $this->assertEquals('bundle_admin_myentity_view', $viewUrl['path']); |
||
93 | $this->assertArrayHasKey('id', $viewUrl['params']); |
||
94 | $this->assertEquals('999', $viewUrl['params']['id']); |
||
95 | |||
96 | // from object |
||
97 | $item = new class() { |
||
98 | public function getId() |
||
99 | { |
||
100 | return 3; |
||
101 | } |
||
102 | }; |
||
103 | $viewUrl = $this->abstractAdminListConfMock->getViewUrlFor($item); |
||
104 | $this->assertArrayHasKey('params', $viewUrl); |
||
105 | $this->assertEquals(3, $viewUrl['params']['id']); |
||
106 | } |
||
107 | |||
108 | public function testGetIndexUrl() |
||
109 | { |
||
110 | $indexUrl = $this->abstractAdminListConfMock->getIndexUrl(); |
||
111 | $this->assertArrayHasKey('path', $indexUrl); |
||
112 | $this->assertArrayHasKey('params', $indexUrl); |
||
113 | $this->assertEquals('bundle_admin_myentity', $indexUrl['path']); |
||
114 | $this->assertIsArray($indexUrl['params']); |
||
115 | } |
||
116 | |||
117 | public function testGetAdminTypeExistsInEntity() |
||
118 | { |
||
119 | $entity = new class() { |
||
120 | public function getAdminType() |
||
121 | { |
||
122 | return 'TestType'; |
||
123 | } |
||
124 | }; |
||
125 | |||
126 | $this->assertEquals('TestType', $this->abstractAdminListConfMock->getAdminType($entity)); |
||
127 | } |
||
128 | |||
129 | public function testGetAdminTypeAlreadySet() |
||
130 | { |
||
131 | $this->abstractAdminListConfMock->setAdminType('TestType'); |
||
132 | $this->assertEquals('TestType', $this->abstractAdminListConfMock->getAdminType(new \stdClass())); |
||
133 | } |
||
134 | |||
135 | public function testGetAdminTypeNotExistsInEntity() |
||
136 | { |
||
137 | $this->expectException(\InvalidArgumentException::class); |
||
138 | $entity = new \stdClass(); |
||
139 | $this->abstractAdminListConfMock->getAdminType($entity); |
||
140 | } |
||
141 | |||
142 | public function testSetAdminType() |
||
143 | { |
||
144 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAdminType('TestType')); |
||
145 | } |
||
146 | |||
147 | public function testSetAdminTypeOptions() |
||
148 | { |
||
149 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAdminTypeOptions([])); |
||
150 | } |
||
151 | |||
152 | public function testGetAdminTypeOptions() |
||
153 | { |
||
154 | $this->assertIsArray($this->abstractAdminListConfMock->getAdminTypeOptions()); |
||
155 | } |
||
156 | |||
157 | public function testCanEdit() |
||
158 | { |
||
159 | $item = new \stdClass(); |
||
160 | $this->assertTrue($this->abstractAdminListConfMock->canEdit($item)); |
||
161 | } |
||
162 | |||
163 | public function testCanDelete() |
||
164 | { |
||
165 | $item = new \stdClass(); |
||
166 | $this->assertTrue($this->abstractAdminListConfMock->canDelete($item)); |
||
167 | } |
||
168 | |||
169 | public function testCanAdd() |
||
170 | { |
||
171 | $this->assertTrue($this->abstractAdminListConfMock->canAdd()); |
||
172 | } |
||
173 | |||
174 | public function testCanView() |
||
175 | { |
||
176 | $item = new \stdClass(); |
||
177 | $this->assertFalse($this->abstractAdminListConfMock->canView($item)); |
||
178 | } |
||
179 | |||
180 | public function testCanExport() |
||
181 | { |
||
182 | $this->assertFalse($this->abstractAdminListConfMock->canExport()); |
||
183 | } |
||
184 | |||
185 | public function testAddField() |
||
186 | { |
||
187 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addField('name', 'header', true)); |
||
188 | $this->assertCount(1, $this->abstractAdminListConfMock->getFields()); |
||
189 | } |
||
190 | |||
191 | public function testAddExportField() |
||
192 | { |
||
193 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addExportField('name', 'header', true)); |
||
194 | $exportFields = $this->abstractAdminListConfMock->getExportFields(); |
||
195 | $this->assertCount(1, $exportFields); |
||
196 | |||
197 | $this->assertFalse($exportFields[0]->isSortable()); |
||
198 | } |
||
199 | |||
200 | public function testAddFilter() |
||
201 | { |
||
202 | $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']); |
||
203 | |||
204 | $filterBuilderMock = $this->createMock(FilterBuilder::class); |
||
205 | $filterBuilderMock |
||
206 | ->expects($this->once()) |
||
207 | ->method('add') |
||
208 | ->willReturn(FilterBuilder::class) |
||
209 | ; |
||
210 | |||
211 | $abstractAdminListConfMock |
||
212 | ->expects($this->once()) |
||
213 | ->method('getFilterBuilder') |
||
214 | ->willReturn($filterBuilderMock) |
||
215 | ; |
||
216 | |||
217 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $abstractAdminListConfMock->addFilter('testColumn')); |
||
218 | } |
||
219 | |||
220 | public function testGetLimit() |
||
221 | { |
||
222 | $this->assertEquals(10, $this->abstractAdminListConfMock->getLimit()); |
||
223 | } |
||
224 | |||
225 | public function testGetSortFields() |
||
226 | { |
||
227 | $this->abstractAdminListConfMock->addField('test', 'test', true); |
||
228 | $sortFields = $this->abstractAdminListConfMock->getSortFields(); |
||
229 | $this->assertContains('test', $sortFields); |
||
230 | } |
||
231 | |||
232 | public function testGetFields() |
||
233 | { |
||
234 | $this->assertIsArray($this->abstractAdminListConfMock->getFields()); |
||
235 | } |
||
236 | |||
237 | public function testGetExportFields() |
||
238 | { |
||
239 | $this->assertIsArray($this->abstractAdminListConfMock->getExportFields()); |
||
240 | } |
||
241 | |||
242 | public function testAddSimpleItemAction() |
||
243 | { |
||
244 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addSimpleItemAction('test', 'test', 'test')); |
||
245 | } |
||
246 | |||
247 | public function testAddHasGetItemAction() |
||
248 | { |
||
249 | $itemActionInterfaceMock = $this->createMock(ItemActionInterface::class); |
||
250 | $this->abstractAdminListConfMock->addItemAction($itemActionInterfaceMock); |
||
251 | $this->assertTrue($this->abstractAdminListConfMock->hasItemActions()); |
||
252 | $this->assertContainsOnlyInstancesOf(ItemActionInterface::class, $this->abstractAdminListConfMock->getItemActions()); |
||
253 | } |
||
254 | |||
255 | View Code Duplication | public function testAddHasGetListAction() |
|
0 ignored issues
–
show
|
|||
256 | { |
||
257 | $listActionInterfaceMock = $this->createMock(ListActionInterface::class); |
||
258 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addListAction($listActionInterfaceMock)); |
||
259 | $this->assertTrue($this->abstractAdminListConfMock->hasListActions()); |
||
260 | $this->assertContainsOnlyInstancesOf(ListActionInterface::class, $this->abstractAdminListConfMock->getListActions()); |
||
261 | } |
||
262 | |||
263 | View Code Duplication | public function testAddHasGetBulkAction() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
264 | { |
||
265 | $bulkActionInterfaceMock = $this->createMock(BulkActionInterface::class); |
||
266 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addBulkAction($bulkActionInterfaceMock)); |
||
267 | $this->assertTrue($this->abstractAdminListConfMock->hasBulkActions()); |
||
268 | $this->assertContainsOnlyInstancesOf(BulkActionInterface::class, $this->abstractAdminListConfMock->getBulkActions()); |
||
269 | } |
||
270 | |||
271 | public function testGetListTemplate() |
||
272 | { |
||
273 | $this->assertEquals('@KunstmaanAdminList/Default/list.html.twig', $this->abstractAdminListConfMock->getListTemplate()); |
||
274 | } |
||
275 | |||
276 | public function testSetListTemplate() |
||
277 | { |
||
278 | $template = 'test_template'; |
||
279 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setListTemplate($template)); |
||
280 | $this->assertEquals($template, $this->abstractAdminListConfMock->getListTemplate()); |
||
281 | } |
||
282 | |||
283 | public function testGetValue() |
||
284 | { |
||
285 | $columnName = 'foo'; |
||
286 | $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue(['foo' => 'bar'], $columnName)); |
||
287 | $this->assertEquals('', $this->abstractAdminListConfMock->getValue(['foz' => 'bar'], $columnName)); |
||
288 | |||
289 | $item = new class() { |
||
290 | public function getFoo() |
||
291 | { |
||
292 | return 'bar'; |
||
293 | } |
||
294 | }; |
||
295 | |||
296 | $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue($item, $columnName)); |
||
297 | $this->assertEquals(sprintf('undefined function [get/is/has]%s()', $columnName), $this->abstractAdminListConfMock->getValue(new \stdClass(), $columnName)); |
||
298 | } |
||
299 | |||
300 | public function testgetStringValue() |
||
301 | { |
||
302 | // value = string |
||
303 | $columnName = 'foo'; |
||
304 | $this->assertEquals('true', $this->abstractAdminListConfMock->getStringValue(['foo' => true], $columnName)); |
||
305 | |||
306 | // value = DateTime |
||
307 | $value = new \DateTime(); |
||
308 | $this->assertEquals($value->format('Y-m-d H:i:s'), $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName)); |
||
309 | |||
310 | // value = empty PersistentCollection |
||
311 | $emMock = $this->createMock(EntityManagerInterface::class); |
||
312 | $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection()); |
||
313 | $this->assertEquals('', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName)); |
||
314 | |||
315 | // value = PersistentCollection |
||
316 | $emMock = $this->createMock(EntityManagerInterface::class); |
||
317 | $emMock |
||
318 | ->expects($this->any()) |
||
319 | ->method('getUnitOfWork') |
||
320 | ->willReturn($this->createMock(UnitOfWork::class)) |
||
321 | ; |
||
322 | |||
323 | $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection()); |
||
324 | $value->add(new class() { |
||
325 | public function getName() |
||
326 | { |
||
327 | return 'bar'; |
||
328 | } |
||
329 | }); |
||
330 | $value->add(new class() { |
||
331 | public function getName() |
||
332 | { |
||
333 | return 'baz'; |
||
334 | } |
||
335 | }); |
||
336 | $this->assertEquals('bar, baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName)); |
||
337 | |||
338 | // value = array |
||
339 | $value = ['bar', 'baz']; |
||
340 | $this->assertEquals('bar, baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName)); |
||
341 | |||
342 | // value = non of the above |
||
343 | $value = 'baz'; |
||
344 | $this->assertEquals('baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName)); |
||
345 | } |
||
346 | |||
347 | public function testSetGetAddTemplate() |
||
348 | { |
||
349 | $value = 'test_template'; |
||
350 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAddTemplate($value)); |
||
351 | $this->assertEquals($value, $this->abstractAdminListConfMock->getAddTemplate()); |
||
352 | } |
||
353 | |||
354 | public function testSetGetViewTemplate() |
||
355 | { |
||
356 | $value = 'test_template'; |
||
357 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setViewTemplate($value)); |
||
358 | $this->assertEquals($value, $this->abstractAdminListConfMock->getViewTemplate()); |
||
359 | } |
||
360 | |||
361 | public function testSetGetEditTemplate() |
||
362 | { |
||
363 | $value = 'test_template'; |
||
364 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setEditTemplate($value)); |
||
365 | $this->assertEquals($value, $this->abstractAdminListConfMock->getEditTemplate()); |
||
366 | } |
||
367 | |||
368 | public function testSetGetDeleteTemplate() |
||
369 | { |
||
370 | $value = 'test_template'; |
||
371 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setDeleteTemplate($value)); |
||
372 | $this->assertEquals($value, $this->abstractAdminListConfMock->getDeleteTemplate()); |
||
373 | } |
||
374 | |||
375 | public function testDecorateNewEntity() |
||
376 | { |
||
377 | $this->assertInstanceOf(\stdClass::class, $this->abstractAdminListConfMock->decorateNewEntity(new \stdClass())); |
||
378 | } |
||
379 | |||
380 | public function testGetFilterBuilder() |
||
381 | { |
||
382 | // test without existsing FilterBuilder |
||
383 | $this->assertInstanceOf(FilterBuilder::class, $this->abstractAdminListConfMock->getFilterBuilder()); |
||
384 | |||
385 | // test with first a set |
||
386 | $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setFilterBuilder(new FilterBuilder())); |
||
387 | } |
||
388 | |||
389 | public function testBindRequestWithoutExistingSession() |
||
390 | { |
||
391 | $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']); |
||
392 | |||
393 | $filterBuilderMock = $this->createMock(FilterBuilder::class); |
||
394 | $filterBuilderMock |
||
395 | ->expects($this->once()) |
||
396 | ->method('bindRequest') |
||
397 | ; |
||
398 | |||
399 | $abstractAdminListConfMock |
||
400 | ->expects($this->once()) |
||
401 | ->method('getFilterBuilder') |
||
402 | ->willReturn($filterBuilderMock) |
||
403 | ; |
||
404 | |||
405 | $requestMock = $this->getMockBuilder(Request::class) |
||
406 | ->setConstructorArgs([['page' => 1], [], ['_route' => 'testroute']]) |
||
407 | ->setMethods(['getSession']) |
||
408 | ->getMock() |
||
409 | ; |
||
410 | $sessionMock = $this->createMock(Session::class); |
||
411 | |||
412 | $requestMock |
||
413 | ->expects($this->once()) |
||
414 | ->method('getSession') |
||
415 | ->willReturn($sessionMock) |
||
416 | ; |
||
417 | |||
418 | $abstractAdminListConfMock->bindRequest($requestMock); |
||
419 | } |
||
420 | |||
421 | public function testBindRequestWithExistingSession() |
||
422 | { |
||
423 | $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']); |
||
424 | |||
425 | $filterBuilderMock = $this->createMock(FilterBuilder::class); |
||
426 | $filterBuilderMock |
||
427 | ->expects($this->once()) |
||
428 | ->method('bindRequest') |
||
429 | ; |
||
430 | |||
431 | $abstractAdminListConfMock |
||
432 | ->expects($this->once()) |
||
433 | ->method('getFilterBuilder') |
||
434 | ->willReturn($filterBuilderMock) |
||
435 | ; |
||
436 | |||
437 | $requestMock = $this->getMockBuilder(Request::class) |
||
438 | ->setConstructorArgs([[], [], ['_route' => 'testroute']]) |
||
439 | ->setMethods(['getSession']) |
||
440 | ->getMock() |
||
441 | ; |
||
442 | $sessionMock = $this->createMock(Session::class); |
||
443 | $sessionMock |
||
444 | ->expects($this->once()) |
||
445 | ->method('has') |
||
446 | ->willReturn(true) |
||
447 | ; |
||
448 | $sessionMock |
||
449 | ->expects($this->once()) |
||
450 | ->method('get') |
||
451 | ->with('listconfig_testroute') |
||
452 | ->willReturn(['page' => 1, 'orderBy' => 'foo', 'orderDirection' => 'up']) |
||
453 | ; |
||
454 | |||
455 | $requestMock |
||
456 | ->expects($this->exactly(2)) |
||
457 | ->method('getSession') |
||
458 | ->willReturn($sessionMock) |
||
459 | ; |
||
460 | |||
461 | $abstractAdminListConfMock->bindRequest($requestMock); |
||
462 | } |
||
463 | |||
464 | public function testGetPage() |
||
465 | { |
||
466 | $this->assertIsInt($this->abstractAdminListConfMock->getPage()); |
||
467 | } |
||
468 | |||
469 | public function testGetOrderBy() |
||
470 | { |
||
471 | $this->assertIsString($this->abstractAdminListConfMock->getOrderBy()); |
||
472 | } |
||
473 | |||
474 | public function testGetOrderDirection() |
||
475 | { |
||
476 | $this->assertIsString($this->abstractAdminListConfMock->getOrderDirection()); |
||
477 | } |
||
478 | |||
479 | public function testGetPathByConvention() |
||
480 | { |
||
481 | $this->assertEquals('bundle_admin_myentity_test', $this->abstractAdminListConfMock->getPathByconvention('test')); |
||
482 | } |
||
483 | |||
484 | public function testGetControllerPath() |
||
485 | { |
||
486 | $this->assertEquals('Bundle:MyEntity', $this->abstractAdminListConfMock->getControllerPath()); |
||
487 | } |
||
488 | |||
489 | public function testGetExtraParameters() |
||
490 | { |
||
491 | $this->assertIsArray($this->abstractAdminListConfMock->getExtraParameters()); |
||
492 | } |
||
493 | } |
||
494 |
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.