Completed
Push — master ( a2c5ce...b3c15f )
by Ruud
328:10 queued 318:03
created

Configurator/AbstractAdminListConfiguratorTest.php (1 issue)

Upgrade to new PHP Analysis Engine

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->abstractAdminListConfMock->addField('name', 'header', true);
188
        $this->assertCount(1, $this->abstractAdminListConfMock->getFields());
189
    }
190
191
    public function testAddExportField()
192
    {
193
        $this->abstractAdminListConfMock->addExportField('name', 'header', true);
194
        $this->assertCount(1, $this->abstractAdminListConfMock->getExportFields());
195
    }
196
197
    public function testAddFilter()
198
    {
199
        $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']);
200
201
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
202
        $filterBuilderMock
203
            ->expects($this->once())
204
            ->method('add')
205
            ->willReturn(FilterBuilder::class)
206
        ;
207
208
        $abstractAdminListConfMock
209
            ->expects($this->once())
210
            ->method('getFilterBuilder')
211
            ->willReturn($filterBuilderMock)
212
        ;
213
214
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $abstractAdminListConfMock->addFilter('testColumn'));
215
    }
216
217
    public function testGetLimit()
218
    {
219
        $this->assertEquals(10, $this->abstractAdminListConfMock->getLimit());
220
    }
221
222
    public function testGetSortFields()
223
    {
224
        $this->abstractAdminListConfMock->addField('test', 'test', true);
225
        $sortFields = $this->abstractAdminListConfMock->getSortFields();
226
        $this->assertContains('test', $sortFields);
227
    }
228
229
    public function testGetFields()
230
    {
231
        $this->assertIsArray($this->abstractAdminListConfMock->getFields());
232
    }
233
234
    public function testGetExportFields()
235
    {
236
        $this->assertIsArray($this->abstractAdminListConfMock->getExportFields());
237
    }
238
239
    public function testAddSimpleItemAction()
240
    {
241
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addSimpleItemAction('test', 'test', 'test'));
242
    }
243
244
    public function testAddHasGetItemAction()
245
    {
246
        $itemActionInterfaceMock = $this->createMock(ItemActionInterface::class);
247
        $this->abstractAdminListConfMock->addItemAction($itemActionInterfaceMock);
248
        $this->assertTrue($this->abstractAdminListConfMock->hasItemActions());
249
        $this->assertContainsOnlyInstancesOf(ItemActionInterface::class, $this->abstractAdminListConfMock->getItemActions());
250
    }
251
252
    public function testAddHasGetListAction()
253
    {
254
        $listActionInterfaceMock = $this->createMock(ListActionInterface::class);
255
        $this->abstractAdminListConfMock->addListAction($listActionInterfaceMock);
256
        $this->assertTrue($this->abstractAdminListConfMock->hasListActions());
257
        $this->assertContainsOnlyInstancesOf(ListActionInterface::class, $this->abstractAdminListConfMock->getListActions());
258
    }
259
260
    public function testAddHasGetBulkAction()
261
    {
262
        $bulkActionInterfaceMock = $this->createMock(BulkActionInterface::class);
263
        $this->abstractAdminListConfMock->addBulkAction($bulkActionInterfaceMock);
264
        $this->assertTrue($this->abstractAdminListConfMock->hasBulkActions());
265
        $this->assertContainsOnlyInstancesOf(BulkActionInterface::class, $this->abstractAdminListConfMock->getBulkActions());
266
    }
267
268
    public function testGetListTemplate()
269
    {
270
        $this->assertEquals('KunstmaanAdminListBundle:Default:list.html.twig', $this->abstractAdminListConfMock->getListTemplate());
271
    }
272
273
    public function testSetListTemplate()
274
    {
275
        $template = 'test_template';
276
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setListTemplate($template));
277
        $this->assertEquals($template, $this->abstractAdminListConfMock->getListTemplate());
278
    }
279
280
    public function testGetValue()
281
    {
282
        $columnName = 'foo';
283
        $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue(['foo' => 'bar'], $columnName));
284
        $this->assertEquals('', $this->abstractAdminListConfMock->getValue(['foz' => 'bar'], $columnName));
285
286
        $item = new class() {
287
            public function getFoo()
288
            {
289
                return 'bar';
290
            }
291
        };
292
293
        $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue($item, $columnName));
294
        $this->assertEquals(sprintf('undefined function [get/is/has]%s()', $columnName), $this->abstractAdminListConfMock->getValue(new \stdClass(), $columnName));
295
    }
296
297
    public function testgetStringValue()
298
    {
299
        // value = string
300
        $columnName = 'foo';
301
        $this->assertEquals('true', $this->abstractAdminListConfMock->getStringValue(['foo' => true], $columnName));
302
303
        // value = DateTime
304
        $value = new \DateTime();
305
        $this->assertEquals($value->format('Y-m-d H:i:s'), $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
306
307
        // value = empty PersistentCollection
308
        $emMock = $this->createMock(EntityManagerInterface::class);
309
        $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection());
310
        $this->assertEquals('', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
311
312
        // value = PersistentCollection
313
        $emMock = $this->createMock(EntityManagerInterface::class, ['getUnitOfWork']);
0 ignored issues
show
The call to AbstractAdminListConfiguratorTest::createMock() has too many arguments starting with array('getUnitOfWork').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
314
        $emMock
315
            ->expects($this->any())
316
            ->method('getUnitOfWork')
317
            ->willReturn($this->createMock(UnitOfWork::class))
318
        ;
319
320
        $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection());
321
        $value->add(new class() {
322
            public function getName()
323
            {
324
                return 'bar';
325
            }
326
        });
327
        $value->add(new class() {
328
            public function getName()
329
            {
330
                return 'baz';
331
            }
332
        });
333
        $this->assertEquals('bar, baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
334
335
        // value = array
336
        $value = ['bar', 'baz'];
337
        $this->assertEquals('bar, baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
338
339
        // value = non of the above
340
        $value = 'baz';
341
        $this->assertEquals('baz', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
342
    }
343
344
    public function testSetGetAddTemplate()
345
    {
346
        $value = 'test_template';
347
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAddTemplate($value));
348
        $this->assertEquals($value, $this->abstractAdminListConfMock->getAddTemplate());
349
    }
350
351
    public function testSetGetViewTemplate()
352
    {
353
        $value = 'test_template';
354
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setViewTemplate($value));
355
        $this->assertEquals($value, $this->abstractAdminListConfMock->getViewTemplate());
356
    }
357
358
    public function testSetGetEditTemplate()
359
    {
360
        $value = 'test_template';
361
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setEditTemplate($value));
362
        $this->assertEquals($value, $this->abstractAdminListConfMock->getEditTemplate());
363
    }
364
365
    public function testSetGetDeleteTemplate()
366
    {
367
        $value = 'test_template';
368
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setDeleteTemplate($value));
369
        $this->assertEquals($value, $this->abstractAdminListConfMock->getDeleteTemplate());
370
    }
371
372
    public function testDecorateNewEntity()
373
    {
374
        $this->assertInstanceOf(\stdClass::class, $this->abstractAdminListConfMock->decorateNewEntity(new \stdClass()));
375
    }
376
377
    public function testGetFilterBuilder()
378
    {
379
        // test without existsing FilterBuilder
380
        $this->assertInstanceOf(FilterBuilder::class, $this->abstractAdminListConfMock->getFilterBuilder());
381
382
        // test with first a set
383
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setFilterBuilder(new FilterBuilder()));
384
    }
385
386
    public function testBindRequestWithoutExistingSession()
387
    {
388
        $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']);
389
390
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
391
        $filterBuilderMock
392
            ->expects($this->once())
393
            ->method('bindRequest')
394
        ;
395
396
        $abstractAdminListConfMock
397
            ->expects($this->once())
398
            ->method('getFilterBuilder')
399
            ->willReturn($filterBuilderMock)
400
        ;
401
402
        $requestMock = $this->getMockBuilder(Request::class)
403
            ->setConstructorArgs([['page' => 1], [], ['_route' => 'testroute']])
404
            ->setMethods(['getSession'])
405
            ->getMock()
406
        ;
407
        $sessionMock = $this->createMock(Session::class);
408
409
        $requestMock
410
            ->expects($this->once())
411
            ->method('getSession')
412
            ->willReturn($sessionMock)
413
        ;
414
415
        $abstractAdminListConfMock->bindRequest($requestMock);
416
    }
417
418
    public function testBindRequestWithExistingSession()
419
    {
420
        $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']);
421
422
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
423
        $filterBuilderMock
424
            ->expects($this->once())
425
            ->method('bindRequest')
426
        ;
427
428
        $abstractAdminListConfMock
429
            ->expects($this->once())
430
            ->method('getFilterBuilder')
431
            ->willReturn($filterBuilderMock)
432
        ;
433
434
        $requestMock = $this->getMockBuilder(Request::class)
435
            ->setConstructorArgs([[], [], ['_route' => 'testroute']])
436
            ->setMethods(['getSession'])
437
            ->getMock()
438
        ;
439
        $sessionMock = $this->createMock(Session::class);
440
        $sessionMock
441
            ->expects($this->once())
442
            ->method('has')
443
            ->willReturn(true)
444
        ;
445
        $sessionMock
446
            ->expects($this->once())
447
            ->method('get')
448
            ->with('listconfig_testroute')
449
            ->willReturn(['page' => 1, 'orderBy' => 'foo', 'orderDirection' => 'up'])
450
        ;
451
452
        $requestMock
453
            ->expects($this->exactly(2))
454
            ->method('getSession')
455
            ->willReturn($sessionMock)
456
        ;
457
458
        $abstractAdminListConfMock->bindRequest($requestMock);
459
    }
460
461
    public function testGetPage()
462
    {
463
        $this->assertIsInt($this->abstractAdminListConfMock->getPage());
464
    }
465
466
    public function testGetOrderBy()
467
    {
468
        $this->assertIsString($this->abstractAdminListConfMock->getOrderBy());
469
    }
470
471
    public function testGetOrderDirection()
472
    {
473
        $this->assertIsString($this->abstractAdminListConfMock->getOrderDirection());
474
    }
475
476
    public function testGetPathByConvention()
477
    {
478
        $this->assertEquals('bundle_admin_myentity_test', $this->abstractAdminListConfMock->getPathByconvention('test'));
479
    }
480
481
    public function testGetControllerPath()
482
    {
483
        $this->assertEquals('Bundle:MyEntity', $this->abstractAdminListConfMock->getControllerPath());
484
    }
485
486
    public function testGetExtraParameters()
487
    {
488
        $this->assertIsArray($this->abstractAdminListConfMock->getExtraParameters());
489
    }
490
}
491