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

ChoiceFilterTest::testFilterMultipleSwitch()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 1
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\ChoiceType;
15
use Sonata\DoctrinePHPCRAdminBundle\Filter\ChoiceFilter;
16
17
class ChoiceFilterTest extends BaseTestCase
18
{
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        $this->filter = new ChoiceFilter();
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' => ChoiceType::TYPE_EQUAL));
42
        $this->assertNull($res);
43
        $this->assertFalse($this->filter->isActive());
44
    }
45
46
    public function getMeaninglessValues()
47
    {
48
        return array(
49
            array('  '),
50
            array(null),
51
            array(false),
52
            array('all'),
53
            array(array()),
54
            array(array('', 'all')),
55
        );
56
    }
57
58
    /**
59
     * @dataProvider getMeaninglessValues
60
     */
61
    public function testFilterEmptyArrayDataWithMeaninglessValue($value)
62
    {
63
        $this->filter->filter($this->proxyQuery, null, 'somefield', array('type' => ChoiceType::TYPE_EQUAL, 'value' => $value));
64
        $this->assertFalse($this->filter->isActive());
65
    }
66
67
    public function getFilters()
68
    {
69
        return array(
70
            array('eq', ChoiceType::TYPE_EQUAL),
71
            array('textSearch', ChoiceType::TYPE_NOT_CONTAINS, '* -somevalue'),
72
            array('like', ChoiceType::TYPE_CONTAINS, '%somevalue%'),
73
        );
74
    }
75
76
    /**
77
     * @dataProvider getFilters
78
     */
79
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue')
80
    {
81
        $this->proxyQuery->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by Roel Sint
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...
82
            ->method('getQueryBuilder')
83
            ->will($this->returnValue($this->qb));
84
85
        $this->filter->filter(
86
            $this->proxyQuery,
87
            null,
88
            'somefield',
89
            array('type' => $choiceType, 'value' => 'somevalue')
90
        );
91
        $this->assertTrue($this->filter->isActive());
92
    }
93
94
    public function getFiltersMultiple()
95
    {
96
        return array(
97
            array(array(
98
                'choiceType' => ChoiceType::TYPE_NOT_CONTAINS,
99
                'value' => 'somevalue',
100
                'qbNodeCount' => 6,
101
                'assertPaths' => array(
102
                    'where.constraint.constraint[0].constraint.operand_dynamic' => array(
103
                        'getAlias' => 'a',
104
                        'getField' => 'somefield',
105
                    ),
106
                    'where.constraint.constraint[0].constraint.operand_static' => array(
107
                        'getValue' => '%somevalue%',
108
                    ),
109
                ),
110
            )),
111
            array(array(
112
                'choiceType' => ChoiceType::TYPE_NOT_CONTAINS,
113
                'value' => array('somevalue', 'somevalue'),
114
                'qbNodeCount' => 10,
115
                'assertPaths' => array(
116
                    'where.constraint.constraint.constraint.operand_dynamic' => array(
117
                        'getAlias' => 'a',
118
                        'getField' => 'somefield',
119
                    ),
120
                    'where.constraint.constraint.constraint.operand_static' => array(
121
                        'getValue' => '%somevalue%',
122
                    ),
123
                    'where.constraint.constraint[1].constraint.operand_dynamic' => array(
124
                        'getAlias' => 'a',
125
                        'getField' => 'somefield',
126
                    ),
127
                    'where.constraint.constraint[1].constraint.operand_static' => array(
128
                        'getValue' => '%somevalue%',
129
                    ),
130
                ),
131
            )),
132
            array(array(
133
                'choiceType' => ChoiceType::TYPE_CONTAINS,
134
                'value' => 'somevalue',
135
                'qbNodeCount' => 5,
136
                'assertPaths' => array(
137
                    'where.constraint.constraint.operand_dynamic' => array(
138
                        'getAlias' => 'a',
139
                        'getField' => 'somefield',
140
                    ),
141
                    'where.constraint.constraint.operand_static' => array(
142
                        'getValue' => '%somevalue%',
143
                    ),
144
                    'where.constraint.constraint.operand_static' => array(
145
                        'getValue' => '%somevalue%',
146
                    ),
147
                ),
148
            )),
149
            array(array(
150
                'choiceType' => ChoiceType::TYPE_CONTAINS,
151
                'value' => array('somevalue', 'somevalue'),
152
                'qbNodeCount' => 8,
153
                'assertPaths' => array(
154
                    'where.constraint.constraint.operand_dynamic' => array(
155
                        'getAlias' => 'a',
156
                        'getField' => 'somefield',
157
                    ),
158
                    'where.constraint.constraint.operand_static' => array(
159
                        'getValue' => '%somevalue%',
160
                    ),
161
                    'where.constraint.constraint[1].operand_dynamic' => array(
162
                        'getAlias' => 'a',
163
                        'getField' => 'somefield',
164
                    ),
165
                    'where.constraint.constraint[1].operand_static' => array(
166
                        'getValue' => '%somevalue%',
167
                    ),
168
                ),
169
            )),
170
            array(array(
171
                'choiceType' => ChoiceType::TYPE_CONTAINS,
172
                'value' => 'somevalue',
173
                'qbNodeCount' => 5,
174
                'assertPaths' => array(
175
                    'where.constraint.constraint.operand_dynamic' => array(
176
                        'getAlias' => 'a',
177
                        'getField' => 'somefield',
178
                    ),
179
                    'where.constraint.constraint.operand_static' => array(
180
                        'getValue' => '%somevalue%',
181
                    ),
182
                    'where.constraint.constraint.operand_static' => array(
183
                        'getValue' => '%somevalue%',
184
                    ),
185
                ),
186
            )),
187
            array(array(
188
                'choiceType' => ChoiceType::TYPE_CONTAINS,
189
                'value' => array('somevalue', 'somevalue'),
190
                'qbNodeCount' => 8,
191
                'assertPaths' => array(
192
                    'where.constraint.constraint.operand_dynamic' => array(
193
                        'getAlias' => 'a',
194
                        'getField' => 'somefield',
195
                    ),
196
                    'where.constraint.constraint.operand_static' => array(
197
                        'getValue' => '%somevalue%',
198
                    ),
199
                    'where.constraint.constraint[1].operand_dynamic' => array(
200
                        'getAlias' => 'a',
201
                        'getField' => 'somefield',
202
                    ),
203
                    'where.constraint.constraint[1].operand_static' => array(
204
                        'getValue' => '%somevalue%',
205
                    ),
206
                ),
207
            )),
208
            array(array(
209
                'choiceType' => ChoiceType::TYPE_EQUAL,
210
                'value' => 'somevalue',
211
                'qbNodeCount' => 5,
212
                'assertPaths' => array(
213
                    'where.constraint.constraint.operand_dynamic' => array(
214
                        'getAlias' => 'a',
215
                        'getField' => 'somefield',
216
                    ),
217
                    'where.constraint.constraint.operand_static' => array(
218
                        'getValue' => 'somevalue',
219
                    ),
220
                    'where.constraint.constraint.operand_static' => array(
221
                        'getValue' => 'somevalue',
222
                    ),
223
                ),
224
            )),
225
            array(array(
226
                'choiceType' => ChoiceType::TYPE_EQUAL,
227
                'value' => array('somevalue', 'somevalue'),
228
                'qbNodeCount' => 8,
229
                'assertPaths' => array(
230
                    'where.constraint.constraint.operand_dynamic' => array(
231
                        'getAlias' => 'a',
232
                        'getField' => 'somefield',
233
                    ),
234
                    'where.constraint.constraint.operand_static' => array(
235
                        'getValue' => 'somevalue',
236
                    ),
237
                    'where.constraint.constraint[1].operand_dynamic' => array(
238
                        'getAlias' => 'a',
239
                        'getField' => 'somefield',
240
                    ),
241
                    'where.constraint.constraint[1].operand_static' => array(
242
                        'getValue' => 'somevalue',
243
                    ),
244
                ),
245
            )),
246
        );
247
    }
248
249
    /**
250
     * @dataProvider getFiltersMultiple
251
     */
252
    public function testFilterMultipleSwitch($options)
253
    {
254
        $options = array_merge(array(
255
            'choiceType' => null,
256
            'value' => null,
257
            'assertPaths' => array(),
258
            'qbNodeCount' => 0,
259
        ), $options);
260
261
        $this->proxyQuery->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by Roel Sint
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...
262
            ->method('getQueryBuilder')
263
            ->will($this->returnValue($this->qb));
264
265
        $this->filter->filter(
266
            $this->proxyQuery,
267
            null,
268
            'somefield',
269
            array('type' => $options['choiceType'], 'value' => $options['value'])
270
        );
271
272
        foreach ($options['assertPaths'] as $path => $methodAssertions) {
0 ignored issues
show
Bug introduced by dantleech
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...
273
            $node = $this->qbTester->getNode($path);
274
            foreach ($methodAssertions as $methodName => $expectedValue) {
275
                $res = $node->$methodName();
276
                $this->assertEquals($expectedValue, $res);
277
            }
278
        }
279
280
        $this->assertTrue($this->filter->isActive());
281
        $this->assertEquals($options['qbNodeCount'], count($this->qbTester->getAllNodes()));
282
    }
283
}
284