DateTimeFilterTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmpty() 0 18 1
A testGetType() 0 4 1
A testFilter() 0 17 1
A getExamples() 0 13 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\DoctrineMongoDBAdminBundle\Tests\Filter;
15
16
use Sonata\AdminBundle\Form\Type\Operator\DateOperatorType;
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' => DateOperatorType::TYPE_EQUAL, 'value' => new \DateTime('now')], 'range'],
72
            [['type' => DateOperatorType::TYPE_GREATER_EQUAL, 'value' => new \DateTime('now')], 'gte'],
73
            [['type' => DateOperatorType::TYPE_GREATER_THAN, 'value' => new \DateTime('now')], 'gt'],
74
            [['type' => DateOperatorType::TYPE_LESS_EQUAL, 'value' => new \DateTime('now')], 'lte'],
75
            [['type' => DateOperatorType::TYPE_LESS_THAN, 'value' => new \DateTime('now')], 'lt'],
76
            [['type' => DateOperatorType::TYPE_NULL], 'equals'],
77
            [['type' => DateOperatorType::TYPE_NOT_NULL], 'notEqual'],
78
            [['value' => new \DateTime('now')], 'range'],
79
        ];
80
    }
81
}
82