Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

FilterType/DBAL/DateTimeFilterTypeTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Tests\AdminList\FilterType\DBAL;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\DateTimeFilterType;
7
use ReflectionClass;
8
use Symfony\Component\HttpFoundation\Request;
9
10 View Code Duplication
class DateTimeFilterTypeTest extends BaseDbalFilterTest
11
{
12
    /**
13
     * @var DateTimeFilterType
14
     */
15
    protected $object;
16
17
    protected function setUp()
18
    {
19
        $this->object = new DateTimeFilterType('datetime', 'e');
20
    }
21
22
    /**
23
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,string>>[].

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.

Loading history...
24
     */
25
    public static function applyDataProvider()
26
    {
27
        return [
28
            ['before', '<= :var_datetime', ['date' => '14/04/2014', 'time' => '09:00'], '2014-04-14 09:00'],
29
            ['after', '> :var_datetime', ['date' => '14/04/2014', 'time' => '10:00'], '2014-04-14 10:00'],
30
        ];
31
    }
32
33
    public function testBindRequest()
34
    {
35
        $request = new Request([
36
            'filter_comparator_datetime' => 'before',
37
            'filter_value_datetime' => ['date' => '14/04/2014', 'time' => '09:00'],
38
        ]);
39
40
        $data = [];
41
        $uniqueId = 'datetime';
42
        $this->object->bindRequest($request, $data, $uniqueId);
43
44
        $this->assertEquals(
45
            ['comparator' => 'before', 'value' => ['date' => '14/04/2014', 'time' => '09:00']],
46
            $data
47
        );
48
    }
49
50
    /**
51
     * @param string $comparator  The comparator
52
     * @param string $whereClause The where clause
53
     * @param mixed  $value       The value
54
     * @param mixed  $testValue   The test value
55
     *
56
     * @dataProvider applyDataProvider
57
     */
58
    public function testApply($comparator, $whereClause, $value, $testValue)
59
    {
60
        $qb = $this->getQueryBuilder();
61
        $qb->select('*')
62
            ->from('entity', 'e');
63
        $this->object->setQueryBuilder($qb);
64
        $this->object->apply(['comparator' => $comparator, 'value' => $value], 'datetime');
65
66
        $this->assertEquals("SELECT * FROM entity e WHERE e.datetime $whereClause", $qb->getSQL());
67
        $this->assertEquals($testValue, $qb->getParameter('var_datetime'));
68
    }
69
70
    public function testGetTemplate()
71
    {
72
        $this->assertEquals(
73
            '@KunstmaanAdminList/FilterType/dateTimeFilter.html.twig',
74
            $this->object->getTemplate()
75
        );
76
    }
77
78
    /**
79
     * @throws \ReflectionException
80
     */
81
    public function testApplyReturnsNull()
82
    {
83
        $queryBuilder = $this->createMock(QueryBuilder::class);
84
        $queryBuilder->expects($this->never())->method('setParameter');
85
        $mirror = new ReflectionClass(DateTimeFilterType::class);
86
        $property = $mirror->getProperty('queryBuilder');
87
        $property->setAccessible(true);
88
        $property->setValue($this->object, $queryBuilder);
89
90
        $badData = [
91
            'value' => [
92
                'date' => 'oopsNotADate',
93
                'time' => 'oopsNotATime',
94
            ],
95
            'comparator' => 'true',
96
        ];
97
        $this->object->apply($badData, uniqid());
98
    }
99
}
100