Completed
Push — master ( 4386d7...5c37fe )
by
unknown
01:41 queued 11s
created

EmptyFilterTest::testRenderSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\EmptyFilter;
19
use Sonata\Form\Type\BooleanType;
20
21
final class EmptyFilterTest extends TestCase
22
{
23
    public function testEmpty(): void
24
    {
25
        $filter = new EmptyFilter();
26
        $filter->initialize('field_name', [
27
            'field_options' => ['class' => 'FooBar'],
28
        ]);
29
30
        $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...
31
32
        $filter->filter($builder, 'alias', 'field', null);
33
34
        $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...
35
        $this->assertFalse($filter->isActive());
36
    }
37
38
    /**
39
     * @dataProvider valueDataProvider
40
     */
41
    public function testValue(int $value, string $expectedQuery): void
42
    {
43
        $filter = new EmptyFilter();
44
        $filter->initialize('field_name', [
45
            'field_options' => ['class' => 'FooBar'],
46
        ]);
47
48
        $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...
49
        $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...
50
51
        $filter->filter($builder, 'alias', 'field', ['value' => $value]);
52
53
        $this->assertSame([$expectedQuery], $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...
54
        $this->assertTrue($filter->isActive());
55
    }
56
57
    public function testRenderSettings(): void
58
    {
59
        $filter = new EmptyFilter();
60
        $filter->initialize('field_name', [
61
            'field_options' => ['class' => 'FooBar'],
62
        ]);
63
        $options = $filter->getRenderSettings()[1];
64
65
        $this->assertSame(BooleanType::class, $options['field_type']);
66
    }
67
68
    public function valueDataProvider(): array
69
    {
70
        return [
71
            [BooleanType::TYPE_YES, 'alias.field IS NULL'],
72
            [BooleanType::TYPE_NO, 'alias.field IS NOT NULL'],
73
        ];
74
    }
75
}
76