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