Completed
Push — master ( 6b2632...1706e7 )
by
unknown
07:57 queued 10s
created

tests/Unit/Filter/DateFilterTest.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
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
    public 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],
48
            ['gt', DateType::TYPE_GREATER_THAN],
49
            ['lte', DateType::TYPE_LESS_EQUAL],
50
            ['lt', DateType::TYPE_LESS_THAN],
51
            ['eq', DateType::TYPE_NULL, null],
52
            ['neq', DateType::TYPE_NOT_NULL, null],
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
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...
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]
99
        );
100
101
        // FROM
102
        $opDynamic = $this->qbTester->getNode(
103
            'where.constraint.constraint.operand_dynamic');
104
        $opStatic = $this->qbTester->getNode(
105
            'where.constraint.constraint.operand_static');
106
107
        $this->assertSame('a', $opDynamic->getAlias());
108
        $this->assertSame('somefield', $opDynamic->getField());
109
        $this->assertSame($from->getTimestamp(), $opStatic->getValue()->getTimestamp());
110
111
        // TO
112
        $opDynamic = $this->qbTester->getNode(
113
            'where.constraint.constraint[1].operand_dynamic');
114
        $opStatic = $this->qbTester->getNode(
115
            'where.constraint.constraint[1].operand_static');
116
117
        $this->assertSame('a', $opDynamic->getAlias());
118
        $this->assertSame('somefield', $opDynamic->getField());
119
        $this->assertSame($to->getTimestamp(), $opStatic->getValue()->getTimestamp());
120
121
        $this->assertTrue($this->filter->isActive());
122
    }
123
}
124