StringFilterTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testFilterNullData() 0 6 1
A testFilterEmptyArrayData() 0 6 1
A testFilterEmptyArrayDataSpecifiedType() 0 6 1
A testFilterEmptyArrayDataWithMeaninglessValue() 0 8 1
A getFilters() 0 51 1
A testFilterSwitch() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Filter;
15
16
use Sonata\DoctrinePHPCRAdminBundle\Filter\StringFilter;
17
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
18
19
class StringFilterTest extends BaseTestCase
20
{
21
    /**
22
     * @var StringFilter
23
     */
24
    private $filter;
25
26
    protected function setUp(): void
27
    {
28
        parent::setUp();
29
        $this->filter = new StringFilter();
30
    }
31
32
    public function testFilterNullData(): void
33
    {
34
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Documentation introduced by
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...
Bug introduced by
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...
35
        $this->assertNull($res);
36
        $this->assertFalse($this->filter->isActive());
37
    }
38
39
    public function testFilterEmptyArrayData(): void
40
    {
41
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', []);
0 ignored issues
show
Bug introduced by
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...
42
        $this->assertNull($res);
43
        $this->assertFalse($this->filter->isActive());
44
    }
45
46
    public function testFilterEmptyArrayDataSpecifiedType(): void
47
    {
48
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL]);
0 ignored issues
show
Bug introduced by
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...
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
49
        $this->assertNull($res);
50
        $this->assertFalse($this->filter->isActive());
51
    }
52
53
    public function testFilterEmptyArrayDataWithMeaninglessValue(): void
54
    {
55
        $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...
56
            ->method('getQueryBuilder');
57
58
        $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => ' ']);
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
59
        $this->assertFalse($this->filter->isActive());
60
    }
61
62
    public function getFilters()
63
    {
64
        return [
65
            [ChoiceType::TYPE_EQUAL, [
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
66
                'where.constraint.operand_dynamic' => [
67
                    'getAlias' => 'a',
68
                    'getField' => 'somefield',
69
                ],
70
                'where.constraint.operand_static' => [
71
                    'getValue' => 'somevalue',
72
                ],
73
            ]],
74
            [ChoiceType::TYPE_NOT_CONTAINS, [
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_NOT_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_NOT_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
75
                'where.constraint' => [
76
                    'getField' => 'somefield',
77
                    'getFullTextSearchExpression' => '* -somevalue', ],
78
            ]],
79
            [ChoiceType::TYPE_CONTAINS, [
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...oiceType::TYPE_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
80
                'where.constraint.operand_dynamic' => [
81
                    'getAlias' => 'a',
82
                    'getField' => 'somefield',
83
                ],
84
                'where.constraint.operand_static' => [
85
                    'getValue' => '%somevalue%',
86
                ],
87
            ]],
88
            [ChoiceType::TYPE_CONTAINS_WORDS, [
89
                'where.constraint' => [
90
                    'getField' => 'somefield',
91
                    'getFullTextSearchExpression' => 'somevalue', ],
92
            ]],
93
            'equalCaseInsensitiveComparision' => [ChoiceType::TYPE_EQUAL, [
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
94
                'where.constraint.operand_dynamic.operand_dynamic' => [
95
                    'getAlias' => 'a',
96
                    'getField' => 'somefield',
97
                ],
98
                'where.constraint.operand_static' => [
99
                    'getValue' => 'somevalue',
100
                ],
101
            ], true],
102
            'containsCaseInsensitiveComparision' => [ChoiceType::TYPE_CONTAINS, [
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...oiceType::TYPE_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
103
                'where.constraint.operand_dynamic.operand_dynamic' => [
104
                    'getAlias' => 'a',
105
                    'getField' => 'somefield',
106
                ],
107
                'where.constraint.operand_static' => [
108
                    'getValue' => '%somevalue%',
109
                ],
110
            ], true],
111
        ];
112
    }
113
114
    /**
115
     * @dataProvider getFilters
116
     */
117
    public function testFilterSwitch($choiceType, $assertPaths, $isLowerCase = false): void
118
    {
119
        if ($isLowerCase) {
120
            $this->filter->setOption('compare_case_insensitive', true);
121
        }
122
        $this->filter->filter(
123
            $this->proxyQuery,
124
            null,
125
            'somefield',
126
            ['type' => $choiceType, 'value' => 'somevalue']
127
        );
128
        $this->assertTrue($this->filter->isActive());
129
130
        foreach ($assertPaths as $path => $assertions) {
131
            $node = $this->qbTester->getNode($path);
132
            foreach ($assertions as $methodName => $expectedValue) {
133
                $res = $node->$methodName();
134
                $this->assertSame($expectedValue, $res);
135
            }
136
        }
137
138
        $this->assertTrue($this->filter->isActive());
139
    }
140
}
141