Completed
Push — master ( c23b86...df4071 )
by Sergei
42:23 queued 06:15
created

IndexTest::indexLengthProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\Index;
6
use PHPUnit\Framework\TestCase;
7
8
class IndexTest extends TestCase
9
{
10
    public function createIndex($unique = false, $primary = false, $options = [])
11
    {
12
        return new Index('foo', ['bar', 'baz'], $unique, $primary, [], $options);
13
    }
14
15
    public function testCreateIndex()
16
    {
17
        $idx = $this->createIndex();
18
        self::assertEquals('foo', $idx->getName());
19
        $columns = $idx->getColumns();
20
        self::assertCount(2, $columns);
21
        self::assertEquals(['bar', 'baz'], $columns);
22
        self::assertFalse($idx->isUnique());
23
        self::assertFalse($idx->isPrimary());
24
    }
25
26
    public function testCreatePrimary()
27
    {
28
        $idx = $this->createIndex(false, true);
29
        self::assertTrue($idx->isUnique());
30
        self::assertTrue($idx->isPrimary());
31
    }
32
33
    public function testCreateUnique()
34
    {
35
        $idx = $this->createIndex(true, false);
36
        self::assertTrue($idx->isUnique());
37
        self::assertFalse($idx->isPrimary());
38
    }
39
40
    /**
41
     * @group DBAL-50
42
     */
43
    public function testFulfilledByUnique()
44
    {
45
        $idx1 = $this->createIndex(true, false);
46
        $idx2 = $this->createIndex(true, false);
47
        $idx3 = $this->createIndex();
48
49
        self::assertTrue($idx1->isFullfilledBy($idx2));
50
        self::assertFalse($idx1->isFullfilledBy($idx3));
51
    }
52
53
    /**
54
     * @group DBAL-50
55
     */
56
    public function testFulfilledByPrimary()
57
    {
58
        $idx1 = $this->createIndex(true, true);
59
        $idx2 = $this->createIndex(true, true);
60
        $idx3 = $this->createIndex(true, false);
61
62
        self::assertTrue($idx1->isFullfilledBy($idx2));
63
        self::assertFalse($idx1->isFullfilledBy($idx3));
64
    }
65
66
    /**
67
     * @group DBAL-50
68
     */
69
    public function testFulfilledByIndex()
70
    {
71
        $idx1 = $this->createIndex();
72
        $idx2 = $this->createIndex();
73
        $pri  = $this->createIndex(true, true);
74
        $uniq = $this->createIndex(true);
75
76
        self::assertTrue($idx1->isFullfilledBy($idx2));
77
        self::assertTrue($idx1->isFullfilledBy($pri));
78
        self::assertTrue($idx1->isFullfilledBy($uniq));
79
    }
80
81
    public function testFulfilledWithPartial()
82
    {
83
        $without = new Index('without', ['col1', 'col2'], true, false, [], []);
84
        $partial = new Index('partial', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
85
        $another = new Index('another', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
86
87
        self::assertFalse($partial->isFullfilledBy($without));
88
        self::assertFalse($without->isFullfilledBy($partial));
89
90
        self::assertTrue($partial->isFullfilledBy($partial));
91
92
        self::assertTrue($partial->isFullfilledBy($another));
93
        self::assertTrue($another->isFullfilledBy($partial));
94
    }
95
96
    public function testOverrulesWithPartial()
97
    {
98
        $without = new Index('without', ['col1', 'col2'], true, false, [], []);
99
        $partial = new Index('partial', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
100
        $another = new Index('another', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
101
102
        self::assertFalse($partial->overrules($without));
103
        self::assertFalse($without->overrules($partial));
104
105
        self::assertTrue($partial->overrules($partial));
106
107
        self::assertTrue($partial->overrules($another));
108
        self::assertTrue($another->overrules($partial));
109
    }
110
111
    /**
112
     * @param string[]     $columns
113
     * @param int[]|null[] $lengths1
114
     * @param int[]|null[] $lengths2
115
     *
116
     * @dataProvider indexLengthProvider
117
     */
118
    public function testFulfilledWithLength(array $columns, array $lengths1, array $lengths2, bool $expected) : void
119
    {
120
        $index1 = new Index('index1', $columns, false, false, [], ['lengths' => $lengths1]);
121
        $index2 = new Index('index2', $columns, false, false, [], ['lengths' => $lengths2]);
122
123
        self::assertSame($expected, $index1->isFullfilledBy($index2));
124
        self::assertSame($expected, $index2->isFullfilledBy($index1));
125
    }
126
127
    /**
128
     * @return mixed[][]
129
     */
130
    public static function indexLengthProvider() : iterable
131
    {
132
        return [
133
            'empty' => [['column'], [], [], true],
134
            'same' => [['column'], [64], [64], true],
135
            'different' => [['column'], [32], [64], false],
136
            'sparse-different-positions' => [['column1', 'column2'], [0 => 32], [1 => 32], false],
137
            'sparse-same-positions' => [['column1', 'column2'], [null, 32], [1 => 32], true],
138
        ];
139
    }
140
141
    /**
142
     * @group DBAL-220
143
     */
144
    public function testFlags()
145
    {
146
        $idx1 = $this->createIndex();
147
        self::assertFalse($idx1->hasFlag('clustered'));
148
        self::assertEmpty($idx1->getFlags());
149
150
        $idx1->addFlag('clustered');
151
        self::assertTrue($idx1->hasFlag('clustered'));
152
        self::assertTrue($idx1->hasFlag('CLUSTERED'));
153
        self::assertSame(['clustered'], $idx1->getFlags());
154
155
        $idx1->removeFlag('clustered');
156
        self::assertFalse($idx1->hasFlag('clustered'));
157
        self::assertEmpty($idx1->getFlags());
158
    }
159
160
    /**
161
     * @group DBAL-285
162
     */
163
    public function testIndexQuotes()
164
    {
165
        $index = new Index('foo', ['`bar`', '`baz`']);
166
167
        self::assertTrue($index->spansColumns(['bar', 'baz']));
168
        self::assertTrue($index->hasColumnAtPosition('bar', 0));
169
        self::assertTrue($index->hasColumnAtPosition('baz', 1));
170
171
        self::assertFalse($index->hasColumnAtPosition('bar', 1));
172
        self::assertFalse($index->hasColumnAtPosition('baz', 0));
173
    }
174
175
    public function testOptions()
176
    {
177
        $idx1 = $this->createIndex();
178
        self::assertFalse($idx1->hasOption('where'));
179
        self::assertEmpty($idx1->getOptions());
180
181
        $idx2 = $this->createIndex(false, false, ['where' => 'name IS NULL']);
182
        self::assertTrue($idx2->hasOption('where'));
183
        self::assertTrue($idx2->hasOption('WHERE'));
184
        self::assertSame('name IS NULL', $idx2->getOption('where'));
185
        self::assertSame('name IS NULL', $idx2->getOption('WHERE'));
186
        self::assertSame(['where' => 'name IS NULL'], $idx2->getOptions());
187
    }
188
}
189