Backporting bugfix for #22 from 1.3-console to 1.2. Adds schema shell test cases, fixes for schema object references, i18n of schema schell output strings.

This commit is contained in:
jperras 2009-09-06 21:05:24 -04:00
parent bcbdb1cf1d
commit 24351b5a21
3 changed files with 375 additions and 20 deletions

View file

@ -1,5 +1,4 @@
<?php <?php
/* SVN FILE: $Id$ */
/** /**
* Command-line database management utility to automate programmer chores. * Command-line database management utility to automate programmer chores.
* *
@ -9,20 +8,17 @@
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* *
* Licensed under The MIT License * Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
* @filesource * @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake * @package cake
* @subpackage cake.cake.console.libs * @subpackage cake.cake.console.libs
* @since CakePHP(tm) v 1.2.0.5550 * @since CakePHP(tm) v 1.2.0.5550
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
App::import('File'); App::import('File');
@ -116,7 +112,7 @@ class SchemaShell extends Shell {
* @access public * @access public
*/ */
function generate() { function generate() {
$this->out('Generating Schema...'); $this->out(__('Generating Schema...', true));
$options = array(); $options = array();
if (isset($this->params['f'])) { if (isset($this->params['f'])) {
$options = array('models' => false); $options = array('models' => false);
@ -131,7 +127,7 @@ class SchemaShell extends Shell {
$snapshot = true; $snapshot = true;
$result = strtolower($this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's')); $result = strtolower($this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's'));
if ($result === 'q') { if ($result === 'q') {
$this->_stop(); return $this->_stop();
} }
if ($result === 'o') { if ($result === 'o') {
$snapshot = false; $snapshot = false;
@ -224,7 +220,7 @@ class SchemaShell extends Shell {
*/ */
function run() { function run() {
if (!isset($this->args[0])) { if (!isset($this->args[0])) {
$this->err('command not found'); $this->err(__('Command not found', true));
$this->_stop(); $this->_stop();
} }
@ -271,8 +267,8 @@ class SchemaShell extends Shell {
$this->__update($Schema, $table); $this->__update($Schema, $table);
break; break;
default: default:
$this->err(__('command not found', true)); $this->err(__('Command not found', true));
$this->_stop(); $this->_stop();
} }
} }
/** /**
@ -281,7 +277,7 @@ class SchemaShell extends Shell {
* *
* @access private * @access private
*/ */
function __create($Schema, $table = null) { function __create(&$Schema, $table = null) {
$db =& ConnectionManager::getDataSource($this->Schema->connection); $db =& ConnectionManager::getDataSource($this->Schema->connection);
$drop = $create = array(); $drop = $create = array();
@ -304,7 +300,7 @@ class SchemaShell extends Shell {
$this->out(array_keys($drop)); $this->out(array_keys($drop));
if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) { if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) {
$this->out('Dropping table(s).'); $this->out(__('Dropping table(s).', true));
$this->__run($drop, 'drop', $Schema); $this->__run($drop, 'drop', $Schema);
} }
@ -312,7 +308,7 @@ class SchemaShell extends Shell {
$this->out(array_keys($create)); $this->out(array_keys($create));
if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) { if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) {
$this->out('Creating table(s).'); $this->out(__('Creating table(s).', true));
$this->__run($create, 'create', $Schema); $this->__run($create, 'create', $Schema);
} }
@ -324,11 +320,15 @@ class SchemaShell extends Shell {
* *
* @access private * @access private
*/ */
function __update($Schema, $table = null) { function __update(&$Schema, $table = null) {
$db =& ConnectionManager::getDataSource($this->Schema->connection); $db =& ConnectionManager::getDataSource($this->Schema->connection);
$this->out('Comparing Database to Schema...'); $this->out(__('Comparing Database to Schema...', true));
$Old = $this->Schema->read(); $options = array();
if (isset($this->params['f'])) {
$options['models'] = false;
}
$Old = $this->Schema->read($options);
$compare = $this->Schema->compare($Old, $Schema); $compare = $this->Schema->compare($Old, $Schema);
$contents = array(); $contents = array();
@ -361,7 +361,7 @@ class SchemaShell extends Shell {
* *
* @access private * @access private
*/ */
function __run($contents, $event, $Schema) { function __run($contents, $event, &$Schema) {
if (empty($contents)) { if (empty($contents)) {
$this->err(__('Sql could not be run', true)); $this->err(__('Sql could not be run', true));
return; return;
@ -369,7 +369,7 @@ class SchemaShell extends Shell {
Configure::write('debug', 2); Configure::write('debug', 2);
$db =& ConnectionManager::getDataSource($this->Schema->connection); $db =& ConnectionManager::getDataSource($this->Schema->connection);
$db->fullDebug = true; $db->fullDebug = true;
foreach ($contents as $table => $sql) { foreach ($contents as $table => $sql) {
if (empty($sql)) { if (empty($sql)) {
$this->out(sprintf(__('%s is up to date.', true), $table)); $this->out(sprintf(__('%s is up to date.', true), $table));
@ -403,7 +403,8 @@ class SchemaShell extends Shell {
* @access public * @access public
*/ */
function help() { function help() {
$this->out("The Schema Shell generates a schema object from \n\t\tthe database and updates the database from the schema."); $this->out("The Schema Shell generates a schema object from");
$this->out("the database and updates the database from the schema.");
$this->hr(); $this->hr();
$this->out("Usage: cake schema <command> <arg1> <arg2>..."); $this->out("Usage: cake schema <command> <arg1> <arg2>...");
$this->hr(); $this->hr();

View file

@ -172,6 +172,7 @@ class CakeSchema extends Object {
* - 'connection' - the db connection to use * - 'connection' - the db connection to use
* - 'name' - name of the schema * - 'name' - name of the schema
* - 'models' - a list of models to use, or false to ignore models * - 'models' - a list of models to use, or false to ignore models
*
* @param array $options schema object properties * @param array $options schema object properties
* @return array Array indexed by name and tables * @return array Array indexed by name and tables
* @access public * @access public

View file

@ -0,0 +1,353 @@
<?php
/**
* SchemaShellTest Test file
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2006-2009, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.tests.cases.console.libs.Shells
* @since CakePHP v 1.3
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Shell', 'Shell', false);
App::import('Model', 'Schema', false);
if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
}
if (!class_exists('ShellDispatcher')) {
ob_start();
$argv = false;
require CAKE . 'console' . DS . 'cake.php';
ob_end_clean();
}
if (!class_exists('SchemaShell')) {
require CAKE . 'console' . DS . 'libs' . DS . 'schema.php';
}
Mock::generatePartial(
'ShellDispatcher', 'TestSchemaShellMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
Mock::generatePartial(
'SchemaShell', 'MockSchemaShell',
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop')
);
Mock::generate('CakeSchema', 'MockSchemaCakeSchema');
/**
* Test for Schema database management
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class SchemaShellTestSchema extends CakeSchema {
/**
* name property
*
* @var string 'MyApp'
* @access public
*/
var $name = 'SchemaShellTest';
/**
* connection property
*
* @var string 'test_suite'
* @access public
*/
var $connection = 'test_suite';
/**
* comments property
*
* @var array
* @access public
*/
var $comments = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'user_id' => array('type' => 'integer', 'null' => false),
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
);
/**
* posts property
*
* @var array
* @access public
*/
var $articles = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
'body' => array('type' => 'text', 'null' => true, 'default' => null),
'summary' => array('type' => 'text', 'null' => true),
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
);
}
/**
* SchemaShellTest class
*
* @package cake
* @subpackage cake.tests.cases.console.libs.Shells
*/
class SchemaShellTest extends CakeTestCase {
var $fixtures = array('core.article', 'core.user');
/**
* startTest method
*
* @return void
* @access public
*/
function startTest() {
$this->Dispatcher =& new TestSchemaShellMockShellDispatcher();
$this->Shell =& new MockSchemaShell($this->Dispatcher);
$this->Shell->Dispatch =& $this->Dispatcher;
}
/**
* endTest method
*
* @return void
* @access public
*/
function endTest() {
ClassRegistry::flush();
}
/**
* test startup method
*
* @return void
**/
function testStartup() {
$this->Shell->startup();
$this->assertTrue(isset($this->Shell->Schema));
$this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
$this->assertEqual(strtolower($this->Shell->Schema->name), APP_DIR);
$this->assertEqual($this->Shell->Schema->file, 'schema.php');
unset($this->Shell->Schema);
$this->Shell->params = array(
'name' => 'TestSchema'
);
$this->Shell->startup();
$this->assertEqual($this->Shell->Schema->name, 'TestSchema');
$this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
$this->assertEqual($this->Shell->Schema->connection, 'default');
$this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'sql');
unset($this->Shell->Schema);
$this->Shell->params = array(
'file' => 'other_file.php',
'connection' => 'test_suite',
'path' => '/test/path'
);
$this->Shell->startup();
$this->assertEqual(strtolower($this->Shell->Schema->name), APP_DIR);
$this->assertEqual($this->Shell->Schema->file, 'other_file.php');
$this->assertEqual($this->Shell->Schema->connection, 'test_suite');
$this->assertEqual($this->Shell->Schema->path, '/test/path');
}
/**
* Test View - and that it dumps the schema file to stdout
*
* @return void
**/
function testView() {
$this->Shell->startup();
$this->Shell->Schema->path = APP . 'config' . DS . 'schema';
$this->Shell->params['file'] = 'i18n.php';
$this->Shell->expectOnce('_stop');
$this->Shell->view();
}
/**
* test dump() with sql file generation
*
* @return void
**/
function testDumpWithFileWriting() {
$file =& new File(APP . 'config' . DS . 'sql' . DS . 'i18n.php');
$contents = $file->read();
$file =& new File(TMP . 'tests' . DS . 'i18n.php');
$file->write($contents);
$this->Shell->params = array('name' => 'i18n');
$this->Shell->args = array('write');
$this->Shell->startup();
$this->Shell->Schema->path = TMP . 'tests';
$this->Shell->dump();
$sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
$contents = $sql->read();
$this->assertPattern('/DROP TABLE/', $contents);
$this->assertPattern('/CREATE TABLE `i18n`/', $contents);
$this->assertPattern('/id/', $contents);
$this->assertPattern('/model/', $contents);
$this->assertPattern('/field/', $contents);
$this->assertPattern('/locale/', $contents);
$this->assertPattern('/foreign_key/', $contents);
$this->assertPattern('/content/', $contents);
$sql->delete();
$file->delete();
}
/**
* test generate with snapshot generation
*
* @return void
*/
function testGenerateSnaphot() {
$this->Shell->path = TMP;
$this->Shell->params['file'] = 'schema.php';
$this->Shell->args = array('snapshot');
$this->Shell->Schema =& new MockSchemaCakeSchema();
$this->Shell->Schema->setReturnValue('read', array('schema data'));
$this->Shell->Schema->setReturnValue('write', true);
$this->Shell->Schema->expectOnce('read');
$this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema_1.php')));
$this->Shell->generate();
}
/**
* test generate without a snapshot.
*
* @return void
**/
function testGenerateNoOverwrite() {
touch(TMP . 'schema.php');
$this->Shell->params['file'] = 'schema.php';
$this->Shell->args = array();
$this->Shell->setReturnValue('in', 'q');
$this->Shell->Schema =& new MockSchemaCakeSchema();
$this->Shell->Schema->path = TMP;
$this->Shell->Schema->expectNever('read');
$result = $this->Shell->generate();
unlink(TMP . 'schema.php');
}
/**
* test generate with overwriting of the schema files.
*
* @return void
**/
function testGenerateOverwrite() {
touch(TMP . 'schema.php');
$this->Shell->params['file'] = 'schema.php';
$this->Shell->args = array();
$this->Shell->setReturnValue('in', 'o');
$this->Shell->expectAt(1, 'out', array(new PatternExpectation('/Schema file:\s[a-z\.]+\sgenerated/')));
$this->Shell->Schema =& new MockSchemaCakeSchema();
$this->Shell->Schema->path = TMP;
$this->Shell->Schema->setReturnValue('read', array('schema data'));
$this->Shell->Schema->setReturnValue('write', true);
$this->Shell->Schema->expectOnce('read');
$this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema.php')));
$this->Shell->generate();
unlink(TMP . 'schema.php');
}
/**
* Test schema run create with no table args.
*
* @return void
**/
function testRunCreateNoArgs() {
$this->Shell->params = array(
'connection' => 'test_suite',
'name' => 'i18n',
'path' => APP . 'config' . DS . 'sql'
);
$this->Shell->args = array('create');
$this->Shell->startup();
$this->Shell->setReturnValue('in', 'y');
$this->Shell->run();
$db =& ConnectionManager::getDataSource('test_suite');
$sources = $db->listSources();
$this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
}
/**
* Test schema run create with no table args.
*
* @return void
**/
function testRunCreateWithTableArgs() {
$this->Shell->params = array(
'connection' => 'test_suite',
'name' => 'DbAcl',
'path' => APP . 'config' . DS . 'sql'
);
$this->Shell->args = array('create', 'acos');
$this->Shell->startup();
$this->Shell->setReturnValue('in', 'y');
$this->Shell->run();
$db =& ConnectionManager::getDataSource('test_suite');
$sources = $db->listSources();
$this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
$this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources));
$this->assertFalse(in_array('aros_acos', $sources));
}
/**
* test run update with a table arg.
*
* @return void
**/
function testRunUpdateWithTable() {
$this->Shell->params = array(
'name' => 'SchemaShellTest',
'connection' => 'test_suite',
'f' => true
);
$this->Shell->args = array('update', 'articles');
$this->Shell->startup();
$this->Shell->setReturnValue('in', 'y');
$this->Shell->run();
$article =& new Model(array('name' => 'Article', 'ds' => 'test_suite'));
$fields = $article->schema();
$this->assertTrue(isset($fields['summary']));
$this->_fixtures['core.article']->drop($this->db);
$this->_fixtures['core.article']->create($this->db);
}
}
?>