Completed
Push — master ( 5b1ac4...a1db3b )
by David
17:06
created

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

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\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', []);
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', ['type' => ChoiceType::TYPE_EQUAL]);
42
        $this->assertNull($res);
43
        $this->assertFalse($this->filter->isActive());
44
    }
45
46
    public function getMeaninglessValues()
47
    {
48
        return [
49
            ['  '],
50
            [null],
51
            [false],
52
            ['all'],
53
            [[]],
54
            [['', 'all']],
55
        ];
56
    }
57
58
    /**
59
     * @dataProvider getMeaninglessValues
60
     */
61
    public function testFilterEmptyArrayDataWithMeaninglessValue($value)
62
    {
63
        $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => $value]);
64
        $this->assertFalse($this->filter->isActive());
65
    }
66
67
    public function getFilters()
68
    {
69
        return [
70
            ['eq', ChoiceType::TYPE_EQUAL],
71
            ['textSearch', ChoiceType::TYPE_NOT_CONTAINS, '* -somevalue'],
72
            ['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())
82
            ->method('getQueryBuilder')
83
            ->will($this->returnValue($this->qb));
84
85
        $this->filter->filter(
86
            $this->proxyQuery,
87
            null,
88
            'somefield',
89
            ['type' => $choiceType, 'value' => 'somevalue']
90
        );
91
        $this->assertTrue($this->filter->isActive());
92
    }
93
94
    public function getFiltersMultiple()
95
    {
96
        return [
97
            [[
98
                'choiceType' => ChoiceType::TYPE_NOT_CONTAINS,
99
                'value' => 'somevalue',
100
                'qbNodeCount' => 6,
101
                'assertPaths' => [
102
                    'where.constraint.constraint[0].constraint.operand_dynamic' => [
103
                        'getAlias' => 'a',
104
                        'getField' => 'somefield',
105
                    ],
106
                    'where.constraint.constraint[0].constraint.operand_static' => [
107
                        'getValue' => '%somevalue%',
108
                    ],
109
                ],
110
            ]],
111
            [[
112
                'choiceType' => ChoiceType::TYPE_NOT_CONTAINS,
113
                'value' => ['somevalue', 'somevalue'],
114
                'qbNodeCount' => 10,
115
                'assertPaths' => [
116
                    'where.constraint.constraint.constraint.operand_dynamic' => [
117
                        'getAlias' => 'a',
118
                        'getField' => 'somefield',
119
                    ],
120
                    'where.constraint.constraint.constraint.operand_static' => [
121
                        'getValue' => '%somevalue%',
122
                    ],
123
                    'where.constraint.constraint[1].constraint.operand_dynamic' => [
124
                        'getAlias' => 'a',
125
                        'getField' => 'somefield',
126
                    ],
127
                    'where.constraint.constraint[1].constraint.operand_static' => [
128
                        'getValue' => '%somevalue%',
129
                    ],
130
                ],
131
            ]],
132
            [[
133
                'choiceType' => ChoiceType::TYPE_CONTAINS,
134
                'value' => 'somevalue',
135
                'qbNodeCount' => 5,
136
                'assertPaths' => [
137
                    'where.constraint.constraint.operand_dynamic' => [
138
                        'getAlias' => 'a',
139
                        'getField' => 'somefield',
140
                    ],
141
                    'where.constraint.constraint.operand_static' => [
142
                        'getValue' => '%somevalue%',
143
                    ],
144
                    'where.constraint.constraint.operand_static' => [
145
                        'getValue' => '%somevalue%',
146
                    ],
147
                ],
148
            ]],
149
            [[
150
                'choiceType' => ChoiceType::TYPE_CONTAINS,
151
                'value' => ['somevalue', 'somevalue'],
152
                'qbNodeCount' => 8,
153
                'assertPaths' => [
154
                    'where.constraint.constraint.operand_dynamic' => [
155
                        'getAlias' => 'a',
156
                        'getField' => 'somefield',
157
                    ],
158
                    'where.constraint.constraint.operand_static' => [
159
                        'getValue' => '%somevalue%',
160
                    ],
161
                    'where.constraint.constraint[1].operand_dynamic' => [
162
                        'getAlias' => 'a',
163
                        'getField' => 'somefield',
164
                    ],
165
                    'where.constraint.constraint[1].operand_static' => [
166
                        'getValue' => '%somevalue%',
167
                    ],
168
                ],
169
            ]],
170
            [[
171
                'choiceType' => ChoiceType::TYPE_CONTAINS,
172
                'value' => 'somevalue',
173
                'qbNodeCount' => 5,
174
                'assertPaths' => [
175
                    'where.constraint.constraint.operand_dynamic' => [
176
                        'getAlias' => 'a',
177
                        'getField' => 'somefield',
178
                    ],
179
                    'where.constraint.constraint.operand_static' => [
180
                        'getValue' => '%somevalue%',
181
                    ],
182
                    'where.constraint.constraint.operand_static' => [
183
                        'getValue' => '%somevalue%',
184
                    ],
185
                ],
186
            ]],
187
            [[
188
                'choiceType' => ChoiceType::TYPE_CONTAINS,
189
                'value' => ['somevalue', 'somevalue'],
190
                'qbNodeCount' => 8,
191
                'assertPaths' => [
192
                    'where.constraint.constraint.operand_dynamic' => [
193
                        'getAlias' => 'a',
194
                        'getField' => 'somefield',
195
                    ],
196
                    'where.constraint.constraint.operand_static' => [
197
                        'getValue' => '%somevalue%',
198
                    ],
199
                    'where.constraint.constraint[1].operand_dynamic' => [
200
                        'getAlias' => 'a',
201
                        'getField' => 'somefield',
202
                    ],
203
                    'where.constraint.constraint[1].operand_static' => [
204
                        'getValue' => '%somevalue%',
205
                    ],
206
                ],
207
            ]],
208
            [[
209
                'choiceType' => ChoiceType::TYPE_EQUAL,
210
                'value' => 'somevalue',
211
                'qbNodeCount' => 5,
212
                'assertPaths' => [
213
                    'where.constraint.constraint.operand_dynamic' => [
214
                        'getAlias' => 'a',
215
                        'getField' => 'somefield',
216
                    ],
217
                    'where.constraint.constraint.operand_static' => [
218
                        'getValue' => 'somevalue',
219
                    ],
220
                    'where.constraint.constraint.operand_static' => [
221
                        'getValue' => 'somevalue',
222
                    ],
223
                ],
224
            ]],
225
            [[
226
                'choiceType' => ChoiceType::TYPE_EQUAL,
227
                'value' => ['somevalue', 'somevalue'],
228
                'qbNodeCount' => 8,
229
                'assertPaths' => [
230
                    'where.constraint.constraint.operand_dynamic' => [
231
                        'getAlias' => 'a',
232
                        'getField' => 'somefield',
233
                    ],
234
                    'where.constraint.constraint.operand_static' => [
235
                        'getValue' => 'somevalue',
236
                    ],
237
                    'where.constraint.constraint[1].operand_dynamic' => [
238
                        'getAlias' => 'a',
239
                        'getField' => 'somefield',
240
                    ],
241
                    'where.constraint.constraint[1].operand_static' => [
242
                        'getValue' => 'somevalue',
243
                    ],
244
                ],
245
            ]],
246
        ];
247
    }
248
249
    /**
250
     * @dataProvider getFiltersMultiple
251
     */
252
    public function testFilterMultipleSwitch($options)
253
    {
254
        $options = array_merge([
255
            'choiceType' => null,
256
            'value' => null,
257
            'assertPaths' => [],
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
            ['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