Failed Conditions
Pull Request — 2.10.x (#4011)
by
unknown
03:07
created

CompositeExpressionTest::testWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 18
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Query\Expression;
4
5
use Doctrine\DBAL\Query\Expression\CompositeExpression;
6
use Doctrine\Tests\DbalTestCase;
7
8
/**
9
 * @group DBAL-12
10
 */
11
class CompositeExpressionTest extends DbalTestCase
12
{
13
    public function testCount() : void
14
    {
15
        $expr = CompositeExpression::or('u.group_id = 1');
16
17
        self::assertCount(1, $expr);
18
19
        $expr = $expr->with('u.group_id = 2');
20
21
        self::assertCount(2, $expr);
22
    }
23
24
    public function testAdd() : void
25
    {
26
        $expr = CompositeExpression::or('u.group_id = 1');
27
28
        self::assertCount(1, $expr);
29
30
        $expr->add(new CompositeExpression(CompositeExpression::TYPE_AND, []));
31
32
        self::assertCount(1, $expr);
33
34
        $expr->add(CompositeExpression::or('u.user_id = 1'));
35
36
        self::assertCount(2, $expr);
37
38
        $expr->add(null);
39
40
        self::assertCount(2, $expr);
41
42
        $expr->add('u.user_id = 1');
43
44
        self::assertCount(3, $expr);
45
    }
46
47
    public function testWith() : void
48
    {
49
        $expr = CompositeExpression::or('u.group_id = 1');
50
51
        self::assertCount(1, $expr);
52
53
        // test immutability
54
        $expr->with(CompositeExpression::or('u.user_id = 1'));
55
56
        self::assertCount(1, $expr);
57
58
        $expr = $expr->with(CompositeExpression::or('u.user_id = 1'));
59
60
        self::assertCount(2, $expr);
61
62
        $expr = $expr->with('u.user_id = 1');
63
64
        self::assertCount(3, $expr);
65
    }
66
67
    /**
68
     * @param string[]|CompositeExpression[] $parts
69
     *
70
     * @dataProvider provideDataForConvertToString
71
     */
72
    public function testCompositeUsageAndGeneration(string $type, array $parts, string $expects) : void
73
    {
74
        $expr = new CompositeExpression($type, $parts);
75
76
        self::assertEquals($expects, (string) $expr);
77
    }
78
79
    /**
80
     * @return mixed[][]
81
     */
82
    public static function provideDataForConvertToString() : iterable
83
    {
84
        return [
85
            [
86
                CompositeExpression::TYPE_AND,
87
                ['u.user = 1'],
88
                'u.user = 1',
89
            ],
90
            [
91
                CompositeExpression::TYPE_AND,
92
                ['u.user = 1', 'u.group_id = 1'],
93
                '(u.user = 1) AND (u.group_id = 1)',
94
            ],
95
            [
96
                CompositeExpression::TYPE_OR,
97
                ['u.user = 1'],
98
                'u.user = 1',
99
            ],
100
            [
101
                CompositeExpression::TYPE_OR,
102
                ['u.group_id = 1', 'u.group_id = 2'],
103
                '(u.group_id = 1) OR (u.group_id = 2)',
104
            ],
105
            [
106
                CompositeExpression::TYPE_AND,
107
                [
108
                    'u.user = 1',
109
                    CompositeExpression::or(
110
                        'u.group_id = 1',
111
                        'u.group_id = 2'
112
                    ),
113
                ],
114
                '(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
115
            ],
116
            [
117
                CompositeExpression::TYPE_OR,
118
                [
119
                    'u.group_id = 1',
120
                    CompositeExpression::and(
121
                        'u.user = 1',
122
                        'u.group_id = 2'
123
                    ),
124
                ],
125
                '(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
126
            ],
127
        ];
128
    }
129
}
130