Passed
Push — master ( 7dffb9...f06de2 )
by Alexander
02:41 queued 10s
created

FilterTest::testWithFiltersArrayFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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