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

AdminList/FilterType/ORM/DateFilterTypeTest.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\ORM;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
7
use Kunstmaan\AdminListBundle\Tests\UnitTester;
8
use ReflectionClass;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class DateFilterTypeTest extends BaseOrmFilterTest
12
{
13
    /**
14
     * @var UnitTester
15
     */
16
    protected $tester;
17
18
    /**
19
     * @var DateFilterType
20
     */
21
    protected $object;
22
23
    protected function setUp()
24
    {
25
        $this->object = new DateFilterType('date', 'b');
26
    }
27
28
    public function testBindRequest()
29
    {
30
        $request = new Request(['filter_comparator_date' => 'before', 'filter_value_date' => '01/01/2012']);
31
32
        $data = [];
33
        $uniqueId = 'date';
34
        $this->object->bindRequest($request, $data, $uniqueId);
35
36
        $this->assertEquals(['comparator' => 'before', 'value' => '01/01/2012'], $data);
37
    }
38
39
    /**
40
     * @param string $comparator  The comparator
41
     * @param string $whereClause The where clause
42
     * @param mixed  $value       The value
43
     * @param mixed  $testValue   The test value
44
     *
45
     * @dataProvider applyDataProvider
46
     */
47
    public function testApply($comparator, $whereClause, $value, $testValue)
48
    {
49
        $qb = $this->getQueryBuilder();
50
51
        $qb->select('b')
52
            ->from('Entity', 'b');
53
        $this->object->setQueryBuilder($qb);
54
        $this->object->apply(['comparator' => $comparator, 'value' => $value], 'date');
55
56
        $this->assertEquals("SELECT b FROM Entity b WHERE b.date $whereClause", $qb->getDQL());
57
        $this->assertEquals($testValue, $qb->getParameter('var_date')->getValue());
58
    }
59
60
    /**
61
     * @return array
62
     */
63 View Code Duplication
    public static function applyDataProvider()
64
    {
65
        return [
66
            ['before', '<= :var_date', '20/12/2012', '2012-12-20'],
67
            ['after', '> :var_date', '21/12/2012', '2012-12-21'],
68
        ];
69
    }
70
71
    public function testGetTemplate()
72
    {
73
        $this->assertEquals('@KunstmaanAdminList/FilterType/dateFilter.html.twig', $this->object->getTemplate());
74
    }
75
76
    /**
77
     * @throws \ReflectionException
78
     */
79
    public function testGetAlias()
80
    {
81
        $this->object = new DateFilterType('date', null);
82
        $mirror = new ReflectionClass(DateFilterType::class);
83
        $method = $mirror->getMethod('getAlias');
84
        $method->setAccessible(true);
85
        $alias = $method->invoke($this->object);
86
        $this->assertEquals('', $alias);
87
        $this->object = new DateFilterType('date', 'hello.');
88
        $mirror = new ReflectionClass(DateFilterType::class);
89
        $method = $mirror->getMethod('getAlias');
90
        $method->setAccessible(true);
91
        $alias = $method->invoke($this->object);
92
        $this->assertEquals('hello.', $alias);
93
    }
94
95
    /**
96
     * @throws \ReflectionException
97
     */
98 View Code Duplication
    public function testApplyReturnsNull()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $queryBuilder = $this->createMock(QueryBuilder::class);
101
        $queryBuilder->expects($this->never())->method('setParameter');
102
        $mirror = new ReflectionClass(DateFilterType::class);
103
        $property = $mirror->getProperty('queryBuilder');
104
        $property->setAccessible(true);
105
        $property->setValue($this->object, $queryBuilder);
106
        $badData = [
107
            'value' => 'oopsNotADate',
108
            'comparator' => 'true',
109
        ];
110
        $this->object->apply($badData, uniqid());
111
    }
112
}
113