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

StringFilterTest::testFilterSwitch()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 3
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\DoctrinePHPCRAdminBundle\Filter\StringFilter;
15
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
16
17
class StringFilterTest extends BaseTestCase
18
{
19
    /**
20
     * @var StringFilter
21
     */
22
    private $filter;
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        $this->filter = new StringFilter();
28
    }
29
30
    public function testFilterNullData()
31
    {
32
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Bug introduced by Roel Sint
Are you sure the assignment to $res is correct as $this->filter->filter($t...ull, 'somefield', null) (which targets Sonata\DoctrinePHPCRAdmi...\StringFilter::filter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33
        $this->assertNull($res);
34
        $this->assertFalse($this->filter->isActive());
35
    }
36
37
    public function testFilterEmptyArrayData()
38
    {
39
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', array());
0 ignored issues
show
Documentation introduced by Roel Sint
array() is of type array, 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...
Bug introduced by Roel Sint
Are you sure the assignment to $res is correct as $this->filter->filter($t..., 'somefield', array()) (which targets Sonata\DoctrinePHPCRAdmi...\StringFilter::filter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
        $this->assertNull($res);
41
        $this->assertFalse($this->filter->isActive());
42
    }
43
44
    public function testFilterEmptyArrayDataSpecifiedType()
45
    {
46
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', array('type' => ChoiceType::TYPE_EQUAL));
0 ignored issues
show
Documentation introduced by Roel Sint
array('type' => \Sonata\...ChoiceType::TYPE_EQUAL) is of type array<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...
Bug introduced by Roel Sint
Are you sure the assignment to $res is correct as $this->filter->filter($t...hoiceType::TYPE_EQUAL)) (which targets Sonata\DoctrinePHPCRAdmi...\StringFilter::filter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
        $this->assertNull($res);
48
        $this->assertFalse($this->filter->isActive());
49
    }
50
51
    public function testFilterEmptyArrayDataWithMeaninglessValue()
52
    {
53
        $this->proxyQuery->expects($this->never())
0 ignored issues
show
Documentation Bug introduced by dantleech
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...
54
            ->method('getQueryBuilder');
55
56
        $this->filter->filter($this->proxyQuery, null, 'somefield', array('type' => ChoiceType::TYPE_EQUAL, 'value' => ' '));
0 ignored issues
show
Documentation introduced by Roel Sint
array('type' => \Sonata\..._EQUAL, 'value' => ' ') is of type array<string,string,{"value":"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...
57
        $this->assertFalse($this->filter->isActive());
58
    }
59
60
    public function getFilters()
61
    {
62
        return array(
63
            array(ChoiceType::TYPE_EQUAL, array(
64
                'where.constraint.operand_dynamic' => array(
65
                    'getAlias' => 'a',
66
                    'getField' => 'somefield',
67
                ),
68
                'where.constraint.operand_static' => array(
69
                    'getValue' => 'somevalue',
70
                ),
71
            )),
72
            array(ChoiceType::TYPE_NOT_CONTAINS, array(
73
                'where.constraint' => array(
74
                    'getField' => 'somefield',
75
                    'getFullTextSearchExpression' => '* -somevalue', ),
76
            )),
77
            array(ChoiceType::TYPE_CONTAINS, array(
78
                'where.constraint.operand_dynamic' => array(
79
                    'getAlias' => 'a',
80
                    'getField' => 'somefield',
81
                ),
82
                'where.constraint.operand_static' => array(
83
                    'getValue' => '%somevalue%',
84
                ),
85
            )),
86
            array(ChoiceType::TYPE_CONTAINS_WORDS, array(
87
                'where.constraint' => array(
88
                    'getField' => 'somefield',
89
                    'getFullTextSearchExpression' => 'somevalue', ),
90
            )),
91
            'equalCaseInsensitiveComparision' => array(ChoiceType::TYPE_EQUAL, array(
92
                'where.constraint.operand_dynamic.operand_dynamic' => array(
93
                    'getAlias' => 'a',
94
                    'getField' => 'somefield',
95
                ),
96
                'where.constraint.operand_static' => array(
97
                    'getValue' => 'somevalue',
98
                ),
99
            ), true),
100
            'containsCaseInsensitiveComparision' => array(ChoiceType::TYPE_CONTAINS, array(
101
                'where.constraint.operand_dynamic.operand_dynamic' => array(
102
                    'getAlias' => 'a',
103
                    'getField' => 'somefield',
104
                ),
105
                'where.constraint.operand_static' => array(
106
                    'getValue' => '%somevalue%',
107
                ),
108
            ), true),
109
        );
110
    }
111
112
    /**
113
     * @dataProvider getFilters
114
     */
115
    public function testFilterSwitch($choiceType, $assertPaths, $isLowerCase = false)
116
    {
117
        if ($isLowerCase) {
118
            $this->filter->setOption('compare_case_insensitive', true);
119
        }
120
        $this->filter->filter(
121
            $this->proxyQuery,
122
            null,
123
            'somefield',
124
            array('type' => $choiceType, 'value' => 'somevalue')
0 ignored issues
show
Documentation introduced by dantleech
array('type' => $choiceT...'value' => 'somevalue') is of type array<string,?,{"type":"?","value":"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...
125
        );
126
        $this->assertTrue($this->filter->isActive());
127
128
        foreach ($assertPaths as $path => $assertions) {
129
            $node = $this->qbTester->getNode($path);
130
            foreach ($assertions as $methodName => $expectedValue) {
131
                $res = $node->$methodName();
132
                $this->assertEquals($expectedValue, $res);
133
            }
134
        }
135
136
        $this->assertTrue($this->filter->isActive());
137
    }
138
}
139