CallbackFilterTest::testFilterMethodEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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' => static function ($builder, $alias, $field, $value) {
0 ignored issues
show
Unused Code introduced by
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...
Unused Code introduced by
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...
Unused Code introduced by
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...
Unused Code introduced by
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...
28
                return true;
29
            },
30
        ]);
31
32
        $filter->filter($builder, 'alias', 'field', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

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...
33
        $filter->filter($builder, 'alias', 'field', 'scalarValue');
34
        $filter->filter($builder, 'alias', 'field', ['value' => '']);
0 ignored issues
show
Documentation introduced by
array('value' => '') is of type array<string,string,{"value":"string"}>, but the function expects a string.

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...
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' => static function ($builder, $alias, $field, $value) {
0 ignored issues
show
Unused Code introduced by
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...
Unused Code introduced by
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...
Unused Code introduced by
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...
Unused Code introduced by
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...
46
                return true;
47
            },
48
        ]);
49
50
        $filter->filter($builder, 'alias', 'field', ['value' => 'myValue']);
0 ignored issues
show
Documentation introduced by
array('value' => 'myValue') is of type array<string,string,{"value":"string"}>, but the function expects a string.

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...
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);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

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...
65
        $filter->filter($builder, 'alias', 'field', 'scalarValue');
66
        $filter->filter($builder, 'alias', 'field', ['value' => '']);
0 ignored issues
show
Documentation introduced by
array('value' => '') is of type array<string,string,{"value":"string"}>, but the function expects a string.

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...
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']);
0 ignored issues
show
Documentation introduced by
array('value' => 'myValue') is of type array<string,string,{"value":"string"}>, but the function expects a string.

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...
81
82
        $this->assertTrue($filter->isActive());
83
    }
84
85
    public function customCallback($builder, $alias, $field, $value)
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