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\DoctrinePHPCRAdminBundle\Tests\Unit\Filter; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
17
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Filter\CallbackFilter; |
18
|
|
|
|
19
|
|
|
class CallBackFilterTest extends BaseTestCase |
20
|
|
|
{ |
21
|
|
|
public function testFilterNullData(): void |
22
|
|
|
{ |
23
|
|
|
$filter = new CallbackFilter(); |
24
|
|
|
$filter->initialize('field_name', ['callback' => static function (): void { |
25
|
|
|
}]); |
26
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
27
|
|
|
$this->assertNull($res); |
28
|
|
|
$this->assertFalse($filter->isActive()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testFilterEmptyArrayData(): void |
32
|
|
|
{ |
33
|
|
|
$filter = new CallbackFilter(); |
34
|
|
|
|
35
|
|
|
$filter->initialize('field_name', ['callback' => static function (): void { |
36
|
|
|
}]); |
37
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', []); |
|
|
|
|
38
|
|
|
$this->assertNull($res); |
39
|
|
|
$this->assertFalse($filter->isActive()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testFilterMethod(): void |
43
|
|
|
{ |
44
|
|
|
$this->proxyQuery->expects($this->once()) |
|
|
|
|
45
|
|
|
->method('getQueryBuilder') |
46
|
|
|
->willReturn($this->qb); |
47
|
|
|
|
48
|
|
|
$filter = new CallbackFilter(); |
49
|
|
|
$filter->initialize('field_name', [ |
50
|
|
|
'callback' => [$this, 'callbackMethod'], |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']); |
54
|
|
|
|
55
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
56
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
57
|
|
|
|
58
|
|
|
$this->assertSame('a', $opDynamic->getAlias()); |
59
|
|
|
$this->assertSame('somefield', $opDynamic->getField()); |
60
|
|
|
$this->assertSame('somevalue', $opStatic->getValue()); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($filter->isActive()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
66
|
|
|
{ |
67
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
68
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testFilterClosure(): void |
74
|
|
|
{ |
75
|
|
|
$this->proxyQuery->expects($this->once()) |
|
|
|
|
76
|
|
|
->method('getQueryBuilder') |
77
|
|
|
->willReturn($this->qb); |
78
|
|
|
|
79
|
|
|
$filter = new CallbackFilter(); |
80
|
|
|
$filter->initialize('field_name', [ |
81
|
|
|
'callback' => static function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) { |
82
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
83
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
}, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']); |
90
|
|
|
|
91
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
92
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
93
|
|
|
|
94
|
|
|
$this->assertSame('a', $opDynamic->getAlias()); |
95
|
|
|
$this->assertSame('somefield', $opDynamic->getField()); |
96
|
|
|
$this->assertSame('somevalue', $opStatic->getValue()); |
97
|
|
|
|
98
|
|
|
$this->assertTrue($filter->isActive()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testWithoutCallback(): void |
102
|
|
|
{ |
103
|
|
|
$this->expectException(\RuntimeException::class); |
104
|
|
|
|
105
|
|
|
$filter = new CallbackFilter(); |
106
|
|
|
|
107
|
|
|
$filter->setOption('callback', null); |
108
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testCallbackNotCallable(): void |
112
|
|
|
{ |
113
|
|
|
$this->expectException(\RuntimeException::class); |
114
|
|
|
|
115
|
|
|
$filter = new CallbackFilter(); |
116
|
|
|
|
117
|
|
|
$filter->setOption('callback', 'someCallback'); |
118
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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: