IndexTest.php 5.13 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Schema;

use Doctrine\DBAL\Schema\Index;

Luís Cobucci's avatar
Luís Cobucci committed
7
class IndexTest extends \PHPUnit\Framework\TestCase
8
{
9
    public function createIndex($unique = false, $primary = false, $options = array())
10
    {
11
        return new Index("foo", array("bar", "baz"), $unique, $primary, array(), $options);
12 13 14 15 16
    }

    public function testCreateIndex()
    {
        $idx = $this->createIndex();
17
        self::assertEquals("foo", $idx->getName());
18
        $columns = $idx->getColumns();
Gabriel Caruso's avatar
Gabriel Caruso committed
19
        self::assertCount(2, $columns);
20 21 22
        self::assertEquals(array("bar", "baz"), $columns);
        self::assertFalse($idx->isUnique());
        self::assertFalse($idx->isPrimary());
23 24 25 26 27
    }

    public function testCreatePrimary()
    {
        $idx = $this->createIndex(false, true);
28 29
        self::assertTrue($idx->isUnique());
        self::assertTrue($idx->isPrimary());
30 31 32 33 34
    }

    public function testCreateUnique()
    {
        $idx = $this->createIndex(true, false);
35 36
        self::assertTrue($idx->isUnique());
        self::assertFalse($idx->isPrimary());
37
    }
38 39 40 41

    /**
     * @group DBAL-50
     */
Possum's avatar
Possum committed
42
    public function testFulfilledByUnique()
43 44 45 46 47
    {
        $idx1 = $this->createIndex(true, false);
        $idx2 = $this->createIndex(true, false);
        $idx3 = $this->createIndex();

48 49
        self::assertTrue($idx1->isFullfilledBy($idx2));
        self::assertFalse($idx1->isFullfilledBy($idx3));
50 51 52 53 54
    }

    /**
     * @group DBAL-50
     */
Possum's avatar
Possum committed
55
    public function testFulfilledByPrimary()
56 57 58 59 60
    {
        $idx1 = $this->createIndex(true, true);
        $idx2 = $this->createIndex(true, true);
        $idx3 = $this->createIndex(true, false);

61 62
        self::assertTrue($idx1->isFullfilledBy($idx2));
        self::assertFalse($idx1->isFullfilledBy($idx3));
63 64 65 66 67
    }

    /**
     * @group DBAL-50
     */
Possum's avatar
Possum committed
68
    public function testFulfilledByIndex()
69 70 71 72 73 74
    {
        $idx1 = $this->createIndex();
        $idx2 = $this->createIndex();
        $pri = $this->createIndex(true, true);
        $uniq = $this->createIndex(true);

75 76 77
        self::assertTrue($idx1->isFullfilledBy($idx2));
        self::assertTrue($idx1->isFullfilledBy($pri));
        self::assertTrue($idx1->isFullfilledBy($uniq));
78
    }
79

Possum's avatar
Possum committed
80
    public function testFulfilledWithPartial()
81
    {
82 83 84
        $without = new Index('without', array('col1', 'col2'), true, false, array(), array());
        $partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
        $another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
85

86 87
        self::assertFalse($partial->isFullfilledBy($without));
        self::assertFalse($without->isFullfilledBy($partial));
88

89
        self::assertTrue($partial->isFullfilledBy($partial));
90

91 92
        self::assertTrue($partial->isFullfilledBy($another));
        self::assertTrue($another->isFullfilledBy($partial));
93 94 95 96
    }

    public function testOverrulesWithPartial()
    {
97 98 99
        $without = new Index('without', array('col1', 'col2'), true, false, array(), array());
        $partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
        $another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
100

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

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

106 107
        self::assertTrue($partial->overrules($another));
        self::assertTrue($another->overrules($partial));
108 109
    }

110 111 112 113 114 115
    /**
     * @group DBAL-220
     */
    public function testFlags()
    {
        $idx1 = $this->createIndex();
116 117
        self::assertFalse($idx1->hasFlag('clustered'));
        self::assertEmpty($idx1->getFlags());
118 119

        $idx1->addFlag('clustered');
120 121 122
        self::assertTrue($idx1->hasFlag('clustered'));
        self::assertTrue($idx1->hasFlag('CLUSTERED'));
        self::assertSame(array('clustered'), $idx1->getFlags());
123 124

        $idx1->removeFlag('clustered');
125 126
        self::assertFalse($idx1->hasFlag('clustered'));
        self::assertEmpty($idx1->getFlags());
127
    }
128 129 130 131 132 133 134 135

    /**
     * @group DBAL-285
     */
    public function testIndexQuotes()
    {
        $index = new Index("foo", array("`bar`", "`baz`"));

136 137 138
        self::assertTrue($index->spansColumns(array("bar", "baz")));
        self::assertTrue($index->hasColumnAtPosition("bar", 0));
        self::assertTrue($index->hasColumnAtPosition("baz", 1));
139

140 141
        self::assertFalse($index->hasColumnAtPosition("bar", 1));
        self::assertFalse($index->hasColumnAtPosition("baz", 0));
142
    }
143 144 145 146

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

        $idx2 = $this->createIndex(false, false, array('where' => 'name IS NULL'));
151 152 153 154 155
        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(array('where' => 'name IS NULL'), $idx2->getOptions());
156
    }
157
}