File: IndexTest.php

package info (click to toggle)
php-doctrine-dbal 3.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,500 kB
  • sloc: php: 54,704; xml: 485; makefile: 42; sh: 24
file content (172 lines) | stat: -rw-r--r-- 6,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

namespace Doctrine\DBAL\Tests\Schema;

use Doctrine\DBAL\Schema\Index;
use PHPUnit\Framework\TestCase;

class IndexTest extends TestCase
{
    /** @param mixed[] $options */
    private function createIndex(bool $unique = false, bool $primary = false, array $options = []): Index
    {
        return new Index('foo', ['bar', 'baz'], $unique, $primary, [], $options);
    }

    public function testCreateIndex(): void
    {
        $idx = $this->createIndex();
        self::assertEquals('foo', $idx->getName());
        $columns = $idx->getColumns();
        self::assertCount(2, $columns);
        self::assertEquals(['bar', 'baz'], $columns);
        self::assertFalse($idx->isUnique());
        self::assertFalse($idx->isPrimary());
    }

    public function testCreatePrimary(): void
    {
        $idx = $this->createIndex(false, true);
        self::assertTrue($idx->isUnique());
        self::assertTrue($idx->isPrimary());
    }

    public function testCreateUnique(): void
    {
        $idx = $this->createIndex(true, false);
        self::assertTrue($idx->isUnique());
        self::assertFalse($idx->isPrimary());
    }

    public function testFulfilledByUnique(): void
    {
        $idx1 = $this->createIndex(true, false);
        $idx2 = $this->createIndex(true, false);
        $idx3 = $this->createIndex();

        self::assertTrue($idx1->isFulfilledBy($idx2));
        self::assertFalse($idx1->isFulfilledBy($idx3));
    }

    public function testFulfilledByPrimary(): void
    {
        $idx1 = $this->createIndex(true, true);
        $idx2 = $this->createIndex(true, true);
        $idx3 = $this->createIndex(true, false);

        self::assertTrue($idx1->isFulfilledBy($idx2));
        self::assertFalse($idx1->isFulfilledBy($idx3));
    }

    public function testFulfilledByIndex(): void
    {
        $idx1 = $this->createIndex();
        $idx2 = $this->createIndex();
        $pri  = $this->createIndex(true, true);
        $uniq = $this->createIndex(true);

        self::assertTrue($idx1->isFulfilledBy($idx2));
        self::assertTrue($idx1->isFulfilledBy($pri));
        self::assertTrue($idx1->isFulfilledBy($uniq));
    }

    public function testFulfilledWithPartial(): void
    {
        $without = new Index('without', ['col1', 'col2'], true, false, [], []);
        $partial = new Index('partial', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
        $another = new Index('another', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);

        self::assertFalse($partial->isFulfilledBy($without));
        self::assertFalse($without->isFulfilledBy($partial));

        self::assertTrue($partial->isFulfilledBy($partial));

        self::assertTrue($partial->isFulfilledBy($another));
        self::assertTrue($another->isFulfilledBy($partial));
    }

    public function testOverrulesWithPartial(): void
    {
        $without = new Index('without', ['col1', 'col2'], true, false, [], []);
        $partial = new Index('partial', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);
        $another = new Index('another', ['col1', 'col2'], true, false, [], ['where' => 'col1 IS NULL']);

        self::assertFalse($partial->overrules($without));
        self::assertFalse($without->overrules($partial));

        self::assertTrue($partial->overrules($partial));

        self::assertTrue($partial->overrules($another));
        self::assertTrue($another->overrules($partial));
    }

    /**
     * @param string[]     $columns
     * @param int[]|null[] $lengths1
     * @param int[]|null[] $lengths2
     *
     * @dataProvider indexLengthProvider
     */
    public function testFulfilledWithLength(array $columns, array $lengths1, array $lengths2, bool $expected): void
    {
        $index1 = new Index('index1', $columns, false, false, [], ['lengths' => $lengths1]);
        $index2 = new Index('index2', $columns, false, false, [], ['lengths' => $lengths2]);

        self::assertSame($expected, $index1->isFulfilledBy($index2));
        self::assertSame($expected, $index2->isFulfilledBy($index1));
    }

    /** @return mixed[][] */
    public static function indexLengthProvider(): iterable
    {
        return [
            'empty' => [['column'], [], [], true],
            'same' => [['column'], [64], [64], true],
            'different' => [['column'], [32], [64], false],
            'sparse-different-positions' => [['column1', 'column2'], [0 => 32], [1 => 32], false],
            'sparse-same-positions' => [['column1', 'column2'], [null, 32], [1 => 32], true],
        ];
    }

    public function testFlags(): void
    {
        $idx1 = $this->createIndex();
        self::assertFalse($idx1->hasFlag('clustered'));
        self::assertEmpty($idx1->getFlags());

        $idx1->addFlag('clustered');
        self::assertTrue($idx1->hasFlag('clustered'));
        self::assertTrue($idx1->hasFlag('CLUSTERED'));
        self::assertSame(['clustered'], $idx1->getFlags());

        $idx1->removeFlag('clustered');
        self::assertFalse($idx1->hasFlag('clustered'));
        self::assertEmpty($idx1->getFlags());
    }

    public function testIndexQuotes(): void
    {
        $index = new Index('foo', ['`bar`', '`baz`']);

        self::assertTrue($index->spansColumns(['bar', 'baz']));
        self::assertTrue($index->hasColumnAtPosition('bar', 0));
        self::assertTrue($index->hasColumnAtPosition('baz', 1));

        self::assertFalse($index->hasColumnAtPosition('bar', 1));
        self::assertFalse($index->hasColumnAtPosition('baz', 0));
    }

    public function testOptions(): void
    {
        $idx1 = $this->createIndex();
        self::assertFalse($idx1->hasOption('where'));
        self::assertEmpty($idx1->getOptions());

        $idx2 = $this->createIndex(false, false, ['where' => 'name IS NULL']);
        self::assertTrue($idx2->hasOption('where'));
        self::assertTrue($idx2->hasOption('WHERE'));
        self::assertSame('name IS NULL', $idx2->getOption('where'));
        self::assertSame('name IS NULL', $idx2->getOption('WHERE'));
        self::assertSame(['where' => 'name IS NULL'], $idx2->getOptions());
    }
}