Completed
Push — master ( d8d13a...e3b085 )
by
unknown
14:53
created

tests/Filter/CallbackFilterTest.php (4 issues)

Severity

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\DoctrineMongoDBAdminBundle\Tests\Filter;
15
16
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
17
use Sonata\DoctrineMongoDBAdminBundle\Filter\CallbackFilter;
18
19
class CallbackFilterTest extends FilterWithQueryBuilderTest
20
{
21
    public function testFilterClosureEmpty(): void
22
    {
23
        $builder = new ProxyQuery($this->getQueryBuilder());
24
25
        $filter = new CallbackFilter();
26
        $filter->initialize('field_name', [
27
            'callback' => function ($builder, $alias, $field, $value) {
28
                return true;
29
            },
30
        ]);
31
32
        $filter->filter($builder, 'alias', 'field', false);
33
        $filter->filter($builder, 'alias', 'field', 'scalarValue');
34
        $filter->filter($builder, 'alias', 'field', ['value' => '']);
35
36
        $this->assertFalse($filter->isActive());
37
    }
38
39
    public function testFilterClosureNotEmpty(): void
40
    {
41
        $builder = new ProxyQuery($this->getQueryBuilder());
42
43
        $filter = new CallbackFilter();
44
        $filter->initialize('field_name', [
45
            'callback' => function ($builder, $alias, $field, $value) {
46
                return true;
47
            },
48
        ]);
49
50
        $filter->filter($builder, 'alias', 'field', ['value' => 'myValue']);
51
52
        $this->assertTrue($filter->isActive());
53
    }
54
55
    public function testFilterMethodEmpty(): void
56
    {
57
        $builder = new ProxyQuery($this->getQueryBuilder());
58
59
        $filter = new CallbackFilter();
60
        $filter->initialize('field_name', [
61
            'callback' => [$this, 'customCallback'],
62
        ]);
63
64
        $filter->filter($builder, 'alias', 'field', false);
65
        $filter->filter($builder, 'alias', 'field', 'scalarValue');
66
        $filter->filter($builder, 'alias', 'field', ['value' => '']);
67
68
        $this->assertFalse($filter->isActive());
69
    }
70
71
    public function testFilterMethodNotEmpty(): void
72
    {
73
        $builder = new ProxyQuery($this->getQueryBuilder());
74
75
        $filter = new CallbackFilter();
76
        $filter->initialize('field_name', [
77
            'callback' => [$this, 'customCallback'],
78
        ]);
79
80
        $filter->filter($builder, 'alias', 'field', ['value' => 'myValue']);
81
82
        $this->assertTrue($filter->isActive());
83
    }
84
85
    public function customCallback($builder, $alias, $field, $value)
0 ignored issues
show
The parameter $builder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $alias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        return true;
88
    }
89
90
    public function testFilterException(): void
91
    {
92
        $this->expectException(\RuntimeException::class);
93
94
        $builder = new ProxyQuery($this->getQueryBuilder());
95
96
        $filter = new CallbackFilter();
97
        $filter->initialize('field_name', []);
98
99
        $filter->filter($builder, 'alias', 'field', 'myValue');
100
    }
101
}
102