SqliteSchemaManagerTest.php 7.3 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Doctrine\Tests\DBAL\Functional\Schema;

use Doctrine\DBAL\Schema;

require_once __DIR__ . '/../../../TestInit.php';
 
romanb's avatar
romanb committed
9
class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
10
{
romanb's avatar
romanb committed
11 12 13
    /**
     * @expectedException \Exception
     */
14 15
    public function testListDatabases()
    {
romanb's avatar
romanb committed
16
        $this->_sm->listDatabases();
17 18
    }

romanb's avatar
romanb committed
19 20 21
    /**
     * @expectedException \Exception
     */
22 23
    public function testListFunctions()
    {
romanb's avatar
romanb committed
24
        $this->_sm->listFunctions();
25 26
    }

romanb's avatar
romanb committed
27 28 29
    /**
     * @expectedException \Exception
     */
30 31
    public function testListTriggers()
    {
romanb's avatar
romanb committed
32
        $this->_sm->listTriggers();
33 34 35 36
    }

    public function testListSequences()
    {
37
        $this->createTestTable('list_sequences_test');
38
        $sequences = $this->_sm->listSequences();
39 40
        $this->assertEquals('list_sequences_test', $sequences[0]['name']);
        $this->assertEquals('sqlite_sequence', $sequences[1]['name']);
41 42 43 44 45 46 47 48
    }

    public function testListTableConstraints()
    {
        // TODO: Implement support for constraints/foreign keys to be specified
        // when creating tables. Sqlite does not support adding them after
        // the table has already been created
        $tableConstraints = $this->_sm->listTableConstraints('list_table_constraints_test');
49
        $this->assertEquals(array(), $tableConstraints);
50 51 52 53
    }

    public function testListTableColumns()
    {
54
        $this->createTestTable('list_table_columns_test');
55 56 57

        $tableColumns = $this->_sm->listTableColumns('list_table_columns_test');

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $this->assertEquals('id', $tableColumns[0]['name']);
        $this->assertEquals(true, $tableColumns[0]['primary']);
        $this->assertEquals('Doctrine\DBAL\Types\IntegerType', get_class($tableColumns[0]['type']));
        $this->assertEquals(4, $tableColumns[0]['length']);
        $this->assertEquals(false, $tableColumns[0]['unsigned']);
        $this->assertEquals(false, $tableColumns[0]['fixed']);
        $this->assertEquals(true, $tableColumns[0]['notnull']);
        $this->assertEquals(null, $tableColumns[0]['default']);

        $this->assertEquals('test', $tableColumns[1]['name']);
        $this->assertEquals(false, $tableColumns[1]['primary']);
        $this->assertEquals('Doctrine\DBAL\Types\StringType', get_class($tableColumns[1]['type']));
        $this->assertEquals(255, $tableColumns[1]['length']);
        $this->assertEquals(false, $tableColumns[1]['unsigned']);
        $this->assertEquals(false, $tableColumns[1]['fixed']);
        $this->assertEquals(false, $tableColumns[1]['notnull']);
        $this->assertEquals(null, $tableColumns[1]['default']);
75 76 77 78
    }

    public function testListTableIndexes()
    {
79
        $data['options'] = array(
80 81 82 83 84 85 86 87 88 89
            'indexes' => array(
                'test' => array(
                    'fields' => array(
                        'test' => array()
                    ),
                    'type' => 'unique'
                )
            )
        );

90
        $this->createTestTable('list_table_indexes_test', $data);
91 92

        $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
93 94
        $this->assertEquals('test', $tableIndexes[0]['name']);
        $this->assertEquals(true, $tableIndexes[0]['unique']);
95 96
    }

97
    public function testListTables()
98
    {
99
        $this->createTestTable('list_tables_test');
100
        $tables = $this->_sm->listTables();
101
        $this->assertEquals(true, in_array('list_tables_test', $tables));
102 103
    }

romanb's avatar
romanb committed
104 105 106
    /**
     * @expectedException \Exception
     */
107 108
    public function testListUsers()
    {
romanb's avatar
romanb committed
109
        $this->_sm->listUsers();
110 111 112 113
    }

    public function testListViews()
    {
114 115
        $this->createTestTable('test_views');
        $this->_sm->dropAndCreateView('test_create_view', 'SELECT * from test_views');
116 117
        $views = $this->_sm->listViews();

118 119
        $this->assertEquals('test_create_view', $views[0]['name']);
        $this->assertEquals('CREATE VIEW test_create_view AS SELECT * from test_views', $views[0]['sql']);
120 121 122 123 124 125
    }

    public function testCreateAndDropDatabase()
    {
        $path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite';

jwage's avatar
jwage committed
126
        $this->_sm->createDatabase($path);
127
        $this->assertEquals(true, file_exists($path));
jwage's avatar
jwage committed
128
        $this->_sm->dropDatabase($path);
129
        $this->assertEquals(false, file_exists($path));
jwage's avatar
jwage committed
130
    }
131 132 133

    public function testCreateTable()
    {
134
        $this->createTestTable('test_create_table');
135
        $tables = $this->_sm->listTables();
136
        $this->assertEquals(true, in_array('test_create_table', $tables));
137 138 139

        $tableColumns = $this->_sm->listTableColumns('test_create_table');

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        $this->assertEquals('id', $tableColumns[0]['name']);
        $this->assertEquals(true, $tableColumns[0]['primary']);
        $this->assertEquals('Doctrine\DBAL\Types\IntegerType', get_class($tableColumns[0]['type']));
        $this->assertEquals(4, $tableColumns[0]['length']);
        $this->assertEquals(false, $tableColumns[0]['unsigned']);
        $this->assertEquals(false, $tableColumns[0]['fixed']);
        $this->assertEquals(true, $tableColumns[0]['notnull']);
        $this->assertEquals(null, $tableColumns[0]['default']);

        $this->assertEquals('test', $tableColumns[1]['name']);
        $this->assertEquals(false, $tableColumns[1]['primary']);
        $this->assertEquals('Doctrine\DBAL\Types\StringType', get_class($tableColumns[1]['type']));
        $this->assertEquals(255, $tableColumns[1]['length']);
        $this->assertEquals(false, $tableColumns[1]['unsigned']);
        $this->assertEquals(false, $tableColumns[1]['fixed']);
        $this->assertEquals(false, $tableColumns[1]['notnull']);
        $this->assertEquals(null, $tableColumns[1]['default']);
157 158
    }

romanb's avatar
romanb committed
159 160 161
    /**
     * @expectedException \Exception
     */
162 163
    public function testCreateSequence()
    {
romanb's avatar
romanb committed
164
        $this->_sm->createSequence('seqname', 1, 1);
165
    }
jwage's avatar
jwage committed
166

167 168
    public function testCreateIndex()
    {
169
        $this->createTestTable('test_create_index');
170 171 172 173 174 175 176

        $index = array(
            'fields' => array(
                'test' => array()
            ),
            'type' => 'unique'
        );
177
        
178
        $this->_sm->dropAndCreateIndex('test_create_index', 'test', $index);
179
        $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
180 181
        $this->assertEquals('test', $tableIndexes[0]['name']);
        $this->assertEquals(true, $tableIndexes[0]['unique']);
jwage's avatar
jwage committed
182
    }
183

romanb's avatar
romanb committed
184 185 186
    /**
     * @expectedException \Exception
     */
187 188
    public function testCreateForeignKey()
    {
romanb's avatar
romanb committed
189
        $this->_sm->createForeignKey('table', array());
190 191
    }

romanb's avatar
romanb committed
192 193 194
    /**
     * @expectedException \Exception
     */
195 196
    public function testRenameTable()
    {
romanb's avatar
romanb committed
197
        $this->_sm->renameTable('oldname', 'newname');
198 199
    }

romanb's avatar
romanb committed
200 201 202
    /**
     * @expectedException \Exception
     */
203 204
    public function testAddTableColumn()
    {
romanb's avatar
romanb committed
205
        return $this->_sm->addTableColumn('table', 'column', array());
206 207
    }

romanb's avatar
romanb committed
208 209 210
    /**
     * @expectedException \Exception
     */
211 212
    public function testRemoveTableColumn()
    {
romanb's avatar
romanb committed
213
        $this->_sm->removeTableColumn('table', 'column');
214 215
    }

romanb's avatar
romanb committed
216 217 218
    /**
     * @expectedException \Exception
     */
219 220
    public function testChangeTableColumn()
    {
romanb's avatar
romanb committed
221
        $this->_sm->changeTableColumn('name', 'type', null, array());
222
    }
romanb's avatar
romanb committed
223 224 225 226
    
    /**
     * @expectedException \Exception
     */
227 228
    public function testRenameTableColumn()
    {
romanb's avatar
romanb committed
229
        $this->_sm->renameTableColumn('table', 'old', 'new', array());
230 231
    }
}