Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

DateFilterTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 0
cbo 2
dl 0
loc 101
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testFilterNullData() 0 6 1
A testFilterEmptyArrayData() 0 6 1
A getFilters() 0 12 1
B testFilterSwitch() 0 24 2
B testFilterEquals() 0 34 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\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