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