IndexTest.php 5.19 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Doctrine\Tests\DBAL\Schema;

use Doctrine\DBAL\Schema\Index;

class IndexTest extends \PHPUnit_Framework_TestCase
{
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 17 18 19 20 21 22 23 24 25 26 27
    }

    public function testCreateIndex()
    {
        $idx = $this->createIndex();
        $this->assertEquals("foo", $idx->getName());
        $columns = $idx->getColumns();
        $this->assertEquals(2, count($columns));
        $this->assertEquals(array("bar", "baz"), $columns);
        $this->assertFalse($idx->isUnique());
        $this->assertFalse($idx->isPrimary());
    }

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

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

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

        $this->assertTrue($idx1->isFullfilledBy($idx2));
        $this->assertFalse($idx1->isFullfilledBy($idx3));
    }

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

        $this->assertTrue($idx1->isFullfilledBy($idx2));
        $this->assertFalse($idx1->isFullfilledBy($idx3));
    }

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

        $this->assertTrue($idx1->isFullfilledBy($idx2));
        $this->assertTrue($idx1->isFullfilledBy($pri));
        $this->assertTrue($idx1->isFullfilledBy($uniq));
    }
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 88 89 90 91 92 93 94 95 96

        $this->assertFalse($partial->isFullfilledBy($without));
        $this->assertFalse($without->isFullfilledBy($partial));

        $this->assertTrue($partial->isFullfilledBy($partial));

        $this->assertTrue($partial->isFullfilledBy($another));
        $this->assertTrue($another->isFullfilledBy($partial));
    }

    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 103 104 105 106 107 108 109

        $this->assertFalse($partial->overrules($without));
        $this->assertFalse($without->overrules($partial));

        $this->assertTrue($partial->overrules($partial));

        $this->assertTrue($partial->overrules($another));
        $this->assertTrue($another->overrules($partial));
    }

110 111 112 113 114 115 116
    /**
     * @group DBAL-220
     */
    public function testFlags()
    {
        $idx1 = $this->createIndex();
        $this->assertFalse($idx1->hasFlag('clustered'));
Steve Müller's avatar
Steve Müller committed
117
        $this->assertEmpty($idx1->getFlags());
118 119 120 121

        $idx1->addFlag('clustered');
        $this->assertTrue($idx1->hasFlag('clustered'));
        $this->assertTrue($idx1->hasFlag('CLUSTERED'));
Steve Müller's avatar
Steve Müller committed
122
        $this->assertSame(array('clustered'), $idx1->getFlags());
123 124 125

        $idx1->removeFlag('clustered');
        $this->assertFalse($idx1->hasFlag('clustered'));
Steve Müller's avatar
Steve Müller committed
126
        $this->assertEmpty($idx1->getFlags());
127
    }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

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

        $this->assertTrue($index->spansColumns(array("bar", "baz")));
        $this->assertTrue($index->hasColumnAtPosition("bar", 0));
        $this->assertTrue($index->hasColumnAtPosition("baz", 1));

        $this->assertFalse($index->hasColumnAtPosition("bar", 1));
        $this->assertFalse($index->hasColumnAtPosition("baz", 0));
    }
143 144 145 146 147 148 149 150 151 152 153 154 155 156

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

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