Skip to content

Commit

Permalink
Add more column types and wire up mocks better.
Browse files Browse the repository at this point in the history
Since I moved around connection logic on a different branch, these
tests are a bit more manageable. Update to reflect the changes
elsewhere, and approach completion on the first create table generation.
  • Loading branch information
markstory committed May 6, 2013
1 parent 93d87f8 commit 0461394
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
27 changes: 24 additions & 3 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -160,8 +160,16 @@ public function extraSchemaColumns() {
];
}

/**
* Generate the SQL to create a table.
*
* @param string $table The name of the table.
* @param array $lines The lines (columns + indexes) to go inside the table.
* @return string A complete CREATE TABLE statement
*/
public function createTableSql($table, $lines) {

$content = implode(",\n", $lines);
return sprintf("CREATE TABLE `%s` (\n%s\n);", $table, $content);
}

/**
Expand All @@ -181,7 +189,7 @@ public function columnSql($name, $data) {
}
break;
case 'integer':
$out .= ' INT';
$out .= ' INTEGER';
break;
case 'biginteger':
$out .= ' BIGINT';
Expand All @@ -195,6 +203,8 @@ public function columnSql($name, $data) {
case 'datetime':
$out .= ' DATETIME';
break;
case 'timestamp':
$out .= ' TIMESTAMP';
break;
}
$hasLength = [
Expand All @@ -206,9 +216,20 @@ public function columnSql($name, $data) {
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (isset($data['default'])) {
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'timestamp') {
$out .= ' DEFAULT ' . $this->_value($data['default']);
}
if (
isset($data['default']) &&
$data['type'] === 'timestamp' &&
strtolower($data['default']) === 'current_timestamp'
) {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
}
if (isset($data['comment'])) {
$out .= ' COMMENT ' . $this->_value($data['comment']);
}
Expand Down
53 changes: 42 additions & 11 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -276,6 +276,11 @@ public static function columnSqlProvider() {
['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],
Expand All @@ -301,7 +306,7 @@ public static function columnSqlProvider() {
[
'post_id',
['type' => 'integer', 'length' => 11],
'`post_id` INT(11)'
'`post_id` INTEGER(11)'
],
[
'post_id',
Expand All @@ -326,7 +331,16 @@ public static function columnSqlProvider() {
'`created` DATETIME COMMENT "Created timestamp"'
],
// timestamps
// TODO add timestamps including CURRENT_TIMESTAMP
[
'created',
['type' => 'timestamp', 'null' => true],
'`created` TIMESTAMP NULL'
],
[
'created',
['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
'`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
],
];
}

Expand All @@ -338,6 +352,15 @@ public static function columnSqlProvider() {
*/
public function testColumnSql($name, $data, $expected) {
$driver = new \Cake\Database\Driver\Mysql();

$mock = $this->getMock('FakePdo', ['quote']);
$mock->expects($this->any())
->method('quote')
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));

$driver->connection($mock);
$dialect = new MysqlSchema($driver);
$this->assertEquals($expected, $dialect->columnSql($name, $data));
}
Expand Down Expand Up @@ -365,25 +388,33 @@ public function testCreateTableSql() {
]);

$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
$driver = $this->getMock('Cake\Database\Driver\Mysql');
$driver = new \Cake\Database\Driver\Mysql();
$mock = $this->getMock('FakePdo', ['quote']);
$driver->connection($mock);

$dialect = new MysqlSchema($driver);

$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));
$driver->expects($this->any())
->method('schemaDialect')
->will($this->returnValue($dialect));

$mock->expects($this->any())
->method('quote')
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));

$result = $table->createTableSql($connection);
$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`)
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL COMMENT "The title",
`body` TEXT,
`created` DATETIME,
PRIMARY KEY (`id`)
);
SQL;
$this->assertEquals($expected, $result);
}


}

0 comments on commit 0461394

Please sign in to comment.