Completed
Push — 5.1 ( e86e6d...b260ec )
by Kristof
106:07 queued 92:52
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 Codeception\Test\Unit;
6
use Doctrine\ORM\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
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 Unit
16
{
17
    /**
18
     * @var UnitTester
19
     */
20
    protected $tester;
21
22
    /**
23
     * @var DateFilterType
24
     */
25
    protected $object;
26
27
    /**
28
     * Sets up the fixture, for example, opens a network connection.
29
     * This method is called before a test is executed.
30
     */
31
    protected function _before()
32
    {
33
        $this->object = new DateFilterType('date', 'b');
34
    }
35
36
    public function testBindRequest()
37
    {
38
        $request = new Request(array('filter_comparator_date' => 'before', 'filter_value_date' => '01/01/2012'));
39
40
        $data = array();
41
        $uniqueId = 'date';
42
        $this->object->bindRequest($request, $data, $uniqueId);
43
44
        $this->assertEquals(array('comparator' => 'before', 'value' => '01/01/2012'), $data);
45
    }
46
47
    /**
48
     * @param string $comparator  The comparator
49
     * @param string $whereClause The where clause
50
     * @param mixed  $value       The value
51
     * @param mixed  $testValue   The test value
52
     *
53
     * @dataProvider applyDataProvider
54
     */
55
    public function testApply($comparator, $whereClause, $value, $testValue)
56
    {
57
        $qb = $this->tester->getORMQueryBuilder();
58
59
        $qb->select('b')
60
            ->from('Entity', 'b');
61
        $this->object->setQueryBuilder($qb);
62
        $this->object->apply(array('comparator' => $comparator, 'value' => $value), 'date');
63
64
        $this->assertEquals("SELECT b FROM Entity b WHERE b.date $whereClause", $qb->getDQL());
65
        $this->assertEquals($testValue, $qb->getParameter('var_date')->getValue());
66
    }
67
68
    /**
69
     * @return array
70
     */
71 View Code Duplication
    public static function applyDataProvider()
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...
72
    {
73
        return array(
74
            array('before', '<= :var_date', '20/12/2012', '2012-12-20'),
75
            array('after', '> :var_date', '21/12/2012', '2012-12-21'),
76
        );
77
    }
78
79
    public function testGetTemplate()
80
    {
81
        $this->assertEquals('KunstmaanAdminListBundle:FilterType:dateFilter.html.twig', $this->object->getTemplate());
82
    }
83
84
    /**
85
     * @throws \ReflectionException
86
     */
87
    public function testGetAlias()
88
    {
89
        $this->object = new DateFilterType('date', null);
90
        $mirror = new ReflectionClass(DateFilterType::class);
91
        $method = $mirror->getMethod('getAlias');
92
        $method->setAccessible(true);
93
        $alias = $method->invoke($this->object);
94
        $this->assertEquals('', $alias);
95
        $this->object = new DateFilterType('date', 'hello.');
96
        $mirror = new ReflectionClass(DateFilterType::class);
97
        $method = $mirror->getMethod('getAlias');
98
        $method->setAccessible(true);
99
        $alias = $method->invoke($this->object);
100
        $this->assertEquals('hello.', $alias);
101
    }
102
103
    /**
104
     * @throws \ReflectionException
105
     */
106 View Code Duplication
    public function testApplyReturnsNull()
107
    {
108
        $queryBuilder = $this->createMock(QueryBuilder::class);
109
        $queryBuilder->expects($this->never())->method('setParameter');
110
        $mirror = new ReflectionClass(DateFilterType::class);
111
        $property = $mirror->getProperty('queryBuilder');
112
        $property->setAccessible(true);
113
        $property->setValue($this->object, $queryBuilder);
114
        $badData = [
115
            'value' => 'oopsNotADate',
116
            'comparator' => 'true',
117
        ];
118
        $this->object->apply($badData, uniqid());
119
    }
120
}
121