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\NumberOperatorType; |
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\NumberFilter; |
20
|
|
|
|
21
|
|
|
class NumberFilterTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testFilterEmpty(): void |
24
|
|
|
{ |
25
|
|
|
$filter = new NumberFilter(); |
26
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
27
|
|
|
|
28
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
|
|
|
|
31
|
|
|
$filter->filter($builder, 'alias', 'field', 'asds'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
34
|
|
|
$this->assertFalse($filter->isActive()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testFilterInvalidOperator(): void |
38
|
|
|
{ |
39
|
|
|
$filter = new NumberFilter(); |
40
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
41
|
|
|
|
42
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => 'foo']); |
45
|
|
|
|
46
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
47
|
|
|
$this->assertFalse($filter->isActive()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testFilter(): void |
51
|
|
|
{ |
52
|
|
|
$filter = new NumberFilter(); |
53
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
54
|
|
|
|
55
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => NumberOperatorType::TYPE_EQUAL, 'value' => 42]); |
58
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => NumberOperatorType::TYPE_GREATER_EQUAL, 'value' => 42]); |
59
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => NumberOperatorType::TYPE_GREATER_THAN, 'value' => 42]); |
60
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => NumberOperatorType::TYPE_LESS_EQUAL, 'value' => 42]); |
61
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => NumberOperatorType::TYPE_LESS_THAN, 'value' => 42]); |
62
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 42]); |
63
|
|
|
|
64
|
|
|
$expected = [ |
65
|
|
|
'alias.field = :field_name_0', |
66
|
|
|
'alias.field >= :field_name_1', |
67
|
|
|
'alias.field > :field_name_2', |
68
|
|
|
'alias.field <= :field_name_3', |
69
|
|
|
'alias.field < :field_name_4', |
70
|
|
|
'alias.field = :field_name_5', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$this->assertSame($expected, $builder->query); |
|
|
|
|
74
|
|
|
$this->assertTrue($filter->isActive()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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: