Completed
Pull Request — 3.x (#6308)
by Vincent
02:59
created

SearchHandlerTest::getPool()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 1
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\Search;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Datagrid\DatagridInterface;
19
use Sonata\AdminBundle\Datagrid\PagerInterface;
20
use Sonata\AdminBundle\Filter\FilterInterface;
21
use Sonata\AdminBundle\Search\SearchHandler;
22
23
class SearchHandlerTest extends TestCase
24
{
25
    public function testBuildPagerWithNoGlobalSearchField(): void
26
    {
27
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
28
        $filter->expects($this->once())->method('getOption')->willReturn(false);
29
        $filter->expects($this->never())->method('setOption');
30
31
        $datagrid = $this->getMockForAbstractClass(DatagridInterface::class);
32
        $datagrid->expects($this->once())->method('getFilters')->willReturn([$filter]);
33
34
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
35
        $admin->expects($this->once())->method('getDatagrid')->willReturn($datagrid);
36
37
        $handler = new SearchHandler(true);
38
        $this->assertFalse($handler->search($admin, 'myservice'));
39
    }
40
41
    /**
42
     * @dataProvider buildPagerWithGlobalSearchFieldProvider
43
     */
44
    public function testBuildPagerWithGlobalSearchField(bool $caseSensitive): void
45
    {
46
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
47
        $filter->expects($this->once())->method('getOption')->willReturn(true);
48
        $filter->expects($this->once())->method('setOption')->with('case_sensitive', $caseSensitive);
49
50
        $pager = $this->getMockForAbstractClass(PagerInterface::class);
51
        $pager->expects($this->once())->method('setPage');
52
        $pager->expects($this->once())->method('setMaxPerPage');
53
54
        $datagrid = $this->getMockForAbstractClass(DatagridInterface::class);
55
        $datagrid->expects($this->once())->method('getFilters')->willReturn([$filter]);
56
        $datagrid->expects($this->once())->method('setValue');
57
        $datagrid->expects($this->once())->method('getPager')->willReturn($pager);
58
59
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
60
        $admin->expects($this->once())->method('getDatagrid')->willReturn($datagrid);
61
62
        $handler = new SearchHandler($caseSensitive);
63
        $this->assertInstanceOf(PagerInterface::class, $handler->search($admin, 'myservice'));
64
    }
65
66
    public function buildPagerWithGlobalSearchFieldProvider(): array
67
    {
68
        return [
69
            [true],
70
            [false],
71
        ];
72
    }
73
}
74