Completed
Push — master ( 5b1ac4...a1db3b )
by David
17:06
created

tests/Unit/Filter/DateFilterTest.php (2 issues)

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
/*
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();
0 ignored issues
show
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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', []);
38
        $this->assertNull($res);
39
        $this->assertFalse($this->filter->isActive());
40
    }
41
42
    public function getFilters()
43
    {
44
        return [
45
            ['gte', DateType::TYPE_GREATER_EQUAL],
46
            ['gt', DateType::TYPE_GREATER_THAN],
47
            ['lte', DateType::TYPE_LESS_EQUAL],
48
            ['lt', DateType::TYPE_LESS_THAN],
49
            ['eq', DateType::TYPE_NULL, null],
50
            ['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__')
0 ignored issues
show
The parameter $operatorMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
            ['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
            ['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