Completed
Push — 3.x ( df0d37...0e617d )
by Grégoire
02:54
created

DatagridTest::getBuildPagerWithPageTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Datagrid;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Datagrid\Datagrid;
20
use Sonata\AdminBundle\Datagrid\PagerInterface;
21
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
22
use Sonata\AdminBundle\Filter\FilterInterface;
23
use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\TestEntity;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\Form\Form;
26
use Symfony\Component\Form\FormBuilder;
27
use Symfony\Component\Form\FormFactoryInterface;
28
29
/**
30
 * @author Andrej Hudec <[email protected]>
31
 */
32
class DatagridTest extends TestCase
33
{
34
    /**
35
     * @var Datagrid
36
     */
37
    private $datagrid;
38
39
    /**
40
     * @var PagerInterface
41
     */
42
    private $pager;
43
44
    /**
45
     * @var ProxyQueryInterface
46
     */
47
    private $query;
48
49
    /**
50
     * @var FieldDescriptionCollection
51
     */
52
    private $columns;
53
54
    /**
55
     * @var FormBuilder
56
     */
57
    private $formBuilder;
58
59
    /**
60
     * @var array
61
     */
62
    private $formTypes;
63
64
    protected function setUp(): void
65
    {
66
        $this->query = $this->createMock(ProxyQueryInterface::class);
67
        $this->columns = new FieldDescriptionCollection();
68
        $this->pager = $this->createMock(PagerInterface::class);
69
70
        $this->formTypes = [];
71
72
        $this->formBuilder = $this->getMockBuilder(FormBuilder::class)
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        $this->formBuilder
77
            ->method('get')
78
            ->willReturnCallback(function (string $name): FormBuilder {
79
                if (isset($this->formTypes[$name])) {
80
                    return $this->formTypes[$name];
81
                }
82
            });
83
84
        $this->formBuilder
85
            ->method('add')
86
            ->willReturnCallback(function (?string $name, string $type, array $options): void {
87
                $this->formTypes[$name] = new FormBuilder(
88
                    $name,
89
                    TestEntity::class,
90
                    $this->createMock(EventDispatcherInterface::class),
91
                    $this->createMock(FormFactoryInterface::class),
92
                    $options
93
                );
94
            });
95
96
        $this->formBuilder
97
            ->method('getForm')
98
            ->willReturnCallback(function () {
99
                return $this->getMockBuilder(Form::class)
100
                    ->disableOriginalConstructor()
101
                    ->getMock();
102
            });
103
104
        $values = [];
105
106
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, $values);
107
    }
108
109
    public function testGetPager(): void
110
    {
111
        $this->assertSame($this->pager, $this->datagrid->getPager());
112
    }
113
114
    /**
115
     * @group legacy
116
     *
117
     * @expectedDeprecation Passing a nonexistent filter name as argument 1 to Sonata\AdminBundle\Datagrid\Datagrid::getFilter() is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0.
118
     */
119
    public function testFilter(): void
120
    {
121
        $this->assertFalse($this->datagrid->hasFilter('foo'));
122
123
        $filter = $this->createMock(FilterInterface::class);
124
        $filter->expects($this->once())
125
            ->method('getName')
126
            ->willReturn('foo');
127
128
        $this->datagrid->addFilter($filter);
129
130
        $this->assertTrue($this->datagrid->hasFilter('foo'));
131
        $this->assertFalse($this->datagrid->hasFilter('nonexistent'));
132
        $this->assertSame($filter, $this->datagrid->getFilter('foo'));
133
134
        $this->datagrid->removeFilter('foo');
135
136
        $this->assertFalse($this->datagrid->hasFilter('foo'));
137
        $this->assertNull($this->datagrid->getFilter('foo'));
138
        // NEXT_MAJOR: Remove previous assertion, the "@group" and "@expectedDeprecation" annotations and uncomment the following lines
139
        // $this->expectException(\InvalidArgumentException::class);
140
        // $this->expectExceptionMessage('Filter named "foo" doesn\'t exist.');
141
        //
142
        // $this->datagrid->getFilter('foo');
143
    }
144
145
    public function testGetFilters(): void
146
    {
147
        $this->assertSame([], $this->datagrid->getFilters());
148
149
        $filter1 = $this->createMock(FilterInterface::class);
150
        $filter1->expects($this->once())
151
            ->method('getName')
152
            ->willReturn('foo');
153
154
        $filter2 = $this->createMock(FilterInterface::class);
155
        $filter2->expects($this->once())
156
            ->method('getName')
157
            ->willReturn('bar');
158
159
        $filter3 = $this->createMock(FilterInterface::class);
160
        $filter3->expects($this->once())
161
            ->method('getName')
162
            ->willReturn('baz');
163
164
        $this->datagrid->addFilter($filter1);
165
        $this->datagrid->addFilter($filter2);
166
        $this->datagrid->addFilter($filter3);
167
168
        $this->assertSame(['foo' => $filter1, 'bar' => $filter2, 'baz' => $filter3], $this->datagrid->getFilters());
169
170
        $this->datagrid->removeFilter('bar');
171
172
        $this->assertSame(['foo' => $filter1, 'baz' => $filter3], $this->datagrid->getFilters());
173
    }
174
175
    public function testReorderFilters(): void
176
    {
177
        $this->assertSame([], $this->datagrid->getFilters());
178
179
        $filter1 = $this->createMock(FilterInterface::class);
180
        $filter1->expects($this->once())
181
            ->method('getName')
182
            ->willReturn('foo');
183
184
        $filter2 = $this->createMock(FilterInterface::class);
185
        $filter2->expects($this->once())
186
            ->method('getName')
187
            ->willReturn('bar');
188
189
        $filter3 = $this->createMock(FilterInterface::class);
190
        $filter3->expects($this->once())
191
            ->method('getName')
192
            ->willReturn('baz');
193
194
        $this->datagrid->addFilter($filter1);
195
        $this->datagrid->addFilter($filter2);
196
        $this->datagrid->addFilter($filter3);
197
198
        $this->assertSame(['foo' => $filter1, 'bar' => $filter2, 'baz' => $filter3], $this->datagrid->getFilters());
199
        $this->assertSame(['foo', 'bar', 'baz'], array_keys($this->datagrid->getFilters()));
200
201
        $this->datagrid->reorderFilters(['bar', 'baz', 'foo']);
202
203
        $this->assertSame(['bar' => $filter2, 'baz' => $filter3, 'foo' => $filter1], $this->datagrid->getFilters());
204
        $this->assertSame(['bar', 'baz', 'foo'], array_keys($this->datagrid->getFilters()));
205
    }
206
207
    public function testGetValues(): void
208
    {
209
        $this->assertSame([], $this->datagrid->getValues());
210
211
        $this->datagrid->setValue('foo', 'bar', 'baz');
212
213
        $this->assertSame(['foo' => ['type' => 'bar', 'value' => 'baz']], $this->datagrid->getValues());
214
    }
215
216
    public function testGetColumns(): void
217
    {
218
        $this->assertSame($this->columns, $this->datagrid->getColumns());
219
    }
220
221
    public function testGetQuery(): void
222
    {
223
        $this->assertSame($this->query, $this->datagrid->getQuery());
224
    }
225
226
    public function testHasActiveFilters(): void
227
    {
228
        $this->assertFalse($this->datagrid->hasActiveFilters());
229
230
        $filter1 = $this->createMock(FilterInterface::class);
231
        $filter1->expects($this->once())
232
            ->method('getName')
233
            ->willReturn('foo');
234
        $filter1
235
            ->method('isActive')
236
            ->willReturn(false);
237
238
        $this->datagrid->addFilter($filter1);
239
240
        $this->assertFalse($this->datagrid->hasActiveFilters());
241
242
        $filter2 = $this->createMock(FilterInterface::class);
243
        $filter2->expects($this->once())
244
            ->method('getName')
245
            ->willReturn('bar');
246
        $filter2
247
            ->method('isActive')
248
            ->willReturn(true);
249
250
        $this->datagrid->addFilter($filter2);
251
252
        $this->assertTrue($this->datagrid->hasActiveFilters());
253
    }
254
255
    public function testHasDisplayableFilters(): void
256
    {
257
        $this->assertFalse($this->datagrid->hasDisplayableFilters());
258
    }
259
260
    public function testHasDisplayableFiltersNotActive(): void
261
    {
262
        $filter = $this->createMock(FilterInterface::class);
263
        $filter->expects($this->once())
264
            ->method('getName')
265
            ->willReturn('foo');
266
        $filter
267
            ->method('getOption')
268
            ->willReturn(false);
269
        $filter
270
            ->method('isActive')
271
            ->willReturn(false);
272
273
        $this->datagrid->addFilter($filter);
274
275
        $this->assertFalse($this->datagrid->hasDisplayableFilters());
276
    }
277
278
    public function testHasDisplayableFiltersActive(): void
279
    {
280
        $filter = $this->createMock(FilterInterface::class);
281
        $filter->expects($this->once())
282
            ->method('getName')
283
            ->willReturn('bar');
284
        $filter
285
            ->method('getOption')
286
            ->willReturn(true);
287
        $filter
288
            ->method('isActive')
289
            ->willReturn(true);
290
291
        $this->datagrid->addFilter($filter);
292
293
        $this->assertTrue($this->datagrid->hasDisplayableFilters());
294
    }
295
296
    public function testHasDisplayableFiltersAlwaysShow(): void
297
    {
298
        $filter = $this->createMock(FilterInterface::class);
299
        $filter->expects($this->once())
300
            ->method('getName')
301
            ->willReturn('bar');
302
        $filter
303
            ->method('getOption')
304
            ->with($this->equalTo('show_filter'))
305
            ->willReturn(true);
306
        $filter
307
            ->method('isActive')
308
            ->willReturn(false);
309
310
        $this->datagrid->addFilter($filter);
311
312
        $this->assertTrue($this->datagrid->hasDisplayableFilters());
313
    }
314
315
    public function testGetForm(): void
316
    {
317
        $this->assertInstanceOf(Form::class, $this->datagrid->getForm());
318
    }
319
320
    public function testGetResults(): void
321
    {
322
        $this->assertNull($this->datagrid->getResults());
323
324
        $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...
325
            ->method('getResults')
326
            ->willReturn(['foo', 'bar']);
327
328
        $this->assertSame(['foo', 'bar'], $this->datagrid->getResults());
329
    }
330
331
    public function testEmptyResults(): void
332
    {
333
        $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...
334
            ->method('getResults')
335
            ->willReturn([]);
336
337
        $this->assertSame([], $this->datagrid->getResults());
338
        $this->assertSame([], $this->datagrid->getResults());
339
    }
340
341
    public function testBuildPager(): void
342
    {
343
        $filter1 = $this->createMock(FilterInterface::class);
344
        $filter1->expects($this->once())
345
            ->method('getName')
346
            ->willReturn('foo');
347
        $filter1
348
            ->method('getFormName')
349
            ->willReturn('fooFormName');
350
        $filter1
351
            ->method('isActive')
352
            ->willReturn(false);
353
        $filter1
354
            ->method('getRenderSettings')
355
            ->willReturn(['foo1', ['bar1' => 'baz1']]);
356
357
        $this->datagrid->addFilter($filter1);
358
359
        $filter2 = $this->createMock(FilterInterface::class);
360
        $filter2->expects($this->once())
361
            ->method('getName')
362
            ->willReturn('bar');
363
        $filter2
364
            ->method('getFormName')
365
            ->willReturn('barFormName');
366
        $filter2
367
            ->method('isActive')
368
            ->willReturn(true);
369
        $filter2
370
            ->method('getRenderSettings')
371
            ->willReturn(['foo2', ['bar2' => 'baz2']]);
372
373
        $this->datagrid->addFilter($filter2);
374
375
        $this->datagrid->buildPager();
376
377
        $this->assertSame(['foo' => null, 'bar' => null], $this->datagrid->getValues());
378
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
379
        $this->assertSame(['bar1' => 'baz1'], $this->formBuilder->get('fooFormName')->getOptions());
380
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('barFormName'));
381
        $this->assertSame(['bar2' => 'baz2'], $this->formBuilder->get('barFormName')->getOptions());
382
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
383
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
384
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
385
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
386
    }
387
388
    public function testBuildPagerWithException(): void
389
    {
390
        $this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
391
        $this->expectExceptionMessage('Expected argument of type "Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface", "array" given');
392
393
        $filter = $this->createMock(FilterInterface::class);
394
        $filter->expects($this->once())
395
            ->method('getName')
396
            ->willReturn('foo');
397
        $filter
398
            ->method('isActive')
399
            ->willReturn(false);
400
        $filter
401
            ->method('getRenderSettings')
402
            ->willReturn(['foo', ['bar' => 'baz']]);
403
404
        $this->datagrid->addFilter($filter);
405
406
        $this->datagrid->setValue('_sort_by', 'foo', 'baz');
407
408
        $this->datagrid->buildPager();
409
    }
410
411
    public function testBuildPagerWithSortBy(): void
412
    {
413
        $sortBy = $this->createMock(FieldDescriptionInterface::class);
414
        $sortBy->expects($this->once())
415
            ->method('isSortable')
416
            ->willReturn(true);
417
418
        $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...
419
            ->method('setMaxPerPage')
420
            ->with($this->equalTo('25'))
421
            ->willReturn(null);
422
423
        $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...
424
            ->method('setPage')
425
            ->with($this->equalTo('1'))
426
            ->willReturn(null);
427
428
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, ['_sort_by' => $sortBy]);
429
430
        $filter = $this->createMock(FilterInterface::class);
431
        $filter->expects($this->once())
432
            ->method('getName')
433
            ->willReturn('foo');
434
        $filter
435
            ->method('getFormName')
436
            ->willReturn('fooFormName');
437
        $filter
438
            ->method('isActive')
439
            ->willReturn(false);
440
        $filter
441
            ->method('getRenderSettings')
442
            ->willReturn(['foo', ['bar' => 'baz']]);
443
444
        $this->datagrid->addFilter($filter);
445
446
        $this->datagrid->buildPager();
447
448
        $this->assertSame(['_sort_by' => $sortBy, 'foo' => null, '_sort_order' => 'ASC'], $this->datagrid->getValues());
449
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
450
        $this->assertSame(['bar' => 'baz'], $this->formBuilder->get('fooFormName')->getOptions());
451
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
452
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
453
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
454
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
455
    }
456
457
    /**
458
     * @dataProvider getBuildPagerWithPageTests
459
     */
460
    public function testBuildPagerWithPage($page, $perPage): void
461
    {
462
        $sortBy = $this->createMock(FieldDescriptionInterface::class);
463
        $sortBy->expects($this->once())
464
            ->method('isSortable')
465
            ->willReturn(true);
466
467
        $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...
468
            ->method('setMaxPerPage')
469
            ->with($this->equalTo('50'))
470
            ->willReturn(null);
471
472
        $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...
473
            ->method('setPage')
474
            ->with($this->equalTo('3'))
475
            ->willReturn(null);
476
477
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, ['_sort_by' => $sortBy, '_page' => $page, '_per_page' => $perPage]);
478
479
        $filter = $this->createMock(FilterInterface::class);
480
        $filter->expects($this->once())
481
            ->method('getName')
482
            ->willReturn('foo');
483
        $filter
484
            ->method('getFormName')
485
            ->willReturn('fooFormName');
486
        $filter
487
            ->method('isActive')
488
            ->willReturn(false);
489
        $filter
490
            ->method('getRenderSettings')
491
            ->willReturn(['foo', ['bar' => 'baz']]);
492
493
        $this->datagrid->addFilter($filter);
494
495
        $this->datagrid->buildPager();
496
497
        $this->assertSame([
498
            '_sort_by' => $sortBy,
499
            '_page' => $page,
500
            '_per_page' => $perPage,
501
            'foo' => null,
502
            '_sort_order' => 'ASC',
503
        ], $this->datagrid->getValues());
504
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('fooFormName'));
505
        $this->assertSame(['bar' => 'baz'], $this->formBuilder->get('fooFormName')->getOptions());
506
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
507
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
508
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
509
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
510
    }
511
512
    public function getBuildPagerWithPageTests(): array
513
    {
514
        // tests for php 5.3, because isset functionality was changed since php 5.4
515
        return [
516
            [3, 50],
517
            ['3', '50'],
518
            [3, '50'],
519
            ['3', 50],
520
            [3, ['type' => null, 'value' => 50]],
521
            [3, ['type' => null, 'value' => '50']],
522
        ];
523
    }
524
525
    /**
526
     * @dataProvider getBuildPagerWithPage2Tests
527
     */
528
    public function testBuildPagerWithPage2($page, $perPage): void
529
    {
530
        $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...
531
            ->method('setMaxPerPage')
532
            ->with($this->equalTo('50'))
533
            ->willReturn(null);
534
535
        $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...
536
            ->method('setPage')
537
            ->with($this->equalTo('3'))
538
            ->willReturn(null);
539
540
        $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, []);
541
        $this->datagrid->setValue('_per_page', null, $perPage);
542
        $this->datagrid->setValue('_page', null, $page);
543
544
        $this->datagrid->buildPager();
545
546
        $this->assertSame([
547
            '_per_page' => ['type' => null, 'value' => $perPage],
548
            '_page' => ['type' => null, 'value' => $page],
549
        ], $this->datagrid->getValues());
550
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_by'));
551
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_sort_order'));
552
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_page'));
553
        $this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
554
    }
555
556
    public function getBuildPagerWithPage2Tests(): array
557
    {
558
        // tests for php 5.3, because isset functionality was changed since php 5.4
559
        return [
560
            [3, 50],
561
            ['3', '50'],
562
            [3, '50'],
563
            ['3', 50],
564
        ];
565
    }
566
567
    public function testSortParameters(): void
568
    {
569
        $field1 = $this->createMock(FieldDescriptionInterface::class);
570
        $field1->method('getName')->willReturn('field1');
571
572
        $field2 = $this->createMock(FieldDescriptionInterface::class);
573
        $field2->method('getName')->willReturn('field2');
574
575
        $field3 = $this->createMock(FieldDescriptionInterface::class);
576
        $field3->method('getName')->willReturn('field3');
577
        $field3->method('getOption')->with('sortable')->willReturn('field3sortBy');
578
579
        $this->datagrid = new Datagrid(
580
            $this->query,
581
            $this->columns,
582
            $this->pager,
583
            $this->formBuilder,
584
            ['_sort_by' => $field1, '_sort_order' => 'ASC']
585
        );
586
587
        $parameters = $this->datagrid->getSortParameters($field1);
588
589
        $this->assertSame('DESC', $parameters['filter']['_sort_order']);
590
        $this->assertSame('field1', $parameters['filter']['_sort_by']);
591
592
        $parameters = $this->datagrid->getSortParameters($field2);
593
594
        $this->assertSame('ASC', $parameters['filter']['_sort_order']);
595
        $this->assertSame('field2', $parameters['filter']['_sort_by']);
596
597
        $parameters = $this->datagrid->getSortParameters($field3);
598
599
        $this->assertSame('ASC', $parameters['filter']['_sort_order']);
600
        $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']);
601
602
        $this->datagrid = new Datagrid(
603
            $this->query,
604
            $this->columns,
605
            $this->pager,
606
            $this->formBuilder,
607
            ['_sort_by' => $field3, '_sort_order' => 'ASC']
608
        );
609
610
        $parameters = $this->datagrid->getSortParameters($field3);
611
612
        $this->assertSame('DESC', $parameters['filter']['_sort_order']);
613
        $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']);
614
    }
615
616
    public function testGetPaginationParameters(): void
617
    {
618
        $field = $this->createMock(FieldDescriptionInterface::class);
619
620
        $this->datagrid = new Datagrid(
621
            $this->query,
622
            $this->columns,
623
            $this->pager,
624
            $this->formBuilder,
625
            ['_sort_by' => $field, '_sort_order' => 'ASC']
626
        );
627
628
        $field->expects($this->once())->method('getName')->willReturn($name = 'test');
629
630
        $result = $this->datagrid->getPaginationParameters($page = 5);
631
632
        $this->assertSame($page, $result['filter']['_page']);
633
        $this->assertSame($name, $result['filter']['_sort_by']);
634
    }
635
}
636