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