|
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\CallbackFilter; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
20
|
|
|
|
|
21
|
|
|
class CallbackFilterTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testRenderSettings(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$filter = new CallbackFilter(); |
|
26
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
|
27
|
|
|
$options = $filter->getRenderSettings()[1]; |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame(HiddenType::class, $options['operator_type']); |
|
30
|
|
|
$this->assertSame([], $options['operator_options']); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testFilterClosure(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$filter = new CallbackFilter(); |
|
38
|
|
|
$filter->initialize('field_name', [ |
|
39
|
|
|
'callback' => static function ($builder, $alias, $field, $value) { |
|
40
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
|
41
|
|
|
$builder->setParameter('value', $value); |
|
42
|
|
|
|
|
43
|
|
|
return true; |
|
44
|
|
|
}, |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame(['CUSTOM QUERY alias.field'], $builder->query); |
|
|
|
|
|
|
50
|
|
|
$this->assertSame(['value' => 'myValue'], $builder->parameters); |
|
|
|
|
|
|
51
|
|
|
$this->assertTrue($filter->isActive()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testFilterMethod(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$filter = new CallbackFilter(); |
|
59
|
|
|
$filter->initialize('field_name', [ |
|
60
|
|
|
'callback' => [$this, 'customCallback'], |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$this->assertSame(['CUSTOM QUERY alias.field'], $builder->query); |
|
|
|
|
|
|
66
|
|
|
$this->assertSame(['value' => 'myValue'], $builder->parameters); |
|
|
|
|
|
|
67
|
|
|
$this->assertTrue($filter->isActive()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function customCallback($builder, $alias, $field, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
|
73
|
|
|
$builder->setParameter('value', $value); |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testFilterException(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->expectException(\RuntimeException::class); |
|
81
|
|
|
|
|
82
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$filter = new CallbackFilter(); |
|
85
|
|
|
$filter->initialize('field_name', []); |
|
86
|
|
|
|
|
87
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testApplyMethod(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$filter = new CallbackFilter(); |
|
95
|
|
|
$filter->initialize('field_name_test', [ |
|
96
|
|
|
'callback' => static function ($builder, $alias, $field, $value) { |
|
97
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
|
98
|
|
|
$builder->setParameter('value', $value['value']); |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
}, |
|
102
|
|
|
'field_name' => 'field_name_test', |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
$filter->apply($builder, ['value' => 'myValue']); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertSame(['CUSTOM QUERY o.field_name_test'], $builder->query); |
|
|
|
|
|
|
108
|
|
|
$this->assertSame(['value' => 'myValue'], $builder->parameters); |
|
|
|
|
|
|
109
|
|
|
$this->assertTrue($filter->isActive()); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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: