Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

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

Severity

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\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
null is of type null, but the function expects a array.

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...
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', []);
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', ['type' => ChoiceType::TYPE_EQUAL]);
47
        $this->assertNull($res);
48
        $this->assertFalse($this->filter->isActive());
49
    }
50
51
    public function testFilterEmptyArrayDataWithMeaninglessValue()
52
    {
53
        $this->proxyQuery->expects($this->never())
54
            ->method('getQueryBuilder');
55
56
        $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => ' ']);
57
        $this->assertFalse($this->filter->isActive());
58
    }
59
60
    public function getFilters()
61
    {
62
        return [
63
            [ChoiceType::TYPE_EQUAL, [
64
                'where.constraint.operand_dynamic' => [
65
                    'getAlias' => 'a',
66
                    'getField' => 'somefield',
67
                ],
68
                'where.constraint.operand_static' => [
69
                    'getValue' => 'somevalue',
70
                ],
71
            ]],
72
            [ChoiceType::TYPE_NOT_CONTAINS, [
73
                'where.constraint' => [
74
                    'getField' => 'somefield',
75
                    'getFullTextSearchExpression' => '* -somevalue', ],
76
            ]],
77
            [ChoiceType::TYPE_CONTAINS, [
78
                'where.constraint.operand_dynamic' => [
79
                    'getAlias' => 'a',
80
                    'getField' => 'somefield',
81
                ],
82
                'where.constraint.operand_static' => [
83
                    'getValue' => '%somevalue%',
84
                ],
85
            ]],
86
            [ChoiceType::TYPE_CONTAINS_WORDS, [
87
                'where.constraint' => [
88
                    'getField' => 'somefield',
89
                    'getFullTextSearchExpression' => 'somevalue', ],
90
            ]],
91
            'equalCaseInsensitiveComparision' => [ChoiceType::TYPE_EQUAL, [
92
                'where.constraint.operand_dynamic.operand_dynamic' => [
93
                    'getAlias' => 'a',
94
                    'getField' => 'somefield',
95
                ],
96
                'where.constraint.operand_static' => [
97
                    'getValue' => 'somevalue',
98
                ],
99
            ], true],
100
            'containsCaseInsensitiveComparision' => [ChoiceType::TYPE_CONTAINS, [
101
                'where.constraint.operand_dynamic.operand_dynamic' => [
102
                    'getAlias' => 'a',
103
                    'getField' => 'somefield',
104
                ],
105
                'where.constraint.operand_static' => [
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
            ['type' => $choiceType, 'value' => 'somevalue']
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