Completed
Push — 3.x ( a394da...8e30a0 )
by Oskar
05:04
created

buildPagerWithGlobalSearchField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
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\Admin\Pool;
19
use Sonata\AdminBundle\Datagrid\DatagridInterface;
20
use Sonata\AdminBundle\Datagrid\PagerInterface;
21
use Sonata\AdminBundle\Filter\FilterInterface;
22
use Sonata\AdminBundle\Search\SearchHandler;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
25
26
class SearchHandlerTest extends TestCase
27
{
28
    /**
29
     * @param AdminInterface $admin
30
     *
31
     * @return Pool
32
     */
33
    public function getPool(AdminInterface $admin = null)
34
    {
35
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
36
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($admin) {
37
            if ('fake' == $id) {
38
                throw new ServiceNotFoundException('Fake service does not exist');
39
            }
40
41
            return $admin;
42
        }));
43
44
        return new Pool($container, 'title', 'logo', ['asd']);
45
    }
46
47
    public function testBuildPagerWithNoGlobalSearchField()
48
    {
49
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
50
        $filter->expects($this->once())->method('getOption')->will($this->returnValue(false));
51
        $filter->expects($this->never())->method('setOption');
52
53
        $datagrid = $this->getMockForAbstractClass(DatagridInterface::class);
54
        $datagrid->expects($this->once())->method('getFilters')->will($this->returnValue([$filter]));
55
56
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
57
        $admin->expects($this->once())->method('getDatagrid')->will($this->returnValue($datagrid));
58
59
        $handler = new SearchHandler($this->getPool($admin));
60
        $this->assertFalse($handler->search($admin, 'myservice'));
61
    }
62
63
    /**
64
     * @test
65
     *
66
     * @dataProvider buildPagerWithGlobalSearchFieldProvider
67
     */
68
    public function buildPagerWithGlobalSearchField($caseSensitive)
69
    {
70
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
71
        $filter->expects($this->once())->method('getOption')->will($this->returnValue(true));
72
        $filter->expects($this->once())->method('setOption')->with('case_sensitive', $caseSensitive);
73
74
        $pager = $this->getMockForAbstractClass(PagerInterface::class);
75
        $pager->expects($this->once())->method('setPage');
76
        $pager->expects($this->once())->method('setMaxPerPage');
77
78
        $datagrid = $this->getMockForAbstractClass(DatagridInterface::class);
79
        $datagrid->expects($this->once())->method('getFilters')->will($this->returnValue([$filter]));
80
        $datagrid->expects($this->once())->method('setValue');
81
        $datagrid->expects($this->once())->method('getPager')->will($this->returnValue($pager));
82
83
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
84
        $admin->expects($this->once())->method('getDatagrid')->will($this->returnValue($datagrid));
85
86
        $handler = new SearchHandler($this->getPool($admin), $caseSensitive);
87
        $this->assertInstanceOf(PagerInterface::class, $handler->search($admin, 'myservice'));
88
    }
89
90
    public function buildPagerWithGlobalSearchFieldProvider()
91
    {
92
        return [
93
            [true],
94
            [false],
95
        ];
96
    }
97
}
98