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

tests/Filter/ChoiceFilterTest.php (3 issues)

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\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();
0 ignored issues
show
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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')
0 ignored issues
show
The parameter $operatorMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $expectedValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        $this->proxyQuery->expects($this->once())
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())
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) {
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