Completed
Push — 3.x ( c43f1e...b382c8 )
by Jordi Sala
01:23
created

DateTimeFilterTest::testFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\DoctrineMongoDBAdminBundle\Tests\Filter;
15
16
use Sonata\AdminBundle\Form\Type\Filter\DateType;
17
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
18
use Sonata\DoctrineMongoDBAdminBundle\Filter\DateTimeFilter;
19
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
20
21
final class DateTimeFilterTest extends FilterWithQueryBuilderTest
22
{
23
    public function testEmpty(): void
24
    {
25
        $filter = new DateTimeFilter();
26
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
27
28
        $builder = new ProxyQuery($this->getQueryBuilder());
29
30
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
            ->expects($this->never())
32
            ->method('field')
33
        ;
34
35
        $filter->filter($builder, 'alias', 'field', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        $filter->filter($builder, 'alias', 'field', '');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        $filter->filter($builder, 'alias', 'field', []);
38
39
        $this->assertFalse($filter->isActive());
40
    }
41
42
    public function testGetType(): void
43
    {
44
        $this->assertSame(DateTimeType::class, (new DateTimeFilter())->getFieldType());
45
    }
46
47
    /**
48
     * @dataProvider getExamples
49
     */
50
    public function testFilter(array $data, string $method): void
51
    {
52
        $filter = new DateTimeFilter();
53
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
54
55
        $builder = new ProxyQuery($this->getQueryBuilder());
56
57
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            ->expects($this->once())
59
            ->method($method)
60
            ->with($data['value'] ?? null)
61
        ;
62
63
        $filter->filter($builder, 'alias', 'field', $data);
64
65
        $this->assertTrue($filter->isActive());
66
    }
67
68
    public function getExamples(): array
69
    {
70
        return [
71
            [['type' => DateType::TYPE_EQUAL, 'value' => new \DateTime('now')], 'range'],
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...
72
            [['type' => DateType::TYPE_GREATER_EQUAL, 'value' => new \DateTime('now')], 'gte'],
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...
73
            [['type' => DateType::TYPE_GREATER_THAN, 'value' => new \DateTime('now')], 'gt'],
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...
74
            [['type' => DateType::TYPE_LESS_EQUAL, 'value' => new \DateTime('now')], 'lte'],
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...
75
            [['type' => DateType::TYPE_LESS_THAN, 'value' => new \DateTime('now')], 'lt'],
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...
76
            [['type' => DateType::TYPE_NULL], 'equals'],
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...
77
            [['type' => DateType::TYPE_NOT_NULL], 'notEqual'],
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...
78
            [['value' => new \DateTime('now')], 'range'],
79
        ];
80
    }
81
}
82