DateFilterTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 109
rs 10
c 0
b 0
f 0

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
A testFilterSwitch() 0 28 3
A testFilterEquals() 0 38 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Filter;
15
16
use Sonata\AdminBundle\Form\Type\Filter\DateType;
17
use Sonata\DoctrinePHPCRAdminBundle\Filter\DateFilter;
18
19
class DateFilterTest extends BaseTestCase
20
{
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->filter = new DateFilter();
25
    }
26
27
    // @todo: Can probably factor the following 4 test cases into a common class
28
    //        IF we introduce another test with the same need.
29
30
    public function testFilterNullData(): void
31
    {
32
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
33
        $this->assertNull($res);
34
        $this->assertFalse($this->filter->isActive());
35
    }
36
37
    public function testFilterEmptyArrayData(): void
38
    {
39
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', []);
40
        $this->assertNull($res);
41
        $this->assertFalse($this->filter->isActive());
42
    }
43
44
    public function getFilters()
45
    {
46
        return [
47
            ['gte', DateType::TYPE_GREATER_EQUAL],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ype::TYPE_GREATER_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_GREATER_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
48
            ['gt', DateType::TYPE_GREATER_THAN],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_GREATER_THAN has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_GREATER_THAN instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
49
            ['lte', DateType::TYPE_LESS_EQUAL],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...teType::TYPE_LESS_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_LESS_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
50
            ['lt', DateType::TYPE_LESS_THAN],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ateType::TYPE_LESS_THAN has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_LESS_THAN instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
51
            ['eq', DateType::TYPE_NULL, null],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ter\DateType::TYPE_NULL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_NULL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
52
            ['neq', DateType::TYPE_NOT_NULL, null],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...DateType::TYPE_NOT_NULL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_NOT_NULL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
53
            // test ChoiceTYPE::TYPE_EQUAL separately, special case.
54
        ];
55
    }
56
57
    /**
58
     * @dataProvider getFilters
59
     */
60
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = '__null__'): void
61
    {
62
        $value = new \DateTime('2013/01/16 00:00:00');
63
64
        if ('__null__' === $expectedValue) {
65
            $expectedValue = new \DateTime('2013/01/16 00:00:00');
66
        }
67
68
        $this->filter->filter(
69
            $this->proxyQuery,
70
            null,
71
            'somefield',
72
            ['type' => $choiceType, 'value' => $value]
73
        );
74
75
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
76
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
77
78
        $this->assertSame('a', $opDynamic->getAlias());
79
        $this->assertSame('somefield', $opDynamic->getField());
80
        $this->assertTrue(
81
            $expectedValue instanceof \DateTimeInterface ?
82
            $expectedValue->getTimestamp() === $opStatic->getValue()->getTimestamp() :
83
            $expectedValue === $opStatic->getValue()
84
        );
85
86
        $this->assertTrue($this->filter->isActive());
87
    }
88
89
    public function testFilterEquals(): void
90
    {
91
        $from = new \DateTime('2013/01/16 00:00:00');
92
        $to = new \DateTime('2013/01/16 23:59:59');
93
94
        $this->filter->filter(
95
            $this->proxyQuery,
96
            null,
97
            'somefield',
98
            ['type' => DateType::TYPE_EQUAL, 'value' => $from]
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...er\DateType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
99
        );
100
101
        // FROM
102
        $opDynamic = $this->qbTester->getNode(
103
            'where.constraint.constraint.operand_dynamic'
104
        );
105
        $opStatic = $this->qbTester->getNode(
106
            'where.constraint.constraint.operand_static'
107
        );
108
109
        $this->assertSame('a', $opDynamic->getAlias());
110
        $this->assertSame('somefield', $opDynamic->getField());
111
        $this->assertSame($from->getTimestamp(), $opStatic->getValue()->getTimestamp());
112
113
        // TO
114
        $opDynamic = $this->qbTester->getNode(
115
            'where.constraint.constraint[1].operand_dynamic'
116
        );
117
        $opStatic = $this->qbTester->getNode(
118
            'where.constraint.constraint[1].operand_static'
119
        );
120
121
        $this->assertSame('a', $opDynamic->getAlias());
122
        $this->assertSame('somefield', $opDynamic->getField());
123
        $this->assertSame($to->getTimestamp(), $opStatic->getValue()->getTimestamp());
124
125
        $this->assertTrue($this->filter->isActive());
126
    }
127
}
128