1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Patrick Landolt <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Tests\Filter; |
13
|
|
|
|
14
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
15
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\DateRangeFilter; |
16
|
|
|
|
17
|
|
|
class DateRangeFilterTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testFilterEmpty() |
20
|
|
|
{ |
21
|
|
|
$filter = new DateRangeFilter(); |
22
|
|
|
$filter->initialize('field_name', array('field_options' => array('class' => 'FooBar'))); |
23
|
|
|
|
24
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
27
|
|
|
$filter->filter($builder, 'alias', 'field', ''); |
28
|
|
|
$filter->filter($builder, 'alias', 'field', 'test'); |
29
|
|
|
$filter->filter($builder, 'alias', 'field', false); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$filter->filter($builder, 'alias', 'field', array()); |
|
|
|
|
32
|
|
|
$filter->filter($builder, 'alias', 'field', array(null, 'test')); |
|
|
|
|
33
|
|
|
$filter->filter($builder, 'alias', 'field', array('type' => null, 'value' => array())); |
|
|
|
|
34
|
|
|
$filter->filter($builder, 'alias', 'field', array( |
|
|
|
|
35
|
|
|
'type' => null, |
36
|
|
|
'value' => array('start' => null, 'end' => null), |
37
|
|
|
)); |
38
|
|
|
$filter->filter($builder, 'alias', 'field', array( |
|
|
|
|
39
|
|
|
'type' => null, |
40
|
|
|
'value' => array('start' => '', 'end' => ''), |
41
|
|
|
)); |
42
|
|
|
|
43
|
|
|
$this->assertSame(array(), $builder->query); |
|
|
|
|
44
|
|
|
$this->assertFalse($filter->isActive()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testFilterStartDateAndEndDate() |
48
|
|
|
{ |
49
|
|
|
$filter = new DateRangeFilter(); |
50
|
|
|
$filter->initialize('field_name', array('field_options' => array('class' => 'FooBar'))); |
51
|
|
|
|
52
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$startDateTime = new \DateTime('2016-08-01'); |
55
|
|
|
$endDateTime = new \DateTime('2016-08-31'); |
56
|
|
|
|
57
|
|
|
$filter->filter($builder, 'alias', 'field', array( |
|
|
|
|
58
|
|
|
'type' => null, |
59
|
|
|
'value' => array( |
60
|
|
|
'start' => $startDateTime, |
61
|
|
|
'end' => $endDateTime, |
62
|
|
|
), |
63
|
|
|
)); |
64
|
|
|
|
65
|
|
|
$this->assertSame(array('alias.field >= :field_name_0', 'alias.field <= :field_name_1'), $builder->query); |
|
|
|
|
66
|
|
|
$this->assertSame(array( |
67
|
|
|
'field_name_0' => $startDateTime, |
68
|
|
|
'field_name_1' => $endDateTime, |
69
|
|
|
), $builder->parameters); |
|
|
|
|
70
|
|
|
$this->assertTrue($filter->isActive()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testFilterStartDate() |
74
|
|
|
{ |
75
|
|
|
$filter = new DateRangeFilter(); |
76
|
|
|
$filter->initialize('field_name', array('field_options' => array('class' => 'FooBar'))); |
77
|
|
|
|
78
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$startDateTime = new \DateTime('2016-08-01'); |
81
|
|
|
|
82
|
|
|
$filter->filter($builder, 'alias', 'field', array( |
|
|
|
|
83
|
|
|
'type' => null, |
84
|
|
|
'value' => array( |
85
|
|
|
'start' => $startDateTime, |
86
|
|
|
'end' => '', |
87
|
|
|
), |
88
|
|
|
)); |
89
|
|
|
|
90
|
|
|
$this->assertSame(array('alias.field >= :field_name_0'), $builder->query); |
|
|
|
|
91
|
|
|
$this->assertSame(array('field_name_0' => $startDateTime), $builder->parameters); |
|
|
|
|
92
|
|
|
$this->assertTrue($filter->isActive()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testFilterEndDate() |
96
|
|
|
{ |
97
|
|
|
$filter = new DateRangeFilter(); |
98
|
|
|
$filter->initialize('field_name', array('field_options' => array('class' => 'FooBar'))); |
99
|
|
|
|
100
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$endDateTime = new \DateTime('2016-08-31'); |
103
|
|
|
|
104
|
|
|
$filter->filter($builder, 'alias', 'field', array( |
|
|
|
|
105
|
|
|
'type' => null, |
106
|
|
|
'value' => array( |
107
|
|
|
'start' => '', |
108
|
|
|
'end' => $endDateTime, |
109
|
|
|
), |
110
|
|
|
)); |
111
|
|
|
|
112
|
|
|
$this->assertSame(array('alias.field <= :field_name_1'), $builder->query); |
|
|
|
|
113
|
|
|
$this->assertSame(array('field_name_1' => $endDateTime), $builder->parameters); |
|
|
|
|
114
|
|
|
$this->assertTrue($filter->isActive()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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: