Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

DateFilterTest::testFilterEquals()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Filter;
13
14
use Sonata\AdminBundle\Form\Type\Filter\DateType;
15
use Sonata\DoctrinePHPCRAdminBundle\Filter\DateFilter;
16
17
class DateFilterTest extends BaseTestCase
18
{
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        $this->filter = new DateFilter();
23
    }
24
25
    // @todo: Can probably factor the following 4 test cases into a common class
26
    //        IF we introduce another test with the same need.
27
28
    public function testFilterNullData()
29
    {
30
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
31
        $this->assertNull($res);
32
        $this->assertFalse($this->filter->isActive());
33
    }
34
35
    public function testFilterEmptyArrayData()
36
    {
37
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', array());
38
        $this->assertNull($res);
39
        $this->assertFalse($this->filter->isActive());
40
    }
41
42
    public function getFilters()
43
    {
44
        return array(
45
            array('gte', DateType::TYPE_GREATER_EQUAL),
46
            array('gt', DateType::TYPE_GREATER_THAN),
47
            array('lte', DateType::TYPE_LESS_EQUAL),
48
            array('lt', DateType::TYPE_LESS_THAN),
49
            array('eq', DateType::TYPE_NULL, null),
50
            array('neq', DateType::TYPE_NOT_NULL, null),
51
            // test ChoiceTYPE::TYPE_EQUAL separately, special case.
52
        );
53
    }
54
55
    /**
56
     * @dataProvider getFilters
57
     */
58
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = '__null__')
59
    {
60
        $value = new \DateTime('2013/01/16 00:00:00');
61
62
        if ($expectedValue == '__null__') {
63
            $expectedValue = new \DateTime('2013/01/16 00:00:00');
64
        }
65
66
        $this->filter->filter(
67
            $this->proxyQuery,
68
            null,
69
            'somefield',
70
            array('type' => $choiceType, 'value' => $value)
71
        );
72
73
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
74
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
75
76
        $this->assertEquals('a', $opDynamic->getAlias());
77
        $this->assertEquals('somefield', $opDynamic->getField());
78
        $this->assertEquals($expectedValue, $opStatic->getValue());
79
80
        $this->assertTrue($this->filter->isActive());
81
    }
82
83
    public function testFilterEquals()
84
    {
85
        $from = new \DateTime('2013/01/16 00:00:00');
86
        $to = new \DateTime('2013/01/16 23:59:59');
87
88
        $this->filter->filter(
89
            $this->proxyQuery,
90
            null,
91
            'somefield',
92
            array('type' => DateType::TYPE_EQUAL, 'value' => $from)
93
        );
94
95
        // FROM
96
        $opDynamic = $this->qbTester->getNode(
97
            'where.constraint.constraint.operand_dynamic');
98
        $opStatic = $this->qbTester->getNode(
99
            'where.constraint.constraint.operand_static');
100
101
        $this->assertEquals('a', $opDynamic->getAlias());
102
        $this->assertEquals('somefield', $opDynamic->getField());
103
        $this->assertEquals($from, $opStatic->getValue());
104
105
        // TO
106
        $opDynamic = $this->qbTester->getNode(
107
            'where.constraint.constraint[1].operand_dynamic');
108
        $opStatic = $this->qbTester->getNode(
109
            'where.constraint.constraint[1].operand_static');
110
111
        $this->assertEquals('a', $opDynamic->getAlias());
112
        $this->assertEquals('somefield', $opDynamic->getField());
113
        $this->assertEquals($to, $opStatic->getValue());
114
115
        $this->assertTrue($this->filter->isActive());
116
    }
117
}
118