| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Database\Schema;
- use Cake\Core\Configure;
- use Cake\Database\Schema\Collection as SchemaCollection;
- use Cake\Database\Schema\MysqlSchema;
- use Cake\Database\Schema\Table;
- use Cake\Datasource\ConnectionManager;
- use Cake\TestSuite\TestCase;
- /**
- * Test case for Mysql Schema Dialect.
- */
- class MysqlSchemaTest extends TestCase
- {
- /**
- * Helper method for skipping tests that need a real connection.
- *
- * @return void
- */
- protected function _needsConnection()
- {
- $config = ConnectionManager::config('test');
- $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Not using Mysql for test config');
- }
- /**
- * Data provider for convert column testing
- *
- * @return array
- */
- public static function convertColumnProvider()
- {
- return [
- [
- 'DATETIME',
- ['type' => 'datetime', 'length' => null]
- ],
- [
- 'DATE',
- ['type' => 'date', 'length' => null]
- ],
- [
- 'TIME',
- ['type' => 'time', 'length' => null]
- ],
- [
- 'TINYINT(1)',
- ['type' => 'boolean', 'length' => null]
- ],
- [
- 'TINYINT(2)',
- ['type' => 'integer', 'length' => 2, 'unsigned' => false]
- ],
- [
- 'INTEGER(11)',
- ['type' => 'integer', 'length' => 11, 'unsigned' => false]
- ],
- [
- 'MEDIUMINT(11)',
- ['type' => 'integer', 'length' => 11, 'unsigned' => false]
- ],
- [
- 'INTEGER(11) UNSIGNED',
- ['type' => 'integer', 'length' => 11, 'unsigned' => true]
- ],
- [
- 'BIGINT',
- ['type' => 'biginteger', 'length' => null, 'unsigned' => false]
- ],
- [
- 'BIGINT UNSIGNED',
- ['type' => 'biginteger', 'length' => null, 'unsigned' => true]
- ],
- [
- 'VARCHAR(255)',
- ['type' => 'string', 'length' => 255]
- ],
- [
- 'CHAR(25)',
- ['type' => 'string', 'length' => 25, 'fixed' => true]
- ],
- [
- 'CHAR(36)',
- ['type' => 'uuid', 'length' => null]
- ],
- [
- 'TINYTEXT',
- ['type' => 'text', 'length' => null]
- ],
- [
- 'BLOB',
- ['type' => 'binary', 'length' => null]
- ],
- [
- 'MEDIUMBLOB',
- ['type' => 'binary', 'length' => null]
- ],
- [
- 'FLOAT',
- ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
- ],
- [
- 'DOUBLE',
- ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
- ],
- [
- 'DOUBLE UNSIGNED',
- ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => true]
- ],
- [
- 'DECIMAL(11,2) UNSIGNED',
- ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => true]
- ],
- [
- 'DECIMAL(11,2)',
- ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => false]
- ],
- [
- 'FLOAT(11,2)',
- ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => false]
- ],
- [
- 'FLOAT(11,2) UNSIGNED',
- ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => true]
- ],
- [
- 'DOUBLE(10,4)',
- ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => false]
- ],
- [
- 'DOUBLE(10,4) UNSIGNED',
- ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => true]
- ],
- ];
- }
- /**
- * Test parsing MySQL column types form field description.
- *
- * @dataProvider convertColumnProvider
- * @return void
- */
- public function testConvertColumn($type, $expected)
- {
- $field = [
- 'Field' => 'field',
- 'Type' => $type,
- 'Null' => 'YES',
- 'Default' => 'Default value',
- 'Collation' => 'Collate information',
- 'Comment' => 'Comment section',
- ];
- $expected += [
- 'null' => true,
- 'default' => 'Default value',
- 'collate' => 'Collate information',
- 'comment' => 'Comment section',
- ];
- $driver = $this->getMock('Cake\Database\Driver\Mysql');
- $dialect = new MysqlSchema($driver);
- $table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
- $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
- $dialect->convertColumnDescription($table, $field);
- }
- /**
- * Helper method for testing methods.
- *
- * @param \Cake\Database\Connection $connection
- * @return void
- */
- protected function _createTables($connection)
- {
- $this->_needsConnection();
- $connection->execute('DROP TABLE IF EXISTS schema_articles');
- $connection->execute('DROP TABLE IF EXISTS schema_authors');
- $table = <<<SQL
- CREATE TABLE schema_authors (
- id INT(11) PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(50),
- bio TEXT,
- created DATETIME
- )ENGINE=InnoDB
- SQL;
- $connection->execute($table);
- $table = <<<SQL
- CREATE TABLE schema_articles (
- id BIGINT PRIMARY KEY AUTO_INCREMENT,
- title VARCHAR(20) COMMENT 'A title',
- body TEXT,
- author_id INT(11) NOT NULL,
- published BOOLEAN DEFAULT 0,
- allow_comments TINYINT(1) DEFAULT 0,
- created DATETIME,
- KEY `author_idx` (`author_id`),
- UNIQUE KEY `length_idx` (`title`(4)),
- FOREIGN KEY `author_idx` (`author_id`) REFERENCES `schema_authors`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
- ) ENGINE=InnoDB COLLATE=utf8_general_ci
- SQL;
- $connection->execute($table);
- }
- /**
- * Integration test for SchemaCollection & MysqlDialect.
- *
- * @return void
- */
- public function testListTables()
- {
- $connection = ConnectionManager::get('test');
- $this->_createTables($connection);
- $schema = new SchemaCollection($connection);
- $result = $schema->listTables();
- $this->assertInternalType('array', $result);
- $this->assertContains('schema_articles', $result);
- $this->assertContains('schema_authors', $result);
- }
- /**
- * Test describing a table with Mysql
- *
- * @return void
- */
- public function testDescribeTable()
- {
- $connection = ConnectionManager::get('test');
- $this->_createTables($connection);
- $schema = new SchemaCollection($connection);
- $result = $schema->describe('schema_articles');
- $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
- $expected = [
- 'id' => [
- 'type' => 'biginteger',
- 'null' => false,
- 'unsigned' => false,
- 'default' => null,
- 'length' => 20,
- 'precision' => null,
- 'comment' => null,
- 'autoIncrement' => true,
- ],
- 'title' => [
- 'type' => 'string',
- 'null' => true,
- 'default' => null,
- 'length' => 20,
- 'precision' => null,
- 'comment' => 'A title',
- 'fixed' => null,
- ],
- 'body' => [
- 'type' => 'text',
- 'null' => true,
- 'default' => null,
- 'length' => null,
- 'precision' => null,
- 'comment' => null,
- ],
- 'author_id' => [
- 'type' => 'integer',
- 'null' => false,
- 'unsigned' => false,
- 'default' => null,
- 'length' => 11,
- 'precision' => null,
- 'comment' => null,
- 'autoIncrement' => null,
- ],
- 'published' => [
- 'type' => 'boolean',
- 'null' => true,
- 'default' => 0,
- 'length' => null,
- 'precision' => null,
- 'comment' => null,
- ],
- 'allow_comments' => [
- 'type' => 'boolean',
- 'null' => true,
- 'default' => 0,
- 'length' => null,
- 'precision' => null,
- 'comment' => null,
- ],
- 'created' => [
- 'type' => 'datetime',
- 'null' => true,
- 'default' => null,
- 'length' => null,
- 'precision' => null,
- 'comment' => null,
- ],
- ];
- $this->assertEquals(['id'], $result->primaryKey());
- foreach ($expected as $field => $definition) {
- $this->assertEquals(
- $definition,
- $result->column($field),
- 'Field definition does not match for ' . $field
- );
- }
- }
- /**
- * Test describing a table with indexes in Mysql
- *
- * @return void
- */
- public function testDescribeTableIndexes()
- {
- $connection = ConnectionManager::get('test');
- $this->_createTables($connection);
- $schema = new SchemaCollection($connection);
- $result = $schema->describe('schema_articles');
- $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
- $this->assertCount(3, $result->constraints());
- $expected = [
- 'primary' => [
- 'type' => 'primary',
- 'columns' => ['id'],
- 'length' => []
- ],
- 'length_idx' => [
- 'type' => 'unique',
- 'columns' => ['title'],
- 'length' => [
- 'title' => 4,
- ]
- ],
- 'schema_articles_ibfk_1' => [
- 'type' => 'foreign',
- 'columns' => ['author_id'],
- 'references' => ['schema_authors', 'id'],
- 'length' => [],
- 'update' => 'cascade',
- 'delete' => 'restrict',
- ]
- ];
- $this->assertEquals($expected['primary'], $result->constraint('primary'));
- $this->assertEquals($expected['length_idx'], $result->constraint('length_idx'));
- $this->assertEquals($expected['schema_articles_ibfk_1'], $result->constraint('schema_articles_ibfk_1'));
- $this->assertCount(1, $result->indexes());
- $expected = [
- 'type' => 'index',
- 'columns' => ['author_id'],
- 'length' => []
- ];
- $this->assertEquals($expected, $result->index('author_idx'));
- }
- /**
- * Test describing a table creates options
- *
- * @return void
- */
- public function testDescribeTableOptions()
- {
- $connection = ConnectionManager::get('test');
- $this->_createTables($connection);
- $schema = new SchemaCollection($connection);
- $result = $schema->describe('schema_articles');
- $this->assertArrayHasKey('engine', $result->options());
- $this->assertArrayHasKey('collation', $result->options());
- }
- public function testDescribeNonPrimaryAutoIncrement()
- {
- $this->_needsConnection();
- $connection = ConnectionManager::get('test');
- $sql = <<<SQL
- CREATE TABLE `odd_primary_key` (
- `id` BIGINT UNSIGNED NOT NULL,
- `other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,
- PRIMARY KEY (`id`),
- UNIQUE KEY `other_field` (`other_field`)
- )
- SQL;
- $connection->execute($sql);
- $schema = new SchemaCollection($connection);
- $table = $schema->describe('odd_primary_key');
- $connection->execute('DROP TABLE odd_primary_key');
- $column = $table->column('id');
- $this->assertNull($column['autoIncrement'], 'should not autoincrement');
- $this->assertTrue($column['unsigned'], 'should be unsigned');
- $column = $table->column('other_field');
- $this->assertTrue($column['autoIncrement'], 'should not autoincrement');
- $this->assertFalse($column['unsigned'], 'should not be unsigned');
- $output = $table->createSql($connection);
- $this->assertContains('`id` BIGINT UNSIGNED NOT NULL,', $output[0]);
- $this->assertContains('`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,', $output[0]);
- }
- /**
- * Column provider for creating column sql
- *
- * @return array
- */
- public static function columnSqlProvider()
- {
- return [
- // strings
- [
- 'title',
- ['type' => 'string', 'length' => 25, 'null' => false],
- '`title` VARCHAR(25) NOT NULL'
- ],
- [
- 'title',
- ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
- '`title` VARCHAR(25) DEFAULT NULL'
- ],
- [
- 'id',
- ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
- '`id` CHAR(32) NOT NULL'
- ],
- [
- 'role',
- ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
- '`role` VARCHAR(10) NOT NULL DEFAULT "admin"'
- ],
- [
- 'title',
- ['type' => 'string'],
- '`title` VARCHAR(255)'
- ],
- [
- 'id',
- ['type' => 'uuid'],
- '`id` CHAR(36)'
- ],
- // Text
- [
- 'body',
- ['type' => 'text', 'null' => false],
- '`body` TEXT NOT NULL'
- ],
- // Integers
- [
- 'post_id',
- ['type' => 'integer', 'length' => 11],
- '`post_id` INTEGER(11)'
- ],
- [
- 'post_id',
- ['type' => 'integer', 'length' => 11, 'unsigned' => true],
- '`post_id` INTEGER(11) UNSIGNED'
- ],
- [
- 'post_id',
- ['type' => 'biginteger', 'length' => 20],
- '`post_id` BIGINT'
- ],
- [
- 'post_id',
- ['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
- '`post_id` BIGINT UNSIGNED'
- ],
- [
- 'post_id',
- ['type' => 'integer', 'length' => 20, 'autoIncrement' => true],
- '`post_id` INTEGER(20) AUTO_INCREMENT'
- ],
- [
- 'post_id',
- ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],
- '`post_id` BIGINT AUTO_INCREMENT'
- ],
- // Decimal
- [
- 'value',
- ['type' => 'decimal'],
- '`value` DECIMAL'
- ],
- [
- 'value',
- ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
- '`value` DECIMAL(11,0) UNSIGNED'
- ],
- [
- 'value',
- ['type' => 'decimal', 'length' => 12, 'precision' => 5],
- '`value` DECIMAL(12,5)'
- ],
- // Float
- [
- 'value',
- ['type' => 'float', 'unsigned'],
- '`value` FLOAT'
- ],
- [
- 'value',
- ['type' => 'float', 'unsigned' => true],
- '`value` FLOAT UNSIGNED'
- ],
- [
- 'value',
- ['type' => 'float', 'length' => 11, 'precision' => 3],
- '`value` FLOAT(11,3)'
- ],
- // Boolean
- [
- 'checked',
- ['type' => 'boolean', 'default' => false],
- '`checked` BOOLEAN DEFAULT FALSE'
- ],
- [
- 'checked',
- ['type' => 'boolean', 'default' => true, 'null' => false],
- '`checked` BOOLEAN NOT NULL DEFAULT TRUE'
- ],
- // datetimes
- [
- 'created',
- ['type' => 'datetime', 'comment' => 'Created timestamp'],
- '`created` DATETIME COMMENT "Created timestamp"'
- ],
- // Date & Time
- [
- 'start_date',
- ['type' => 'date'],
- '`start_date` DATE'
- ],
- [
- 'start_time',
- ['type' => 'time'],
- '`start_time` TIME'
- ],
- // timestamps
- [
- 'created',
- ['type' => 'timestamp', 'null' => true],
- '`created` TIMESTAMP NULL'
- ],
- [
- 'created',
- ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
- '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
- ],
- ];
- }
- /**
- * Test generating column definitions
- *
- * @dataProvider columnSqlProvider
- * @return void
- */
- public function testColumnSql($name, $data, $expected)
- {
- $driver = $this->_getMockedDriver();
- $schema = new MysqlSchema($driver);
- $table = (new Table('articles'))->addColumn($name, $data);
- $this->assertEquals($expected, $schema->columnSql($table, $name));
- }
- /**
- * Provide data for testing constraintSql
- *
- * @return array
- */
- public static function constraintSqlProvider()
- {
- return [
- [
- 'primary',
- ['type' => 'primary', 'columns' => ['title']],
- 'PRIMARY KEY (`title`)'
- ],
- [
- 'unique_idx',
- ['type' => 'unique', 'columns' => ['title', 'author_id']],
- 'UNIQUE KEY `unique_idx` (`title`, `author_id`)'
- ],
- [
- 'length_idx',
- [
- 'type' => 'unique',
- 'columns' => ['author_id', 'title'],
- 'length' => ['author_id' => 5, 'title' => 4]
- ],
- 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))'
- ],
- [
- 'author_id_idx',
- ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
- 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
- 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
- ],
- [
- 'author_id_idx',
- ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
- 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
- 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT'
- ],
- [
- 'author_id_idx',
- ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
- 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
- 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
- ],
- [
- 'author_id_idx',
- ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
- 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
- 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT'
- ],
- [
- 'author_id_idx',
- ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
- 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
- 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT'
- ],
- ];
- }
- /**
- * Test the constraintSql method.
- *
- * @dataProvider constraintSqlProvider
- */
- public function testConstraintSql($name, $data, $expected)
- {
- $driver = $this->_getMockedDriver();
- $schema = new MysqlSchema($driver);
- $table = (new Table('articles'))->addColumn('title', [
- 'type' => 'string',
- 'length' => 255
- ])->addColumn('author_id', [
- 'type' => 'integer',
- ])->addConstraint($name, $data);
- $this->assertEquals($expected, $schema->constraintSql($table, $name));
- }
- /**
- * Test provider for indexSql()
- *
- * @return array
- */
- public static function indexSqlProvider()
- {
- return [
- [
- 'key_key',
- ['type' => 'index', 'columns' => ['author_id']],
- 'KEY `key_key` (`author_id`)'
- ],
- [
- 'full_text',
- ['type' => 'fulltext', 'columns' => ['title']],
- 'FULLTEXT KEY `full_text` (`title`)'
- ],
- ];
- }
- /**
- * Test the indexSql method.
- *
- * @dataProvider indexSqlProvider
- */
- public function testIndexSql($name, $data, $expected)
- {
- $driver = $this->_getMockedDriver();
- $schema = new MysqlSchema($driver);
- $table = (new Table('articles'))->addColumn('title', [
- 'type' => 'string',
- 'length' => 255
- ])->addColumn('author_id', [
- 'type' => 'integer',
- ])->addIndex($name, $data);
- $this->assertEquals($expected, $schema->indexSql($table, $name));
- }
- /**
- * Test generating a column that is a primary key.
- *
- * @return void
- */
- public function testColumnSqlPrimaryKey()
- {
- $driver = $this->_getMockedDriver();
- $schema = new MysqlSchema($driver);
- $table = new Table('articles');
- $table->addColumn('id', [
- 'type' => 'integer',
- 'null' => false,
- ])
- ->addConstraint('primary', [
- 'type' => 'primary',
- 'columns' => ['id']
- ]);
- $result = $schema->columnSql($table, 'id');
- $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
- $table = new Table('articles');
- $table->addColumn('id', [
- 'type' => 'biginteger',
- 'null' => false
- ])
- ->addConstraint('primary', [
- 'type' => 'primary',
- 'columns' => ['id']
- ]);
- $result = $schema->columnSql($table, 'id');
- $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
- }
- /**
- * Integration test for converting a Schema\Table into MySQL table creates.
- *
- * @return void
- */
- public function testCreateSql()
- {
- $driver = $this->_getMockedDriver();
- $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $connection->expects($this->any())->method('driver')
- ->will($this->returnValue($driver));
- $table = (new Table('posts'))->addColumn('id', [
- 'type' => 'integer',
- 'null' => false
- ])
- ->addColumn('title', [
- 'type' => 'string',
- 'null' => false,
- 'comment' => 'The title'
- ])
- ->addColumn('body', [
- 'type' => 'text',
- 'comment' => ''
- ])
- ->addColumn('created', 'datetime')
- ->addConstraint('primary', [
- 'type' => 'primary',
- 'columns' => ['id']
- ])
- ->options([
- 'engine' => 'InnoDB',
- 'charset' => 'utf8',
- 'collate' => 'utf8_general_ci',
- ]);
- $expected = <<<SQL
- CREATE TABLE `posts` (
- `id` INTEGER NOT NULL AUTO_INCREMENT,
- `title` VARCHAR(255) NOT NULL COMMENT "The title",
- `body` TEXT,
- `created` DATETIME,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
- SQL;
- $result = $table->createSql($connection);
- $this->assertCount(1, $result);
- $this->assertTextEquals($expected, $result[0]);
- }
- /**
- * Tests creating temporary tables
- *
- * @return void
- */
- public function testCreateTemporary()
- {
- $driver = $this->_getMockedDriver();
- $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $connection->expects($this->any())->method('driver')
- ->will($this->returnValue($driver));
- $table = (new Table('schema_articles'))->addColumn('id', [
- 'type' => 'integer',
- 'null' => false
- ]);
- $table->temporary(true);
- $sql = $table->createSql($connection);
- $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
- }
- /**
- * Test primary key generation & auto-increment.
- *
- * @return void
- */
- public function testCreateSqlCompositeIntegerKey()
- {
- $driver = $this->_getMockedDriver();
- $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $connection->expects($this->any())->method('driver')
- ->will($this->returnValue($driver));
- $table = (new Table('articles_tags'))
- ->addColumn('article_id', [
- 'type' => 'integer',
- 'null' => false
- ])
- ->addColumn('tag_id', [
- 'type' => 'integer',
- 'null' => false,
- ])
- ->addConstraint('primary', [
- 'type' => 'primary',
- 'columns' => ['article_id', 'tag_id']
- ]);
- $expected = <<<SQL
- CREATE TABLE `articles_tags` (
- `article_id` INTEGER NOT NULL,
- `tag_id` INTEGER NOT NULL,
- PRIMARY KEY (`article_id`, `tag_id`)
- )
- SQL;
- $result = $table->createSql($connection);
- $this->assertCount(1, $result);
- $this->assertTextEquals($expected, $result[0]);
- $table = (new Table('composite_key'))
- ->addColumn('id', [
- 'type' => 'integer',
- 'null' => false,
- 'autoIncrement' => true
- ])
- ->addColumn('account_id', [
- 'type' => 'integer',
- 'null' => false,
- ])
- ->addConstraint('primary', [
- 'type' => 'primary',
- 'columns' => ['id', 'account_id']
- ]);
- $expected = <<<SQL
- CREATE TABLE `composite_key` (
- `id` INTEGER NOT NULL AUTO_INCREMENT,
- `account_id` INTEGER NOT NULL,
- PRIMARY KEY (`id`, `account_id`)
- )
- SQL;
- $result = $table->createSql($connection);
- $this->assertCount(1, $result);
- $this->assertTextEquals($expected, $result[0]);
- }
- /**
- * test dropSql
- *
- * @return void
- */
- public function testDropSql()
- {
- $driver = $this->_getMockedDriver();
- $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $connection->expects($this->any())->method('driver')
- ->will($this->returnValue($driver));
- $table = new Table('articles');
- $result = $table->dropSql($connection);
- $this->assertCount(1, $result);
- $this->assertEquals('DROP TABLE `articles`', $result[0]);
- }
- /**
- * Test truncateSql()
- *
- * @return void
- */
- public function testTruncateSql()
- {
- $driver = $this->_getMockedDriver();
- $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $connection->expects($this->any())->method('driver')
- ->will($this->returnValue($driver));
- $table = new Table('articles');
- $result = $table->truncateSql($connection);
- $this->assertCount(1, $result);
- $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
- }
- /**
- * Test that constructing a schema dialect connects the driver.
- *
- * @return void
- */
- public function testConstructConnectsDriver()
- {
- $driver = $this->getMock('Cake\Database\Driver');
- $driver->expects($this->once())
- ->method('connect');
- $schema = new MysqlSchema($driver);
- }
- /**
- * Get a schema instance with a mocked driver/pdo instances
- *
- * @return MysqlSchema
- */
- protected function _getMockedDriver()
- {
- $driver = new \Cake\Database\Driver\Mysql();
- $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
- $mock->expects($this->any())
- ->method('quote')
- ->will($this->returnCallback(function ($value) {
- return '"' . $value . '"';
- }));
- $mock->expects($this->any())
- ->method('quoteIdentifier')
- ->will($this->returnCallback(function ($value) {
- return '`' . $value . '`';
- }));
- $driver->connection($mock);
- return $driver;
- }
- }
|