Completed
Push — 3.x ( c9d427...4f4acc )
by Vincent
03:01
created

SearchHandlerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildPagerWithNoGlobalSearchField() 0 15 1
A testBuildPagerWithGlobalSearchField() 0 21 1
A buildPagerWithGlobalSearchFieldProvider() 0 7 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