ChoiceFilterTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

10 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 getMeaninglessValues() 0 10 1
A testFilterEmptyArrayDataWithMeaninglessValue() 0 5 1
A getFilters() 0 8 1
A testFilterSwitch() 0 14 1
B getFiltersMultiple() 0 154 1
A testFilterMultipleSwitch() 0 31 3
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\AdminBundle\Form\Type\Filter\ChoiceType;
17
use Sonata\DoctrinePHPCRAdminBundle\Filter\ChoiceFilter;
18
19
class ChoiceFilterTest extends BaseTestCase
20
{
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->filter = new ChoiceFilter();
25
    }
26
27
    public function testFilterNullData(): void
28
    {
29
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', null);
30
        $this->assertNull($res);
31
        $this->assertFalse($this->filter->isActive());
32
    }
33
34
    public function testFilterEmptyArrayData(): void
35
    {
36
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', []);
37
        $this->assertNull($res);
38
        $this->assertFalse($this->filter->isActive());
39
    }
40
41
    public function testFilterEmptyArrayDataSpecifiedType(): void
42
    {
43
        $res = $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => 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...
44
        $this->assertNull($res);
45
        $this->assertFalse($this->filter->isActive());
46
    }
47
48
    public function getMeaninglessValues()
49
    {
50
        return [
51
            ['  '],
52
            [null],
53
            [false],
54
            [[]],
55
            [['', '   ']],
56
        ];
57
    }
58
59
    /**
60
     * @dataProvider getMeaninglessValues
61
     */
62
    public function testFilterEmptyArrayDataWithMeaninglessValue($value): void
63
    {
64
        $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => $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...
65
        $this->assertFalse($this->filter->isActive());
66
    }
67
68
    public function getFilters()
69
    {
70
        return [
71
            ['eq', 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...
72
            ['textSearch', ChoiceType::TYPE_NOT_CONTAINS, '* -somevalue'],
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...
73
            ['like', ChoiceType::TYPE_CONTAINS, '%somevalue%'],
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...
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider getFilters
79
     */
80
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue'): void
81
    {
82
        $this->proxyQuery->expects($this->once())
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...
83
            ->method('getQueryBuilder')
84
            ->willReturn($this->qb);
85
86
        $this->filter->filter(
87
            $this->proxyQuery,
88
            null,
89
            'somefield',
90
            ['type' => $choiceType, 'value' => 'somevalue']
91
        );
92
        $this->assertTrue($this->filter->isActive());
93
    }
94
95
    public function getFiltersMultiple()
96
    {
97
        return [
98
            [[
99
                'choiceType' => 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...
100
                'value' => 'somevalue',
101
                'qbNodeCount' => 6,
102
                'assertPaths' => [
103
                    'where.constraint.constraint[0].constraint.operand_dynamic' => [
104
                        'getAlias' => 'a',
105
                        'getField' => 'somefield',
106
                    ],
107
                    'where.constraint.constraint[0].constraint.operand_static' => [
108
                        'getValue' => '%somevalue%',
109
                    ],
110
                ],
111
            ]],
112
            [[
113
                'choiceType' => 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...
114
                'value' => ['somevalue', 'somevalue'],
115
                'qbNodeCount' => 10,
116
                'assertPaths' => [
117
                    'where.constraint.constraint.constraint.operand_dynamic' => [
118
                        'getAlias' => 'a',
119
                        'getField' => 'somefield',
120
                    ],
121
                    'where.constraint.constraint.constraint.operand_static' => [
122
                        'getValue' => '%somevalue%',
123
                    ],
124
                    'where.constraint.constraint[1].constraint.operand_dynamic' => [
125
                        'getAlias' => 'a',
126
                        'getField' => 'somefield',
127
                    ],
128
                    'where.constraint.constraint[1].constraint.operand_static' => [
129
                        'getValue' => '%somevalue%',
130
                    ],
131
                ],
132
            ]],
133
            [[
134
                'choiceType' => 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...
135
                'value' => 'somevalue',
136
                'qbNodeCount' => 5,
137
                'assertPaths' => [
138
                    'where.constraint.constraint.operand_dynamic' => [
139
                        'getAlias' => 'a',
140
                        'getField' => 'somefield',
141
                    ],
142
                    'where.constraint.constraint.operand_static' => [
143
                        'getValue' => '%somevalue%',
144
                    ],
145
                    'where.constraint.constraint.operand_static' => [
146
                        'getValue' => '%somevalue%',
147
                    ],
148
                ],
149
            ]],
150
            [[
151
                'choiceType' => 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...
152
                'value' => ['somevalue', 'somevalue'],
153
                'qbNodeCount' => 8,
154
                'assertPaths' => [
155
                    'where.constraint.constraint.operand_dynamic' => [
156
                        'getAlias' => 'a',
157
                        'getField' => 'somefield',
158
                    ],
159
                    'where.constraint.constraint.operand_static' => [
160
                        'getValue' => '%somevalue%',
161
                    ],
162
                    'where.constraint.constraint[1].operand_dynamic' => [
163
                        'getAlias' => 'a',
164
                        'getField' => 'somefield',
165
                    ],
166
                    'where.constraint.constraint[1].operand_static' => [
167
                        'getValue' => '%somevalue%',
168
                    ],
169
                ],
170
            ]],
171
            [[
172
                'choiceType' => 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...
173
                'value' => 'somevalue',
174
                'qbNodeCount' => 5,
175
                'assertPaths' => [
176
                    'where.constraint.constraint.operand_dynamic' => [
177
                        'getAlias' => 'a',
178
                        'getField' => 'somefield',
179
                    ],
180
                    'where.constraint.constraint.operand_static' => [
181
                        'getValue' => '%somevalue%',
182
                    ],
183
                    'where.constraint.constraint.operand_static' => [
184
                        'getValue' => '%somevalue%',
185
                    ],
186
                ],
187
            ]],
188
            [[
189
                'choiceType' => 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...
190
                'value' => ['somevalue', 'somevalue'],
191
                'qbNodeCount' => 8,
192
                'assertPaths' => [
193
                    'where.constraint.constraint.operand_dynamic' => [
194
                        'getAlias' => 'a',
195
                        'getField' => 'somefield',
196
                    ],
197
                    'where.constraint.constraint.operand_static' => [
198
                        'getValue' => '%somevalue%',
199
                    ],
200
                    'where.constraint.constraint[1].operand_dynamic' => [
201
                        'getAlias' => 'a',
202
                        'getField' => 'somefield',
203
                    ],
204
                    'where.constraint.constraint[1].operand_static' => [
205
                        'getValue' => '%somevalue%',
206
                    ],
207
                ],
208
            ]],
209
            [[
210
                'choiceType' => 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...
211
                'value' => 'somevalue',
212
                'qbNodeCount' => 5,
213
                'assertPaths' => [
214
                    'where.constraint.constraint.operand_dynamic' => [
215
                        'getAlias' => 'a',
216
                        'getField' => 'somefield',
217
                    ],
218
                    'where.constraint.constraint.operand_static' => [
219
                        'getValue' => 'somevalue',
220
                    ],
221
                    'where.constraint.constraint.operand_static' => [
222
                        'getValue' => 'somevalue',
223
                    ],
224
                ],
225
            ]],
226
            [[
227
                'choiceType' => 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...
228
                'value' => ['somevalue', 'somevalue'],
229
                'qbNodeCount' => 8,
230
                'assertPaths' => [
231
                    'where.constraint.constraint.operand_dynamic' => [
232
                        'getAlias' => 'a',
233
                        'getField' => 'somefield',
234
                    ],
235
                    'where.constraint.constraint.operand_static' => [
236
                        'getValue' => 'somevalue',
237
                    ],
238
                    'where.constraint.constraint[1].operand_dynamic' => [
239
                        'getAlias' => 'a',
240
                        'getField' => 'somefield',
241
                    ],
242
                    'where.constraint.constraint[1].operand_static' => [
243
                        'getValue' => 'somevalue',
244
                    ],
245
                ],
246
            ]],
247
        ];
248
    }
249
250
    /**
251
     * @dataProvider getFiltersMultiple
252
     */
253
    public function testFilterMultipleSwitch($options): void
254
    {
255
        $options = array_merge([
256
            'choiceType' => null,
257
            'value' => null,
258
            'assertPaths' => [],
259
            'qbNodeCount' => 0,
260
        ], $options);
261
262
        $this->proxyQuery->expects($this->once())
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...
263
            ->method('getQueryBuilder')
264
            ->willReturn($this->qb);
265
266
        $this->filter->filter(
267
            $this->proxyQuery,
268
            null,
269
            'somefield',
270
            ['type' => $options['choiceType'], 'value' => $options['value']]
271
        );
272
273
        foreach ($options['assertPaths'] as $path => $methodAssertions) {
0 ignored issues
show
Bug introduced by
The expression $options['assertPaths'] of type null|array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
274
            $node = $this->qbTester->getNode($path);
275
            foreach ($methodAssertions as $methodName => $expectedValue) {
276
                $res = $node->$methodName();
277
                $this->assertSame($expectedValue, $res);
278
            }
279
        }
280
281
        $this->assertTrue($this->filter->isActive());
282
        $this->assertSame($options['qbNodeCount'], \count($this->qbTester->getAllNodes()));
283
    }
284
}
285