Completed
Push — master ( 46c3a0...a94e83 )
by Grégoire
13s queued 11s
created

tests/Search/SearchHandlerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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')->willReturnCallback(static 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(): void
48
    {
49
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
50
        $filter->expects($this->once())->method('getOption')->willReturn(false);
51
        $filter->expects($this->never())->method('setOption');
52
53
        $datagrid = $this->getMockForAbstractClass(DatagridInterface::class);
54
        $datagrid->expects($this->once())->method('getFilters')->willReturn([$filter]);
55
56
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
57
        $admin->expects($this->once())->method('getDatagrid')->willReturn($datagrid);
58
59
        $handler = new SearchHandler($this->getPool($admin));
0 ignored issues
show
$admin is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Sonata\Admin...e\Admin\AdminInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
        $this->assertFalse($handler->search($admin, 'myservice'));
0 ignored issues
show
$admin is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundle\Admin\AdminInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
    }
62
63
    /**
64
     * @test
65
     *
66
     * @dataProvider buildPagerWithGlobalSearchFieldProvider
67
     */
68
    public function buildPagerWithGlobalSearchField($caseSensitive): void
69
    {
70
        $filter = $this->getMockForAbstractClass(FilterInterface::class);
71
        $filter->expects($this->once())->method('getOption')->willReturn(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')->willReturn([$filter]);
80
        $datagrid->expects($this->once())->method('setValue');
81
        $datagrid->expects($this->once())->method('getPager')->willReturn($pager);
82
83
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
84
        $admin->expects($this->once())->method('getDatagrid')->willReturn($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