DateFilterTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmpty() 0 13 1
A testGetType() 0 4 1
A testFilterRecordsWholeDay() 0 11 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\DoctrineORMAdminBundle\Tests\Filter;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
18
use Sonata\DoctrineORMAdminBundle\Filter\DateFilter;
19
use Symfony\Component\Form\Extension\Core\Type\DateType;
20
21
/**
22
 * @author Ennio Wolsink <[email protected]>
23
 */
24
class DateFilterTest extends TestCase
25
{
26
    public function testEmpty(): void
27
    {
28
        $filter = new DateFilter();
29
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
30
31
        $builder = new ProxyQuery(new QueryBuilder());
0 ignored issues
show
Documentation introduced by
new \Sonata\DoctrineORMA...s\Filter\QueryBuilder() is of type object<Sonata\DoctrineOR...ts\Filter\QueryBuilder>, but the function expects a object<Doctrine\ORM\QueryBuilder>.

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...
32
33
        $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...
34
        $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...
35
36
        $this->assertSame([], $builder->query);
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $this->assertFalse($filter->isActive());
38
    }
39
40
    public function testGetType(): void
41
    {
42
        $this->assertSame(DateType::class, (new DateFilter())->getFieldType());
43
    }
44
45
    public function testFilterRecordsWholeDay(): void
46
    {
47
        $filter = new DateFilter();
48
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
49
50
        $builder = new ProxyQuery(new QueryBuilder());
0 ignored issues
show
Documentation introduced by
new \Sonata\DoctrineORMA...s\Filter\QueryBuilder() is of type object<Sonata\DoctrineOR...ts\Filter\QueryBuilder>, but the function expects a object<Doctrine\ORM\QueryBuilder>.

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...
51
        $filter->filter($builder, 'alias', 'field', ['value' => new \DateTime()]);
52
53
        $this->assertCount(2, $builder->parameters);
0 ignored issues
show
Documentation introduced by
The property parameters does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $this->assertCount(2, $builder->query);
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
}
57