Completed
Push — master ( 080928...f7c290 )
by
unknown
15:37
created

NumberFilterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\DoctrinePHPCRAdminBundle\Tests\Unit\Filter;
13
14
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
15
use Sonata\DoctrinePHPCRAdminBundle\Filter\NumberFilter;
16
17
class NumberFilterTest extends BaseTestCase
18
{
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        $this->filter = new NumberFilter();
0 ignored issues
show
Bug introduced by
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
    }
24
25
    public function testFilterNullData()
26
    {
27
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
28
        $this->assertNull($res);
29
        $this->assertFalse($this->filter->isActive());
30
    }
31
32
    public function testFilterEmptyArrayData()
33
    {
34
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', array());
35
        $this->assertNull($res);
36
        $this->assertFalse($this->filter->isActive());
37
    }
38
39
    public function testFilterEmptyArrayDataSpecifiedType()
40
    {
41
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', array('type' => NumberType::TYPE_EQUAL));
42
        $this->assertNull($res);
43
        $this->assertFalse($this->filter->isActive());
44
    }
45
46
    public function testFilterEmptyArrayDataWithMeaninglessValue()
47
    {
48
        $this->proxyQuery->expects($this->never())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49
            ->method('getQueryBuilder');
50
51
        $this->filter->filter($this->proxyQuery, null, 'somefield', array('type' => NumberType::TYPE_EQUAL, 'value' => ' '));
52
        $this->assertFalse($this->filter->isActive());
53
    }
54
55
    public function getFilters()
56
    {
57
        return array(
58
            array('gte', NumberType::TYPE_GREATER_EQUAL, 2),
59
            array('gt', NumberType::TYPE_GREATER_THAN, 3),
60
            array('lte', NumberType::TYPE_LESS_EQUAL, 4),
61
            array('lt', NumberType::TYPE_LESS_THAN, 5),
62
            array('eq', NumberType::TYPE_EQUAL, 6),
63
            array('eq', 'default', 7),
64
        );
65
    }
66
67
    /**
68
     * @dataProvider getFilters
69
     */
70
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue')
0 ignored issues
show
Unused Code introduced by
The parameter $operatorMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $this->filter->filter(
73
            $this->proxyQuery,
74
            null,
75
            'somefield',
76
            array('type' => $choiceType, 'value' => $expectedValue)
77
        );
78
79
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
80
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
81
82
        $this->assertEquals('a', $opDynamic->getAlias());
83
        $this->assertEquals('somefield', $opDynamic->getField());
84
        $this->assertEquals($expectedValue, $opStatic->getValue());
85
86
        $this->assertTrue($this->filter->isActive());
87
    }
88
}
89