Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

NumberFilterTest::testFilterEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Tests\Filter;
13
14
use Sonata\DoctrineORMAdminBundle\Filter\NumberFilter;
15
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
16
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
17
18
class NumberFilterTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testFilterEmpty()
21
    {
22
        $filter = new NumberFilter;
23
        $filter->initialize('field_name', array('field_options' => array('class' => 'FooBar')));
24
25
        $builder = new ProxyQuery(new QueryBuilder);
26
27
        $filter->filter($builder, 'alias', 'field', null);
28
        $filter->filter($builder, 'alias', 'field', 'asds');
29
30
        $this->assertEquals(array(), $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...
31
        $this->assertEquals(false, $filter->isActive());
32
    }
33
34
    public function testFilterInvalidOperator()
35
    {
36
        $filter = new NumberFilter;
37
        $filter->initialize('field_name', array('field_options' => array('class' => 'FooBar')));
38
39
        $builder = new ProxyQuery(new QueryBuilder);
40
41
        $filter->filter($builder, 'alias', 'field', array('type' => 'foo'));
0 ignored issues
show
Documentation introduced by
array('type' => 'foo') is of type array<string,string,{"type":"string"}>, but the function expects a string.

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...
42
43
        $this->assertEquals(array(), $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...
44
        $this->assertEquals(false, $filter->isActive());
45
    }
46
47
    public function testFilter()
48
    {
49
        $filter = new NumberFilter;
50
        $filter->initialize('field_name', array('field_options' => array('class' => 'FooBar')));
51
52
        $builder = new ProxyQuery(new QueryBuilder);
53
54
        $filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_EQUAL, 'value' => 42));
0 ignored issues
show
Documentation introduced by
array('type' => \Sonata\...E_EQUAL, 'value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
55
        $filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_GREATER_EQUAL, 'value' => 42));
0 ignored issues
show
Documentation introduced by
array('type' => \Sonata\...R_EQUAL, 'value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
56
        $filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_GREATER_THAN, 'value' => 42));
0 ignored issues
show
Documentation introduced by
array('type' => \Sonata\...ER_THAN, 'value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
57
        $filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_LESS_EQUAL, 'value' => 42));
0 ignored issues
show
Documentation introduced by
array('type' => \Sonata\...S_EQUAL, 'value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
58
        $filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_LESS_THAN, 'value' => 42));
0 ignored issues
show
Documentation introduced by
array('type' => \Sonata\...SS_THAN, 'value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
59
        $filter->filter($builder, 'alias', 'field', array('value' => 42));
0 ignored issues
show
Documentation introduced by
array('value' => 42) is of type array<string,integer,{"value":"integer"}>, but the function expects a string.

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...
60
61
        $expected = array(
62
            'alias.field = :field_name_0',
63
            'alias.field >= :field_name_1',
64
            'alias.field > :field_name_2',
65
            'alias.field <= :field_name_3',
66
            'alias.field < :field_name_4',
67
            'alias.field = :field_name_5',
68
        );
69
70
        $this->assertEquals($expected, $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...
71
        $this->assertEquals(true, $filter->isActive());
72
    }
73
}
74