MysqlTestCase.php 7.47 KB
Newer Older
zYne's avatar
zYne committed
1
<?php
zYne's avatar
zYne committed
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
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_Export_Mysql_TestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase 
{
    public function __construct() 
    {
zYne's avatar
zYne committed
37 38 39
        parent::__construct('mysql');
    }

zYne's avatar
zYne committed
40 41
    public function testAlterTableThrowsExceptionWithoutValidTableName() 
    {
zYne's avatar
zYne committed
42 43 44 45 46 47 48 49
        try {
            $this->export->alterTable(0, array(), array());

            $this->fail();
        } catch(Doctrine_Export_Exception $e) {
            $this->pass();
        }
    }
zYne's avatar
zYne committed
50 51
    public function testCreateTableExecutesSql() 
    {
zYne's avatar
zYne committed
52 53 54 55 56 57 58 59 60
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'integer', 'unsigned' => 1));
        $options = array('type' => 'MYISAM');
        
        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id INT UNSIGNED) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
61 62
    public function testCreateTableSupportsDefaultTableType() 
    {
zYne's avatar
zYne committed
63 64 65 66 67 68 69 70 71
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'integer', 'unsigned' => 1));

        $this->export->createTable($name, $fields);

        // INNODB is the default type
        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id INT UNSIGNED) ENGINE = INNODB');
    }
zYne's avatar
zYne committed
72 73
    public function testCreateTableSupportsMultiplePks() 
    {
zYne's avatar
zYne committed
74 75 76 77 78 79 80 81 82
        $name = 'mytable';
        $fields  = array('name' => array('type' => 'char', 'length' => 10),
                         'type' => array('type' => 'integer', 'length' => 3));
                         
        $options = array('primary' => array('name', 'type'));
        $this->export->createTable($name, $fields, $options);
        
        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10), type MEDIUMINT, PRIMARY KEY(name, type)) ENGINE = INNODB');
    }
zYne's avatar
zYne committed
83 84
    public function testCreateTableSupportsAutoincPks() 
    {
zYne's avatar
zYne committed
85 86 87 88 89 90 91 92 93
        $name = 'mytable';

        $fields  = array('id' => array('type' => 'integer', 'unsigned' => 1, 'autoincrement' => true));
        $options = array('type' => 'INNODB');
        
        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY) ENGINE = INNODB');
    }
zYne's avatar
zYne committed
94 95
    public function testCreateTableSupportsCharType() 
    {
zYne's avatar
zYne committed
96 97 98 99 100 101 102 103 104
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'char', 'length' => 3));
        $options = array('type' => 'MYISAM');
        
        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id CHAR(3)) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
105 106
    public function testCreateTableSupportsCharType2() 
    {
zYne's avatar
zYne committed
107 108 109 110 111 112 113 114 115
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'char'));
        $options = array('type' => 'MYISAM');
        
        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id CHAR(255)) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
116 117
    public function testCreateTableSupportsVarcharType() 
    {
zYne's avatar
zYne committed
118 119 120 121 122 123 124 125 126
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'varchar', 'length' => '100'));
        $options = array('type' => 'MYISAM');

        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id VARCHAR(100)) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
127 128
    public function testCreateTableSupportsIntegerType() 
    {
zYne's avatar
zYne committed
129 130 131 132 133 134 135 136 137
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'integer', 'length' => '10'));
        $options = array('type' => 'MYISAM');

        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id BIGINT) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
138 139
    public function testCreateTableSupportsBlobType() 
    {
zYne's avatar
zYne committed
140 141 142 143 144 145 146 147 148
        $name = 'mytable';
        
        $fields  = array('content' => array('type' => 'blob'));
        $options = array('type' => 'MYISAM');

        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (content LONGBLOB) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
149 150
    public function testCreateTableSupportsBlobType2() 
    {
zYne's avatar
zYne committed
151 152 153 154 155 156 157 158 159 160
        $name = 'mytable';
        
        $fields  = array('content' => array('type' => 'blob', 'length' => 2000));
        $options = array('type' => 'MYISAM');

        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (content BLOB) ENGINE = MYISAM');
    }

zYne's avatar
zYne committed
161 162
    public function testCreateTableSupportsBooleanType() 
    {
zYne's avatar
zYne committed
163 164 165 166 167 168 169 170 171
        $name = 'mytable';
        
        $fields  = array('id' => array('type' => 'boolean'));
        $options = array('type' => 'MYISAM');

        $this->export->createTable($name, $fields, $options);

        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id TINYINT(1)) ENGINE = MYISAM');
    }
zYne's avatar
zYne committed
172 173
    public function testCreateDatabaseExecutesSql() 
    {
zYne's avatar
zYne committed
174 175 176 177
        $this->export->createDatabase('db');

        $this->assertEqual($this->adapter->pop(), 'CREATE DATABASE db');
    }
zYne's avatar
zYne committed
178 179
    public function testDropDatabaseExecutesSql() 
    {
zYne's avatar
zYne committed
180 181 182 183 184
        $this->export->dropDatabase('db');

        $this->assertEqual($this->adapter->pop(), 'DROP DATABASE db');
    }

zYne's avatar
zYne committed
185 186
    public function testDropIndexExecutesSql() 
    {
zYne's avatar
zYne committed
187 188 189 190
        $this->export->dropIndex('sometable', 'relevancy');

        $this->assertEqual($this->adapter->pop(), 'DROP INDEX relevancy_idx ON sometable');
    }
zYne's avatar
zYne committed
191 192 193 194 195 196 197 198 199 200 201 202 203
    public function testRecordDefinitionsSupportTableOptions() 
    {
        $r = new MysqlTestRecord;
        
        $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_test_record (name TEXT, code BIGINT, PRIMARY KEY(name, code)) ENGINE = INNODB');
    }
}
class MysqlTestRecord extends Doctrine_Record 
{
    public function setTableDefinition() 
    {
        $this->hasColumn('name', 'string', null, 'primary');
        $this->hasColumn('code', 'integer', null, 'primary');
zYne's avatar
zYne committed
204

zYne's avatar
zYne committed
205 206
        $this->option('engine', 'INNODB');
    }
zYne's avatar
zYne committed
207 208
}
?>