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\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\BooleanFilter; |
19
|
|
|
use Sonata\Form\Type\BooleanType; |
20
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
21
|
|
|
|
22
|
|
|
class BooleanFilterTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testRenderSettings(): void |
25
|
|
|
{ |
26
|
|
|
$filter = new BooleanFilter(); |
27
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
28
|
|
|
$options = $filter->getRenderSettings()[1]; |
29
|
|
|
|
30
|
|
|
$this->assertSame(HiddenType::class, $options['operator_type']); |
31
|
|
|
$this->assertSame([], $options['operator_options']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testFilterEmpty(): void |
35
|
|
|
{ |
36
|
|
|
$filter = new BooleanFilter(); |
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', ''); |
|
|
|
|
43
|
|
|
$filter->filter($builder, 'alias', 'field', 'test'); |
|
|
|
|
44
|
|
|
$filter->filter($builder, 'alias', 'field', false); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$filter->filter($builder, 'alias', 'field', []); |
47
|
|
|
$filter->filter($builder, 'alias', 'field', [null, 'test']); |
48
|
|
|
|
49
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
50
|
|
|
$this->assertFalse($filter->isActive()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFilterNo(): void |
54
|
|
|
{ |
55
|
|
|
$filter = new BooleanFilter(); |
56
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
57
|
|
|
|
58
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => BooleanType::TYPE_NO]); |
61
|
|
|
|
62
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
63
|
|
|
$this->assertSame(['field_name_0' => 0], $builder->parameters); |
|
|
|
|
64
|
|
|
$this->assertTrue($filter->isActive()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testFilterYes(): void |
68
|
|
|
{ |
69
|
|
|
$filter = new BooleanFilter(); |
70
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
71
|
|
|
|
72
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => BooleanType::TYPE_YES]); |
75
|
|
|
|
76
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
77
|
|
|
$this->assertSame(['field_name_0' => 1], $builder->parameters); |
|
|
|
|
78
|
|
|
$this->assertTrue($filter->isActive()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testFilterArray(): void |
82
|
|
|
{ |
83
|
|
|
$filter = new BooleanFilter(); |
84
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
85
|
|
|
|
86
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => [BooleanType::TYPE_NO]]); |
89
|
|
|
|
90
|
|
|
$this->assertSame(['in_alias.field', 'alias.field IN ("0")'], $builder->query); |
|
|
|
|
91
|
|
|
$this->assertTrue($filter->isActive()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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: