1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\Tests\AdminList\FilterType\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateTimeFilterType; |
7
|
|
|
use Kunstmaan\AdminListBundle\Tests\unit\AdminList\FilterType\ORM\BaseOrmFilterTest; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
View Code Duplication |
class DateTimeFilterTypeTest extends BaseOrmFilterTest |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DateTimeFilterType |
15
|
|
|
*/ |
16
|
|
|
protected $object; |
17
|
|
|
|
18
|
|
|
protected function setUp(): void |
19
|
|
|
{ |
20
|
|
|
$this->object = new DateTimeFilterType('datetime', 'b'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public static function applyDataProvider() |
27
|
|
|
{ |
28
|
|
|
return array( |
29
|
|
|
array('before', '<= :var_datetime', array('date' => '14/04/2014', 'time' => '09:00'), '2014-04-14 09:00'), |
30
|
|
|
array('after', '> :var_datetime', array('date' => '14/04/2014', 'time' => '10:00'), '2014-04-14 10:00'), |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testBindRequest() |
35
|
|
|
{ |
36
|
|
|
$request = new Request(array( |
37
|
|
|
'filter_comparator_datetime' => 'before', |
38
|
|
|
'filter_value_datetime' => array('date' => '14/04/2014', 'time' => '09:00'), |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$data = array(); |
42
|
|
|
$uniqueId = 'datetime'; |
43
|
|
|
$this->object->bindRequest($request, $data, $uniqueId); |
44
|
|
|
|
45
|
|
|
$this->assertEquals( |
46
|
|
|
array('comparator' => 'before', 'value' => array('date' => '14/04/2014', 'time' => '09:00')), |
47
|
|
|
$data |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $comparator The comparator |
53
|
|
|
* @param string $whereClause The where clause |
54
|
|
|
* @param mixed $value The value |
55
|
|
|
* @param mixed $testValue The test value |
56
|
|
|
* |
57
|
|
|
* @dataProvider applyDataProvider |
58
|
|
|
*/ |
59
|
|
|
public function testApply($comparator, $whereClause, $value, $testValue) |
60
|
|
|
{ |
61
|
|
|
$qb = $this->getQueryBuilder(); |
62
|
|
|
$qb->select('b') |
63
|
|
|
->from('Entity', 'b'); |
64
|
|
|
$this->object->setQueryBuilder($qb); |
65
|
|
|
$this->object->apply(array('comparator' => $comparator, 'value' => $value), 'datetime'); |
66
|
|
|
|
67
|
|
|
$this->assertEquals("SELECT b FROM Entity b WHERE b.datetime $whereClause", $qb->getDQL()); |
68
|
|
|
$this->assertEquals($testValue, $qb->getParameter('var_datetime')->getValue()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetTemplate() |
72
|
|
|
{ |
73
|
|
|
$this->assertEquals( |
74
|
|
|
'@KunstmaanAdminList/FilterType/dateTimeFilter.html.twig', |
75
|
|
|
$this->object->getTemplate() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws \ReflectionException |
81
|
|
|
*/ |
82
|
|
|
public function testApplyReturnsNull() |
83
|
|
|
{ |
84
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
85
|
|
|
$queryBuilder->expects($this->never())->method('setParameter'); |
86
|
|
|
$mirror = new ReflectionClass(DateTimeFilterType::class); |
87
|
|
|
$property = $mirror->getProperty('queryBuilder'); |
88
|
|
|
$property->setAccessible(true); |
89
|
|
|
$property->setValue($this->object, $queryBuilder); |
90
|
|
|
|
91
|
|
|
$badData = [ |
92
|
|
|
'value' => [ |
93
|
|
|
'date' => 'oopsNotADate', |
94
|
|
|
'time' => 'oopsNotATime', |
95
|
|
|
], |
96
|
|
|
'comparator' => 'true', |
97
|
|
|
]; |
98
|
|
|
$this->object->apply($badData, uniqid()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.