1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\Tests\AdminList\FilterType\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType; |
7
|
|
|
use Kunstmaan\AdminListBundle\Tests\unit\AdminList\FilterType\ORM\BaseOrmFilterTest; |
8
|
|
|
use Kunstmaan\AdminListBundle\Tests\UnitTester; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generated by PHPUnit_SkeletonGenerator on 2012-09-26 at 13:21:34. |
14
|
|
|
*/ |
15
|
|
|
class DateFilterTypeTest extends BaseOrmFilterTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UnitTester |
19
|
|
|
*/ |
20
|
|
|
protected $tester; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DateFilterType |
24
|
|
|
*/ |
25
|
|
|
protected $object; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->object = new DateFilterType('date', 'b'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testBindRequest() |
33
|
|
|
{ |
34
|
|
|
$request = new Request(array('filter_comparator_date' => 'before', 'filter_value_date' => '01/01/2012')); |
35
|
|
|
|
36
|
|
|
$data = array(); |
37
|
|
|
$uniqueId = 'date'; |
38
|
|
|
$this->object->bindRequest($request, $data, $uniqueId); |
39
|
|
|
|
40
|
|
|
$this->assertEquals(array('comparator' => 'before', 'value' => '01/01/2012'), $data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $comparator The comparator |
45
|
|
|
* @param string $whereClause The where clause |
46
|
|
|
* @param mixed $value The value |
47
|
|
|
* @param mixed $testValue The test value |
48
|
|
|
* |
49
|
|
|
* @dataProvider applyDataProvider |
50
|
|
|
*/ |
51
|
|
|
public function testApply($comparator, $whereClause, $value, $testValue) |
52
|
|
|
{ |
53
|
|
|
$qb = $this->getQueryBuilder(); |
54
|
|
|
|
55
|
|
|
$qb->select('b') |
56
|
|
|
->from('Entity', 'b'); |
57
|
|
|
$this->object->setQueryBuilder($qb); |
58
|
|
|
$this->object->apply(array('comparator' => $comparator, 'value' => $value), 'date'); |
59
|
|
|
|
60
|
|
|
$this->assertEquals("SELECT b FROM Entity b WHERE b.date $whereClause", $qb->getDQL()); |
61
|
|
|
$this->assertEquals($testValue, $qb->getParameter('var_date')->getValue()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
|
|
|
|
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public static function applyDataProvider() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return array( |
70
|
|
|
array('before', '<= :var_date', '20/12/2012', '2012-12-20'), |
71
|
|
|
array('after', '> :var_date', '21/12/2012', '2012-12-21'), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetTemplate() |
76
|
|
|
{ |
77
|
|
|
$this->assertEquals('KunstmaanAdminListBundle:FilterType:dateFilter.html.twig', $this->object->getTemplate()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws \ReflectionException |
82
|
|
|
*/ |
83
|
|
|
public function testGetAlias() |
84
|
|
|
{ |
85
|
|
|
$this->object = new DateFilterType('date', null); |
86
|
|
|
$mirror = new ReflectionClass(DateFilterType::class); |
87
|
|
|
$method = $mirror->getMethod('getAlias'); |
88
|
|
|
$method->setAccessible(true); |
89
|
|
|
$alias = $method->invoke($this->object); |
90
|
|
|
$this->assertEquals('', $alias); |
91
|
|
|
$this->object = new DateFilterType('date', 'hello.'); |
92
|
|
|
$mirror = new ReflectionClass(DateFilterType::class); |
93
|
|
|
$method = $mirror->getMethod('getAlias'); |
94
|
|
|
$method->setAccessible(true); |
95
|
|
|
$alias = $method->invoke($this->object); |
96
|
|
|
$this->assertEquals('hello.', $alias); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws \ReflectionException |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function testApplyReturnsNull() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
105
|
|
|
$queryBuilder->expects($this->never())->method('setParameter'); |
106
|
|
|
$mirror = new ReflectionClass(DateFilterType::class); |
107
|
|
|
$property = $mirror->getProperty('queryBuilder'); |
108
|
|
|
$property->setAccessible(true); |
109
|
|
|
$property->setValue($this->object, $queryBuilder); |
110
|
|
|
$badData = [ |
111
|
|
|
'value' => 'oopsNotADate', |
112
|
|
|
'comparator' => 'true', |
113
|
|
|
]; |
114
|
|
|
$this->object->apply($badData, uniqid()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.