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\DoctrineORMAdminBundle\Tests\Filter; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType; |
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter; |
20
|
|
|
|
21
|
|
|
class ChoiceFilterTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testRenderSettings(): void |
24
|
|
|
{ |
25
|
|
|
$filter = new ChoiceFilter(); |
26
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
27
|
|
|
$options = $filter->getRenderSettings()[1]; |
28
|
|
|
|
29
|
|
|
$this->assertSame(EqualOperatorType::class, $options['operator_type']); |
30
|
|
|
$this->assertSame([], $options['operator_options']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testFilterEmpty(): void |
34
|
|
|
{ |
35
|
|
|
$filter = new ChoiceFilter(); |
36
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
37
|
|
|
|
38
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
|
|
|
|
41
|
|
|
$filter->filter($builder, 'alias', 'field', []); |
42
|
|
|
|
43
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
44
|
|
|
$this->assertFalse($filter->isActive()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testFilterArray(): void |
48
|
|
|
{ |
49
|
|
|
$filter = new ChoiceFilter(); |
50
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
51
|
|
|
|
52
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', '2']]); |
55
|
|
|
|
56
|
|
|
$this->assertSame(['alias.field IN :field_name_0'], $builder->query); |
|
|
|
|
57
|
|
|
$this->assertSame(['field_name_0' => ['1', '2']], $builder->parameters); |
|
|
|
|
58
|
|
|
$this->assertTrue($filter->isActive()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testFilterArrayWithNullValue(): void |
62
|
|
|
{ |
63
|
|
|
$filter = new ChoiceFilter(); |
64
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
65
|
|
|
|
66
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', null]]); |
69
|
|
|
|
70
|
|
|
$this->assertSame(['alias.field IN :field_name_0 OR alias.field IS NULL'], $builder->query); |
|
|
|
|
71
|
|
|
$this->assertSame(['field_name_0' => ['1']], $builder->parameters); |
|
|
|
|
72
|
|
|
$this->assertTrue($filter->isActive()); |
73
|
|
|
|
74
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => ['1', null]]); |
77
|
|
|
|
78
|
|
|
$this->assertSame(['alias.field IS NOT NULL AND alias.field NOT IN :field_name_0'], $builder->query); |
|
|
|
|
79
|
|
|
$this->assertSame(['field_name_0' => ['1']], $builder->parameters); |
|
|
|
|
80
|
|
|
$this->assertTrue($filter->isActive()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testFilterScalar(): void |
84
|
|
|
{ |
85
|
|
|
$filter = new ChoiceFilter(); |
86
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
87
|
|
|
|
88
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => '1']); |
91
|
|
|
|
92
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
93
|
|
|
$this->assertSame(['field_name_0' => '1'], $builder->parameters); |
|
|
|
|
94
|
|
|
$this->assertTrue($filter->isActive()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testFilterNull(): void |
98
|
|
|
{ |
99
|
|
|
$filter = new ChoiceFilter(); |
100
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
101
|
|
|
|
102
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => null]); |
105
|
|
|
|
106
|
|
|
$this->assertSame(['alias.field IS NULL'], $builder->query); |
|
|
|
|
107
|
|
|
$this->assertSame([], $builder->parameters); |
|
|
|
|
108
|
|
|
$this->assertTrue($filter->isActive()); |
109
|
|
|
|
110
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => null]); |
113
|
|
|
|
114
|
|
|
$this->assertSame(['alias.field IS NOT NULL'], $builder->query); |
|
|
|
|
115
|
|
|
$this->assertSame([], $builder->parameters); |
|
|
|
|
116
|
|
|
$this->assertTrue($filter->isActive()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testFilterZero(): void |
120
|
|
|
{ |
121
|
|
|
$filter = new ChoiceFilter(); |
122
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
123
|
|
|
|
124
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => 0]); |
127
|
|
|
|
128
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
129
|
|
|
$this->assertSame(['field_name_0' => 0], $builder->parameters); |
|
|
|
|
130
|
|
|
$this->assertTrue($filter->isActive()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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: