Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

tests/Unit/Filter/NumberFilterTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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();
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')
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