Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Configurator/AbstractAdminListConfiguratorTest.php (8 issues)

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
    protected function setUp()
24
    {
25
        $this->abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...istConfigurator::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\AdminLi...tAdminListConfigurator> of property $abstractAdminListConfMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
        $this->abstractAdminListConfMock
27
            ->expects($this->any())
28
            ->method('getBundleName')
29
            ->willReturn('Bundle')
30
        ;
31
        $this->abstractAdminListConfMock
32
            ->expects($this->any())
33
            ->method('getEntityName')
34
            ->willReturn('MyEntity')
35
        ;
36
    }
37
38
    public function testGetRepositoryName()
39
    {
40
        $this->assertEquals('Bundle:MyEntity', $this->abstractAdminListConfMock->getRepositoryName());
41
    }
42
43
    public function testBuildExportFields()
44
    {
45
        $this->abstractAdminListConfMock
46
            ->expects($this->once())
47
            ->method('buildFields')
48
            ->will($this->returnValue(['hello', 'world']))
49
        ;
50
51
        $this->assertNull($this->abstractAdminListConfMock->buildExportFields());
52
    }
53
54
    public function testResetBuilds()
55
    {
56
        $this->abstractAdminListConfMock->addField('name', 'header', true);
57
        $this->abstractAdminListConfMock->resetBuilds();
58
        $this->assertCount(0, $this->abstractAdminListConfMock->getFields());
0 ignored issues
show
$this->abstractAdminListConfMock->getFields() is of type array<integer,object<Kun...undle\AdminList\Field>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
    }
60
61
    public function testGetAddUrlFor()
62
    {
63
        $addUrl = $this->abstractAdminListConfMock->getAddUrlFor(['paramTest']);
64
        $this->assertArrayHasKey('My Entity', $addUrl);
65
        $this->assertArrayHasKey('path', $addUrl['My Entity']);
66
        $this->assertArrayHasKey('params', $addUrl['My Entity']);
67
        $this->assertEquals('bundle_admin_myentity_add', $addUrl['My Entity']['path']);
68
        $this->assertContains('paramTest', $addUrl['My Entity']['params']);
69
    }
70
71
    public function testGetExportUrlFor()
72
    {
73
        $exportUrl = $this->abstractAdminListConfMock->getExportUrl();
74
        $this->assertArrayHasKey('path', $exportUrl);
75
        $this->assertArrayHasKey('params', $exportUrl);
76
        $this->assertEquals('bundle_admin_myentity_export', $exportUrl['path']);
77
        $this->assertArrayHasKey('_format', $exportUrl['params']);
78
        $this->assertEquals('csv', $exportUrl['params']['_format']);
79
    }
80
81
    public function testGetViewUrlFor()
82
    {
83
        //from array
84
        $item = ['id' => 999];
85
        $viewUrl = $this->abstractAdminListConfMock->getViewUrlFor($item);
86
        $this->assertArrayHasKey('path', $viewUrl);
87
        $this->assertArrayHasKey('params', $viewUrl);
88
        $this->assertEquals('bundle_admin_myentity_view', $viewUrl['path']);
89
        $this->assertArrayHasKey('id', $viewUrl['params']);
90
        $this->assertEquals('999', $viewUrl['params']['id']);
91
92
        // from object
93
        $item = new class() {
94
            public function getId()
95
            {
96
                return 3;
97
            }
98
        };
99
        $viewUrl = $this->abstractAdminListConfMock->getViewUrlFor($item);
100
        $this->assertArrayHasKey('params', $viewUrl);
101
        $this->assertEquals(3, $viewUrl['params']['id']);
102
    }
103
104
    public function testGetIndexUrl()
105
    {
106
        $indexUrl = $this->abstractAdminListConfMock->getIndexUrl();
107
        $this->assertArrayHasKey('path', $indexUrl);
108
        $this->assertArrayHasKey('params', $indexUrl);
109
        $this->assertEquals('bundle_admin_myentity', $indexUrl['path']);
110
        $this->assertIsArray($indexUrl['params']);
111
    }
112
113
    public function testGetAdminTypeExistsInEntity()
114
    {
115
        $entity = new class() {
116
            public function getAdminType()
117
            {
118
                return 'TestType';
119
            }
120
        };
121
122
        $this->assertEquals('TestType', $this->abstractAdminListConfMock->getAdminType($entity));
123
    }
124
125
    public function testGetAdminTypeAlreadySet()
126
    {
127
        $this->abstractAdminListConfMock->setAdminType('TestType');
128
        $this->assertEquals('TestType', $this->abstractAdminListConfMock->getAdminType(new \stdClass()));
129
    }
130
131
    public function testGetAdminTypeNotExistsInEntity()
132
    {
133
        $this->expectException(\InvalidArgumentException::class);
134
        $entity = new \stdClass();
135
        $this->abstractAdminListConfMock->getAdminType($entity);
136
    }
137
138
    public function testSetAdminType()
139
    {
140
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAdminType('TestType'));
141
    }
142
143
    public function testSetAdminTypeOptions()
144
    {
145
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setAdminTypeOptions([]));
146
    }
147
148
    public function testGetAdminTypeOptions()
149
    {
150
        $this->assertIsArray($this->abstractAdminListConfMock->getAdminTypeOptions());
151
    }
152
153
    public function testCanEdit()
154
    {
155
        $item = new \stdClass();
156
        $this->assertTrue($this->abstractAdminListConfMock->canEdit($item));
157
    }
158
159
    public function testCanDelete()
160
    {
161
        $item = new \stdClass();
162
        $this->assertTrue($this->abstractAdminListConfMock->canDelete($item));
163
    }
164
165
    public function testCanAdd()
166
    {
167
        $this->assertTrue($this->abstractAdminListConfMock->canAdd());
168
    }
169
170
    public function testCanView()
171
    {
172
        $item = new \stdClass();
173
        $this->assertFalse($this->abstractAdminListConfMock->canView($item));
174
    }
175
176
    public function testCanExport()
177
    {
178
        $this->assertFalse($this->abstractAdminListConfMock->canExport());
179
    }
180
181
    public function testAddField()
182
    {
183
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addField('name', 'header', true));
184
        $this->assertCount(1, $this->abstractAdminListConfMock->getFields());
0 ignored issues
show
$this->abstractAdminListConfMock->getFields() is of type array<integer,object<Kun...undle\AdminList\Field>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
    }
186
187
    public function testAddExportField()
188
    {
189
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addExportField('name', 'header', true));
190
        $exportFields = $this->abstractAdminListConfMock->getExportFields();
191
        $this->assertCount(1, $exportFields);
192
193
        $this->assertFalse($exportFields[0]->isSortable());
194
    }
195
196
    public function testAddFilter()
197
    {
198
        $abstractAdminListConfMock = $this->getMockForAbstractClass(AbstractAdminListConfigurator::class, [], '', true, true, true, ['getFilterBuilder']);
199
200
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
201
        $filterBuilderMock
202
            ->expects($this->once())
203
            ->method('add')
204
            ->willReturn(FilterBuilder::class)
205
        ;
206
207
        $abstractAdminListConfMock
208
            ->expects($this->once())
209
            ->method('getFilterBuilder')
210
            ->willReturn($filterBuilderMock)
211
        ;
212
213
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $abstractAdminListConfMock->addFilter('testColumn'));
214
    }
215
216
    public function testGetLimit()
217
    {
218
        $this->assertEquals(10, $this->abstractAdminListConfMock->getLimit());
219
    }
220
221
    public function testGetSortFields()
222
    {
223
        $this->abstractAdminListConfMock->addField('test', 'test', true);
224
        $sortFields = $this->abstractAdminListConfMock->getSortFields();
225
        $this->assertContains('test', $sortFields);
226
    }
227
228
    public function testGetFields()
229
    {
230
        $this->assertIsArray($this->abstractAdminListConfMock->getFields());
231
    }
232
233
    public function testGetExportFields()
234
    {
235
        $this->assertIsArray($this->abstractAdminListConfMock->getExportFields());
236
    }
237
238
    public function testAddSimpleItemAction()
239
    {
240
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addSimpleItemAction('test', 'test', 'test'));
241
    }
242
243
    public function testAddHasGetItemAction()
244
    {
245
        $itemActionInterfaceMock = $this->createMock(ItemActionInterface::class);
246
        $this->abstractAdminListConfMock->addItemAction($itemActionInterfaceMock);
0 ignored issues
show
$itemActionInterfaceMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminLi...on\ItemActionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
247
        $this->assertTrue($this->abstractAdminListConfMock->hasItemActions());
248
        $this->assertContainsOnlyInstancesOf(ItemActionInterface::class, $this->abstractAdminListConfMock->getItemActions());
249
    }
250
251 View Code Duplication
    public function testAddHasGetListAction()
252
    {
253
        $listActionInterfaceMock = $this->createMock(ListActionInterface::class);
254
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addListAction($listActionInterfaceMock));
255
        $this->assertTrue($this->abstractAdminListConfMock->hasListActions());
256
        $this->assertContainsOnlyInstancesOf(ListActionInterface::class, $this->abstractAdminListConfMock->getListActions());
257
    }
258
259 View Code Duplication
    public function testAddHasGetBulkAction()
260
    {
261
        $bulkActionInterfaceMock = $this->createMock(BulkActionInterface::class);
262
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->addBulkAction($bulkActionInterfaceMock));
263
        $this->assertTrue($this->abstractAdminListConfMock->hasBulkActions());
264
        $this->assertContainsOnlyInstancesOf(BulkActionInterface::class, $this->abstractAdminListConfMock->getBulkActions());
265
    }
266
267
    public function testGetListTemplate()
268
    {
269
        $this->assertEquals('@KunstmaanAdminList/Default/list.html.twig', $this->abstractAdminListConfMock->getListTemplate());
270
    }
271
272
    public function testSetListTemplate()
273
    {
274
        $template = 'test_template';
275
        $this->assertInstanceOf(AbstractAdminListConfigurator::class, $this->abstractAdminListConfMock->setListTemplate($template));
276
        $this->assertEquals($template, $this->abstractAdminListConfMock->getListTemplate());
277
    }
278
279
    public function testGetValue()
280
    {
281
        $columnName = 'foo';
282
        $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue(['foo' => 'bar'], $columnName));
283
        $this->assertEquals('', $this->abstractAdminListConfMock->getValue(['foz' => 'bar'], $columnName));
284
285
        $item = new class() {
286
            public function getFoo()
287
            {
288
                return 'bar';
289
            }
290
        };
291
292
        $this->assertEquals('bar', $this->abstractAdminListConfMock->getValue($item, $columnName));
293
        $this->assertEquals(sprintf('undefined function [get/is/has]%s()', $columnName), $this->abstractAdminListConfMock->getValue(new \stdClass(), $columnName));
294
    }
295
296
    public function testgetStringValue()
297
    {
298
        // value = string
299
        $columnName = 'foo';
300
        $this->assertEquals('true', $this->abstractAdminListConfMock->getStringValue(['foo' => true], $columnName));
301
302
        // value = DateTime
303
        $value = new \DateTime();
304
        $this->assertEquals($value->format('Y-m-d H:i:s'), $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
305
306
        // value = empty PersistentCollection
307
        $emMock = $this->createMock(EntityManagerInterface::class);
308
        $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection());
0 ignored issues
show
$emMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
'ClassName' is of type string, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
309
        $this->assertEquals('', $this->abstractAdminListConfMock->getStringValue(['foo' => $value], $columnName));
310
311
        // value = PersistentCollection
312
        $emMock = $this->createMock(EntityManagerInterface::class);
313
        $emMock
314
            ->expects($this->any())
315
            ->method('getUnitOfWork')
316
            ->willReturn($this->createMock(UnitOfWork::class))
317
        ;
318
319
        $value = new PersistentCollection($emMock, 'ClassName', new ArrayCollection());
0 ignored issues
show
$emMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
'ClassName' is of type string, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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