Completed
Pull Request — master (#4831)
by Jordi Sala
10:38 queued 05:56
created

DatagridTest::setUp()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 30
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Datagrid;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
16
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
17
use Sonata\AdminBundle\Datagrid\Datagrid;
18
use Sonata\AdminBundle\Datagrid\PagerInterface;
19
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
20
use Sonata\AdminBundle\Filter\FilterInterface;
21
use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\TestEntity;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\Form\Form;
24
use Symfony\Component\Form\FormBuilder;
25
use Symfony\Component\Form\FormFactoryInterface;
26
27
/**
28
 * @author Andrej Hudec <[email protected]>
29
 */
30
class DatagridTest extends TestCase
31
{
32
    /**
33
     * @var Datagrid
34
     */
35
    private $datagrid;
36
37
    /**
38
     * @var PagerInterface
39
     */
40
    private $pager;
41
42
    /**
43
     * @var ProxyQueryInterface
44
     */
45
    private $query;
46
47
    /**
48
     * @var FieldDescriptionCollection
49
     */
50
    private $columns;
51
52
    /**
53
     * @var FormBuilder
54
     */
55
    private $formBuilder;
56
57
    /**
58
     * @var array
59
     */
60
    private $formTypes;
61
62
    public function setUp()
63
    {
64
        $this->query = $this->createMock(ProxyQueryInterface::class);
65
        $this->columns = new FieldDescriptionCollection();
66
        $this->pager = $this->createMock(PagerInterface::class);
67
68
        $this->formTypes = [];
69
70
        $this->formBuilder = $this->getMockBuilder(FormBuilder::class)
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
74
        $this->formBuilder->expects($this->any())
75
            ->method('get')
76
            ->will($this->returnCallback(function ($name) {
77
                if (isset($this->formTypes[$name])) {
78
                    return $this->formTypes[$name];
79
                }
80
            }));
81
82
        $this->formBuilder->expects($this->any())
83
            ->method('add')
84
            ->will($this->returnCallback(function ($name, $type, $options) {
85
                $this->formTypes[$name] = new FormBuilder(
86
                    $name,
87
                    TestEntity::class,
88
                    $this->createMock(EventDispatcherInterface::class),
89
                    $this->createMock(FormFactoryInterface::class),
90
                    $options
91
                );
92
            }));
93
94
        $this->formBuilder->expects($this->any())
95
            ->method('getForm')
96
            ->will($this->returnCallback(function () {
97
                return $this->getMockBuilder(Form::class)
98
                    ->disableOriginalConstructor()
99
                    ->getMock();
100
            }));
101
102
        $values = [];
103
104
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, $values);
105
    }
106
107
    public function testGetPager()
108
    {
109
        $this->assertSame($this->pager, $this->datagrid->getPager());
110
    }
111
112
    public function testFilter()
113
    {
114
        $this->assertFalse($this->datagrid->hasFilter('foo'));
115
        $this->assertNull($this->datagrid->getFilter('foo'));
116
117
        $filter = $this->createMock(FilterInterface::class);
118
        $filter->expects($this->once())
119
            ->method('getName')
120
            ->will($this->returnValue('foo'));
121
122
        $this->datagrid->addFilter($filter);
123
124
        $this->assertTrue($this->datagrid->hasFilter('foo'));
125
        $this->assertFalse($this->datagrid->hasFilter('nonexistent'));
126
        $this->assertSame($filter, $this->datagrid->getFilter('foo'));
127
128
        $this->datagrid->removeFilter('foo');
129
130
        $this->assertFalse($this->datagrid->hasFilter('foo'));
131
    }
132
133
    public function testGetFilters()
134
    {
135
        $this->assertSame([], $this->datagrid->getFilters());
136
137
        $filter1 = $this->createMock(FilterInterface::class);
138
        $filter1->expects($this->once())
139
            ->method('getName')
140
            ->will($this->returnValue('foo'));
141
142
        $filter2 = $this->createMock(FilterInterface::class);
143
        $filter2->expects($this->once())
144
            ->method('getName')
145
            ->will($this->returnValue('bar'));
146
147
        $filter3 = $this->createMock(FilterInterface::class);
148
        $filter3->expects($this->once())
149
            ->method('getName')
150
            ->will($this->returnValue('baz'));
151
152
        $this->datagrid->addFilter($filter1);
153
        $this->datagrid->addFilter($filter2);
154
        $this->datagrid->addFilter($filter3);
155
156
        $this->assertSame(['foo' => $filter1, 'bar' => $filter2, 'baz' => $filter3], $this->datagrid->getFilters());
157
158
        $this->datagrid->removeFilter('bar');
159
160
        $this->assertSame(['foo' => $filter1, 'baz' => $filter3], $this->datagrid->getFilters());
161
    }
162
163
    public function testReorderFilters()
164
    {
165
        $this->assertSame([], $this->datagrid->getFilters());
166
167
        $filter1 = $this->createMock(FilterInterface::class);
168
        $filter1->expects($this->once())
169
            ->method('getName')
170
            ->will($this->returnValue('foo'));
171
172
        $filter2 = $this->createMock(FilterInterface::class);
173
        $filter2->expects($this->once())
174
            ->method('getName')
175
            ->will($this->returnValue('bar'));
176
177
        $filter3 = $this->createMock(FilterInterface::class);
178
        $filter3->expects($this->once())
179
            ->method('getName')
180
            ->will($this->returnValue('baz'));
181
182
        $this->datagrid->addFilter($filter1);
183
        $this->datagrid->addFilter($filter2);
184
        $this->datagrid->addFilter($filter3);
185
186
        $this->assertSame(['foo' => $filter1, 'bar' => $filter2, 'baz' => $filter3], $this->datagrid->getFilters());
187
        $this->assertSame(['foo', 'bar', 'baz'], array_keys($this->datagrid->getFilters()));
188
189
        $this->datagrid->reorderFilters(['bar', 'baz', 'foo']);
190
191
        $this->assertSame(['bar' => $filter2, 'baz' => $filter3, 'foo' => $filter1], $this->datagrid->getFilters());
192
        $this->assertSame(['bar', 'baz', 'foo'], array_keys($this->datagrid->getFilters()));
193
    }
194
195
    public function testGetValues()
196
    {
197
        $this->assertSame([], $this->datagrid->getValues());
198
199
        $this->datagrid->setValue('foo', 'bar', 'baz');
200
201
        $this->assertSame(['foo' => ['type' => 'bar', 'value' => 'baz']], $this->datagrid->getValues());
202
    }
203
204
    public function testGetColumns()
205
    {
206
        $this->assertSame($this->columns, $this->datagrid->getColumns());
207
    }
208
209
    public function testGetQuery()
210
    {
211
        $this->assertSame($this->query, $this->datagrid->getQuery());
212
    }
213
214
    public function testHasActiveFilters()
215
    {
216
        $this->assertFalse($this->datagrid->hasActiveFilters());
217
218
        $filter1 = $this->createMock(FilterInterface::class);
219
        $filter1->expects($this->once())
220
            ->method('getName')
221
            ->will($this->returnValue('foo'));
222
        $filter1->expects($this->any())
223
            ->method('isActive')
224
            ->will($this->returnValue(false));
225
226
        $this->datagrid->addFilter($filter1);
227
228
        $this->assertFalse($this->datagrid->hasActiveFilters());
229
230
        $filter2 = $this->createMock(FilterInterface::class);
231
        $filter2->expects($this->once())
232
            ->method('getName')
233
            ->will($this->returnValue('bar'));
234
        $filter2->expects($this->any())
235
            ->method('isActive')
236
            ->will($this->returnValue(true));
237
238
        $this->datagrid->addFilter($filter2);
239
240
        $this->assertTrue($this->datagrid->hasActiveFilters());
241
    }
242
243
    public function testHasDisplayableFilters()
244
    {
245
        $this->assertFalse($this->datagrid->hasDisplayableFilters());
246
    }
247
248
    public function testHasDisplayableFiltersNotActive()
249
    {
250
        $filter = $this->createMock(FilterInterface::class);
251
        $filter->expects($this->once())
252
            ->method('getName')
253
            ->will($this->returnValue('foo'));
254
        $filter->expects($this->any())
255
            ->method('getOption')
256
            ->will($this->returnValue(false));
257
        $filter->expects($this->any())
258
            ->method('isActive')
259
            ->will($this->returnValue(false));
260
261
        $this->datagrid->addFilter($filter);
262
263
        $this->assertFalse($this->datagrid->hasDisplayableFilters());
264
    }
265
266
    public function testHasDisplayableFiltersActive()
267
    {
268
        $filter = $this->createMock(FilterInterface::class);
269
        $filter->expects($this->once())
270
            ->method('getName')
271
            ->will($this->returnValue('bar'));
272
        $filter->expects($this->any())
273
            ->method('getOption')
274
            ->will($this->returnValue(true));
275
        $filter->expects($this->any())
276
            ->method('isActive')
277
            ->will($this->returnValue(true));
278
279
        $this->datagrid->addFilter($filter);
280
281
        $this->assertTrue($this->datagrid->hasDisplayableFilters());
282
    }
283
284
    public function testHasDisplayableFiltersAlwaysShow()
285
    {
286
        $filter = $this->createMock(FilterInterface::class);
287
        $filter->expects($this->once())
288
            ->method('getName')
289
            ->will($this->returnValue('bar'));
290
        $filter->expects($this->any())
291
            ->method('getOption')
292
            ->with($this->equalTo('show_filter'))
293
            ->will($this->returnValue(true));
294
        $filter->expects($this->any())
295
            ->method('isActive')
296
            ->will($this->returnValue(false));
297
298
        $this->datagrid->addFilter($filter);
299
300
        $this->assertTrue($this->datagrid->hasDisplayableFilters());
301
    }
302
303
    public function testGetForm()
304
    {
305
        $this->assertInstanceOf(Form::class, $this->datagrid->getForm());
306
    }
307
308
    public function testGetResults()
309
    {
310
        $this->assertNull($this->datagrid->getResults());
311
312
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
            ->method('getResults')
314
            ->will($this->returnValue(['foo', 'bar']));
315
316
        $this->assertSame(['foo', 'bar'], $this->datagrid->getResults());
317
    }
318
319
    public function testEmptyResults()
320
    {
321
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
322
            ->method('getResults')
323
            ->will($this->returnValue([]));
324
325
        $this->assertSame([], $this->datagrid->getResults());
326
        $this->assertSame([], $this->datagrid->getResults());
327
    }
328
329
    public function testBuildPager()
330
    {
331
        $filter1 = $this->createMock(FilterInterface::class);
332
        $filter1->expects($this->once())
333
            ->method('getName')
334
            ->will($this->returnValue('foo'));
335
        $filter1->expects($this->any())
336
            ->method('getFormName')
337
            ->will($this->returnValue('fooFormName'));
338
        $filter1->expects($this->any())
339
            ->method('isActive')
340
            ->will($this->returnValue(false));
341
        $filter1->expects($this->any())
342
            ->method('getRenderSettings')
343
            ->will($this->returnValue(['foo1', ['bar1' => 'baz1']]));
344
345
        $this->datagrid->addFilter($filter1);
346
347
        $filter2 = $this->createMock(FilterInterface::class);
348
        $filter2->expects($this->once())
349
            ->method('getName')
350
            ->will($this->returnValue('bar'));
351
        $filter2->expects($this->any())
352
            ->method('getFormName')
353
            ->will($this->returnValue('barFormName'));
354
        $filter2->expects($this->any())
355
            ->method('isActive')
356
            ->will($this->returnValue(true));
357
        $filter2->expects($this->any())
358
            ->method('getRenderSettings')
359
            ->will($this->returnValue(['foo2', ['bar2' => 'baz2']]));
360
361
        $this->datagrid->addFilter($filter2);
362
363
        $this->datagrid->buildPager();
364
365
        $this->assertSame(['foo' => null, 'bar' => null], $this->datagrid->getValues());
366
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
367
        $this->assertSame(['bar1' => 'baz1'], $this->formBuilder->get('fooFormName')->getOptions());
368
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('barFormName'));
369
        $this->assertSame(['bar2' => 'baz2'], $this->formBuilder->get('barFormName')->getOptions());
370
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
371
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
372
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
373
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
374
    }
375
376
    public function testBuildPagerWithException()
377
    {
378
        $this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
379
        $this->expectExceptionMessage('Expected argument of type "Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface", "array" given');
380
381
        $filter = $this->createMock(FilterInterface::class);
382
        $filter->expects($this->once())
383
            ->method('getName')
384
            ->will($this->returnValue('foo'));
385
        $filter->expects($this->any())
386
            ->method('isActive')
387
            ->will($this->returnValue(false));
388
        $filter->expects($this->any())
389
            ->method('getRenderSettings')
390
            ->will($this->returnValue(['foo', ['bar' => 'baz']]));
391
392
        $this->datagrid->addFilter($filter);
393
394
        $this->datagrid->setValue('_sort_by', 'foo', 'baz');
395
396
        $this->datagrid->buildPager();
397
    }
398
399
    public function testBuildPagerWithSortBy()
400
    {
401
        $sortBy = $this->createMock(FieldDescriptionInterface::class);
402
        $sortBy->expects($this->once())
403
            ->method('isSortable')
404
            ->will($this->returnValue(true));
405
406
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
407
            ->method('setMaxPerPage')
408
            ->with($this->equalTo('25'))
409
            ->will($this->returnValue(null));
410
411
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
412
            ->method('setPage')
413
            ->with($this->equalTo('1'))
414
            ->will($this->returnValue(null));
415
416
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, ['_sort_by' => $sortBy]);
417
418
        $filter = $this->createMock(FilterInterface::class);
419
        $filter->expects($this->once())
420
            ->method('getName')
421
            ->will($this->returnValue('foo'));
422
        $filter->expects($this->any())
423
            ->method('getFormName')
424
            ->will($this->returnValue('fooFormName'));
425
        $filter->expects($this->any())
426
            ->method('isActive')
427
            ->will($this->returnValue(false));
428
        $filter->expects($this->any())
429
            ->method('getRenderSettings')
430
            ->will($this->returnValue(['foo', ['bar' => 'baz']]));
431
432
        $this->datagrid->addFilter($filter);
433
434
        $this->datagrid->buildPager();
435
436
        $this->assertSame(['_sort_by' => $sortBy, 'foo' => null], $this->datagrid->getValues());
437
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
438
        $this->assertSame(['bar' => 'baz'], $this->formBuilder->get('fooFormName')->getOptions());
439
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
440
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
441
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
442
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
443
    }
444
445
    /**
446
     * @dataProvider getBuildPagerWithPageTests
447
     */
448
    public function testBuildPagerWithPage($page, $perPage)
449
    {
450
        $sortBy = $this->createMock(FieldDescriptionInterface::class);
451
        $sortBy->expects($this->once())
452
            ->method('isSortable')
453
            ->will($this->returnValue(true));
454
455
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
456
            ->method('setMaxPerPage')
457
            ->with($this->equalTo('50'))
458
            ->will($this->returnValue(null));
459
460
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
461
            ->method('setPage')
462
            ->with($this->equalTo('3'))
463
            ->will($this->returnValue(null));
464
465
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, ['_sort_by' => $sortBy, '_page' => $page, '_per_page' => $perPage]);
466
467
        $filter = $this->createMock(FilterInterface::class);
468
        $filter->expects($this->once())
469
            ->method('getName')
470
            ->will($this->returnValue('foo'));
471
        $filter->expects($this->any())
472
            ->method('getFormName')
473
            ->will($this->returnValue('fooFormName'));
474
        $filter->expects($this->any())
475
            ->method('isActive')
476
            ->will($this->returnValue(false));
477
        $filter->expects($this->any())
478
            ->method('getRenderSettings')
479
            ->will($this->returnValue(['foo', ['bar' => 'baz']]));
480
481
        $this->datagrid->addFilter($filter);
482
483
        $this->datagrid->buildPager();
484
485
        $this->assertSame([
486
            '_sort_by' => $sortBy,
487
            '_page' => $page,
488
            '_per_page' => $perPage,
489
            'foo' => null,
490
        ], $this->datagrid->getValues());
491
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
492
        $this->assertSame(['bar' => 'baz'], $this->formBuilder->get('fooFormName')->getOptions());
493
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
494
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
495
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
496
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
497
    }
498
499
    public function getBuildPagerWithPageTests()
500
    {
501
        // tests for php 5.3, because isset functionality was changed since php 5.4
502
        return [
503
            [3, 50],
504
            ['3', '50'],
505
            [3, '50'],
506
            ['3', 50],
507
        ];
508
    }
509
510
    /**
511
     * @dataProvider getBuildPagerWithPage2Tests
512
     */
513
    public function testBuildPagerWithPage2($page, $perPage)
514
    {
515
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
516
            ->method('setMaxPerPage')
517
            ->with($this->equalTo('50'))
518
            ->will($this->returnValue(null));
519
520
        $this->pager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...atagrid\PagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
521
            ->method('setPage')
522
            ->with($this->equalTo('3'))
523
            ->will($this->returnValue(null));
524
525
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, []);
526
        $this->datagrid->setValue('_per_page', null, $perPage);
527
        $this->datagrid->setValue('_page', null, $page);
528
529
        $this->datagrid->buildPager();
530
531
        $this->assertSame([
532
            '_per_page' => ['type' => null, 'value' => $perPage],
533
            '_page' => ['type' => null, 'value' => $page],
534
        ], $this->datagrid->getValues());
535
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
536
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
537
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
538
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
539
    }
540
541
    public function getBuildPagerWithPage2Tests()
542
    {
543
        // tests for php 5.3, because isset functionality was changed since php 5.4
544
        return [
545
            [3, 50],
546
            ['3', '50'],
547
            [3, '50'],
548
            ['3', 50],
549
        ];
550
    }
551
}
552