Completed
Push — master ( 77087e...43819a )
by Sergei
22s queued 15s
created

ExpressionBuilderTest::provideDataForOr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 38
rs 9.6333
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Query\Expression;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Query\Expression\CompositeExpression;
9
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
10
use Doctrine\Tests\DbalTestCase;
11
12
/**
13
 * @group DBAL-12
14
 */
15
class ExpressionBuilderTest extends DbalTestCase
16
{
17
    /** @var ExpressionBuilder */
18
    protected $expr;
19
20
    protected function setUp() : void
21
    {
22
        $conn = $this->createMock(Connection::class);
23
24
        $this->expr = new ExpressionBuilder($conn);
25
26
        $conn->expects($this->any())
27
             ->method('getExpressionBuilder')
28
             ->will($this->returnValue($this->expr));
29
    }
30
31
    /**
32
     * @param string[]|CompositeExpression[] $parts
33
     *
34
     * @dataProvider provideDataForAnd
35
     */
36
    public function testAnd(array $parts, string $expected) : void
37
    {
38
        $composite = $this->expr->and(...$parts);
39
40
        self::assertEquals($expected, (string) $composite);
41
    }
42
43
    /**
44
     * @param string[]|CompositeExpression[] $parts
45
     *
46
     * @dataProvider provideDataForAnd
47
     */
48
    public function testAndX(array $parts, string $expected) : void
49
    {
50
        $composite = $this->expr->andX();
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...pressionBuilder::andX() has been deprecated: Use `and()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
        $composite = /** @scrutinizer ignore-deprecated */ $this->expr->andX();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
52
        foreach ($parts as $part) {
53
            $composite->add($part);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...positeExpression::add() has been deprecated: This class will be made immutable. Use with() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
            /** @scrutinizer ignore-deprecated */ $composite->add($part);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
54
        }
55
56
        self::assertEquals($expected, (string) $composite);
57
    }
58
59
    /**
60
     * @return mixed[][]
61
     */
62
    public static function provideDataForAnd() : iterable
63
    {
64
        return [
65
            [
66
                ['u.user = 1'],
67
                'u.user = 1',
68
            ],
69
            [
70
                ['u.user = 1', 'u.group_id = 1'],
71
                '(u.user = 1) AND (u.group_id = 1)',
72
            ],
73
            [
74
                ['u.user = 1'],
75
                'u.user = 1',
76
            ],
77
            [
78
                ['u.group_id = 1', 'u.group_id = 2'],
79
                '(u.group_id = 1) AND (u.group_id = 2)',
80
            ],
81
            [
82
                [
83
                    'u.user = 1',
84
                    new CompositeExpression(
85
                        CompositeExpression::TYPE_OR,
86
                        ['u.group_id = 1', 'u.group_id = 2']
87
                    ),
88
                ],
89
                '(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
90
            ],
91
            [
92
                [
93
                    'u.group_id = 1',
94
                    new CompositeExpression(
95
                        CompositeExpression::TYPE_AND,
96
                        ['u.user = 1', 'u.group_id = 2']
97
                    ),
98
                ],
99
                '(u.group_id = 1) AND ((u.user = 1) AND (u.group_id = 2))',
100
            ],
101
        ];
102
    }
103
104
    /**
105
     * @param string[]|CompositeExpression[] $parts
106
     *
107
     * @dataProvider provideDataForOr
108
     */
109
    public function testOr(array $parts, string $expected) : void
110
    {
111
        $composite = $this->expr->or(...$parts);
112
113
        self::assertEquals($expected, (string) $composite);
114
    }
115
116
    /**
117
     * @param string[]|CompositeExpression[] $parts
118
     *
119
     * @dataProvider provideDataForOr
120
     */
121
    public function testOrX(array $parts, string $expected) : void
122
    {
123
        $composite = $this->expr->orX();
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...xpressionBuilder::orX() has been deprecated: Use `or()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
        $composite = /** @scrutinizer ignore-deprecated */ $this->expr->orX();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
124
125
        foreach ($parts as $part) {
126
            $composite->add($part);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...positeExpression::add() has been deprecated: This class will be made immutable. Use with() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

126
            /** @scrutinizer ignore-deprecated */ $composite->add($part);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
127
        }
128
129
        self::assertEquals($expected, (string) $composite);
130
    }
131
132
    /**
133
     * @return mixed[][]
134
     */
135
    public static function provideDataForOr() : iterable
136
    {
137
        return [
138
            [
139
                ['u.user = 1'],
140
                'u.user = 1',
141
            ],
142
            [
143
                ['u.user = 1', 'u.group_id = 1'],
144
                '(u.user = 1) OR (u.group_id = 1)',
145
            ],
146
            [
147
                ['u.user = 1'],
148
                'u.user = 1',
149
            ],
150
            [
151
                ['u.group_id = 1', 'u.group_id = 2'],
152
                '(u.group_id = 1) OR (u.group_id = 2)',
153
            ],
154
            [
155
                [
156
                    'u.user = 1',
157
                    new CompositeExpression(
158
                        CompositeExpression::TYPE_OR,
159
                        ['u.group_id = 1', 'u.group_id = 2']
160
                    ),
161
                ],
162
                '(u.user = 1) OR ((u.group_id = 1) OR (u.group_id = 2))',
163
            ],
164
            [
165
                [
166
                    'u.group_id = 1',
167
                    new CompositeExpression(
168
                        CompositeExpression::TYPE_AND,
169
                        ['u.user = 1', 'u.group_id = 2']
170
                    ),
171
                ],
172
                '(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
173
            ],
174
        ];
175
    }
176
177
    /**
178
     * @dataProvider provideDataForComparison
179
     */
180
    public function testComparison(string $leftExpr, string $operator, string $rightExpr, string $expected) : void
181
    {
182
        $part = $this->expr->comparison($leftExpr, $operator, $rightExpr);
183
184
        self::assertEquals($expected, (string) $part);
185
    }
186
187
    /**
188
     * @return mixed[][]
189
     */
190
    public static function provideDataForComparison() : iterable
191
    {
192
        return [
193
            ['u.user_id', ExpressionBuilder::EQ, '1', 'u.user_id = 1'],
194
            ['u.user_id', ExpressionBuilder::NEQ, '1', 'u.user_id <> 1'],
195
            ['u.salary', ExpressionBuilder::LT, '10000', 'u.salary < 10000'],
196
            ['u.salary', ExpressionBuilder::LTE, '10000', 'u.salary <= 10000'],
197
            ['u.salary', ExpressionBuilder::GT, '10000', 'u.salary > 10000'],
198
            ['u.salary', ExpressionBuilder::GTE, '10000', 'u.salary >= 10000'],
199
        ];
200
    }
201
202
    public function testEq() : void
203
    {
204
        self::assertEquals('u.user_id = 1', $this->expr->eq('u.user_id', '1'));
205
    }
206
207
    public function testNeq() : void
208
    {
209
        self::assertEquals('u.user_id <> 1', $this->expr->neq('u.user_id', '1'));
210
    }
211
212
    public function testLt() : void
213
    {
214
        self::assertEquals('u.salary < 10000', $this->expr->lt('u.salary', '10000'));
215
    }
216
217
    public function testLte() : void
218
    {
219
        self::assertEquals('u.salary <= 10000', $this->expr->lte('u.salary', '10000'));
220
    }
221
222
    public function testGt() : void
223
    {
224
        self::assertEquals('u.salary > 10000', $this->expr->gt('u.salary', '10000'));
225
    }
226
227
    public function testGte() : void
228
    {
229
        self::assertEquals('u.salary >= 10000', $this->expr->gte('u.salary', '10000'));
230
    }
231
232
    public function testIsNull() : void
233
    {
234
        self::assertEquals('u.deleted IS NULL', $this->expr->isNull('u.deleted'));
235
    }
236
237
    public function testIsNotNull() : void
238
    {
239
        self::assertEquals('u.updated IS NOT NULL', $this->expr->isNotNull('u.updated'));
240
    }
241
242
    public function testIn() : void
243
    {
244
        self::assertEquals('u.groups IN (1, 3, 4, 7)', $this->expr->in('u.groups', ['1', '3', '4', '7']));
245
    }
246
247
    public function testInWithPlaceholder() : void
248
    {
249
        self::assertEquals('u.groups IN (?)', $this->expr->in('u.groups', '?'));
250
    }
251
252
    public function testNotIn() : void
253
    {
254
        self::assertEquals('u.groups NOT IN (1, 3, 4, 7)', $this->expr->notIn('u.groups', ['1', '3', '4', '7']));
255
    }
256
257
    public function testNotInWithPlaceholder() : void
258
    {
259
        self::assertEquals('u.groups NOT IN (:values)', $this->expr->notIn('u.groups', ':values'));
260
    }
261
262
    public function testLikeWithoutEscape() : void
263
    {
264
        self::assertEquals("a.song LIKE 'a virgin'", $this->expr->like('a.song', "'a virgin'"));
265
    }
266
267
    public function testLikeWithEscape() : void
268
    {
269
        self::assertEquals(
270
            "a.song LIKE 'a virgin' ESCAPE '💩'",
271
            $this->expr->like('a.song', "'a virgin'", "'💩'")
272
        );
273
    }
274
275
    public function testNotLikeWithoutEscape() : void
276
    {
277
        self::assertEquals(
278
            "s.last_words NOT LIKE 'this'",
279
            $this->expr->notLike('s.last_words', "'this'")
280
        );
281
    }
282
283
    public function testNotLikeWithEscape() : void
284
    {
285
        self::assertEquals(
286
            "p.description NOT LIKE '20💩%' ESCAPE '💩'",
287
            $this->expr->notLike('p.description', "'20💩%'", "'💩'")
288
        );
289
    }
290
}
291