Passed
Push — master ( 019716...f69765 )
by Alexander
07:16
created

FilterTest::filterDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 129
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 74
c 3
b 0
f 1
dl 0
loc 129
rs 8.5672
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Tests\Reader;
6
7
use Yiisoft\Data\Reader\Filter\All;
8
use Yiisoft\Data\Reader\Filter\Any;
9
use Yiisoft\Data\Reader\Filter\FilterInterface;
10
use Yiisoft\Data\Reader\Filter\Equals;
11
use Yiisoft\Data\Reader\Filter\GreaterThan;
12
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
13
use Yiisoft\Data\Reader\Filter\In;
14
use Yiisoft\Data\Reader\Filter\LessThan;
15
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
16
use Yiisoft\Data\Reader\Filter\Like;
17
use Yiisoft\Data\Reader\Filter\Not;
18
use Yiisoft\Data\Tests\TestCase;
19
20
final class FilterTest extends TestCase
21
{
22
    public function filterDataProvider(): array
23
    {
24
        return [
25
            'Equals' => [
26
                new Equals('test', 42),
27
                ['=', 'test', 42],
28
            ],
29
            'In' => [
30
                new In('test', [1, 2, 3]),
31
                ['in', 'test', [1, 2, 3]],
32
            ],
33
            'GreaterThan' => [
34
                new GreaterThan('test', 42),
35
                ['>', 'test', 42],
36
            ],
37
            'GreaterThanOrEqual' => [
38
                new GreaterThanOrEqual('test', 42),
39
                ['>=', 'test', 42],
40
            ],
41
            'LessThan' => [
42
                new LessThan('test', 42),
43
                ['<', 'test', 42],
44
            ],
45
            'LessThanOrEqual' => [
46
                new LessThanOrEqual('test', 42),
47
                ['<=', 'test', 42],
48
            ],
49
            'Like' => [
50
                new Like('test', '42'),
51
                ['like', 'test', '42'],
52
            ],
53
            'Not' => [
54
                new Not(new Equals('test', 42)),
55
                ['not', ['=', 'test', 42]],
56
            ],
57
            'Any' => [
58
                new Any(
59
                    new Equals('test', 1),
60
                    new Equals('test', 2)
61
                ),
62
                ['or', [
63
                    ['=', 'test', 1],
64
                    ['=', 'test', 2],
65
                ]]
66
            ],
67
            'All' => [
68
                new All(
69
                    new LessThan('test', 3),
70
                    new GreaterThan('test', 2)
71
                ),
72
                ['and', [
73
                    ['<', 'test', 3],
74
                    ['>', 'test', 2],
75
                ]]
76
            ],
77
            'NestedGroup' => [
78
                new All(
79
                    new Equals('test', 42),
80
                    new Any(
81
                        new Equals('test', 1),
82
                        new Equals('test', 2)
83
                    )
84
                ),
85
                [
86
                    'and',
87
                    [
88
                        ['=', 'test', 42],
89
                        [
90
                            'or',
91
                            [
92
                                ['=', 'test', 1],
93
                                ['=', 'test', 2],
94
                            ]
95
                        ]
96
                    ]
97
                ]
98
            ],
99
            'withFiltersArrayAll' => [
100
                (new All())->withFiltersArray([
101
                    ['<', 'test', 3],
102
                ]),
103
                [
104
                    'and',
105
                    [
106
                        ['<', 'test', 3],
107
                    ],
108
                ]
109
            ],
110
            'withFiltersArrayAny' => [
111
                (new Any())->withFiltersArray([
112
                    ['<', 'test1', 3],
113
                    new Equals('test2', 33),
114
                ]),
115
                [
116
                    'or',
117
                    [
118
                        ['<', 'test1', 3],
119
                        ['=', 'test2', 33],
120
                    ],
121
                ],
122
            ],
123
            'withFiltersArrayNestedGroup' => [
124
                (new All())->withFiltersArray([
125
                    ['<', 'test', 3],
126
                    [
127
                        'and',
128
                        [
129
                            ['>', 'test2', 99],
130
                        ]
131
                    ],
132
                ]),
133
                [
134
                    'and',
135
                    [
136
                        ['<', 'test', 3],
137
                        [
138
                            'and',
139
                            [
140
                                ['>', 'test2', 99],
141
                            ],
142
                        ],
143
                    ],
144
                ],
145
            ],
146
            'withFiltersArrayEmpty' => [
147
                (new All())->withFiltersArray([]),
148
                [
149
                    'and',
150
                    [],
151
                ],
152
            ]
153
        ];
154
    }
155
156
    /**
157
     * @dataProvider filterDataProvider
158
     */
159
    public function testFilter(FilterInterface $filter, array $filterArray): void
160
    {
161
        $this->assertSame($filterArray, $filter->toArray());
162
    }
163
164
    /**
165
     * @dataProvider arrayFailDataProvider
166
     */
167
    public function testWithFiltersArrayFail(array $filtersArray): void
168
    {
169
        $this->expectException(\InvalidArgumentException::class);
170
        (new All())->withFiltersArray($filtersArray);
171
    }
172
173
    public function arrayFailDataProvider(): array
174
    {
175
        return [
176
            'filterIsNotArray' => [
177
                ['test'],
178
            ],
179
            'emptyFilterArray' => [
180
                [[]],
181
            ],
182
            'operatorFailInvalidType' => [
183
                [[false]],
184
            ],
185
            'operatorFailWithEmptyString' => [
186
                [['']],
187
            ],
188
        ];
189
    }
190
}
191