add schema param and skip interactive prompts if unnecessary

This commit is contained in:
euromark 2013-05-02 00:19:46 +02:00
parent 62186ac8da
commit 0959c7008f
2 changed files with 91 additions and 5 deletions

View file

@ -83,8 +83,12 @@ class FixtureTask extends BakeTask {
))->addOption('plugin', array(
'help' => __d('cake_console', 'CamelCased name of the plugin to bake fixtures for.'),
'short' => 'p',
))->addOption('schema', array(
'help' => __d('cake_console', 'Importing schema for fixtures rather than hardcoding it.'),
'short' => 's',
'boolean' => true
))->addOption('records', array(
'help' => __d('cake_console', 'Used with --count and <name>/all commands to pull [n] records from the live tables, where [n] is either --count or the default of 10'),
'help' => __d('cake_console', 'Used with --count and <name>/all commands to pull [n] records from the live tables, where [n] is either --count or the default of 10.'),
'short' => 'r',
'boolean' => true
))->epilog(__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.'));
@ -124,9 +128,14 @@ class FixtureTask extends BakeTask {
$this->interactive = false;
$this->Model->interactive = false;
$tables = $this->Model->listAll($this->connection, false);
foreach ($tables as $table) {
$model = $this->_modelName($table);
$this->bake($model);
$importOptions = array();
if (!empty($this->params['schema'])) {
$importOptions['schema'] = $model;
}
$this->bake($model, false, $importOptions);
}
}
@ -158,11 +167,20 @@ class FixtureTask extends BakeTask {
*/
public function importOptions($modelName) {
$options = array();
if (!empty($this->params['schema'])) {
$options['schema'] = $modelName;
} else {
$doSchema = $this->in(__d('cake_console', 'Would you like to import schema for this fixture?'), array('y', 'n'), 'n');
if ($doSchema === 'y') {
$options['schema'] = $modelName;
}
}
if (!empty($this->params['records'])) {
$doRecords = 'y';
} else {
$doRecords = $this->in(__d('cake_console', 'Would you like to use record importing for this fixture?'), array('y', 'n'), 'n');
}
if ($doRecords === 'y') {
$options['records'] = true;
}

View file

@ -123,6 +123,48 @@ class FixtureTaskTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test importOptions with overwriting command line options.
*
* @return void
*/
public function testImportOptionsWithCommandLineOptions() {
$this->Task->params = array('schema' => true, 'records' => true);
$result = $this->Task->importOptions('Article');
$expected = array('schema' => 'Article', 'records' => true);
$this->assertEquals($expected, $result);
}
/**
* test importOptions with schema.
*
* @return void
*/
public function testImportOptionsWithSchema() {
$this->Task->params = array('schema' => true);
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
$result = $this->Task->importOptions('Article');
$expected = array('schema' => 'Article');
$this->assertEquals($expected, $result);
}
/**
* test importOptions with records.
*
* @return void
*/
public function testImportOptionsWithRecords() {
$this->Task->params = array('records' => true);
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
$result = $this->Task->importOptions('Article');
$expected = array('records' => true);
$this->assertEquals($expected, $result);
}
/**
* test importOptions choosing from Table.
*
@ -272,6 +314,32 @@ class FixtureTaskTest extends CakeTestCase {
$this->Task->all();
}
/**
* test using all() with -schema
*
* @return void
*/
public function testAllWithSchemaImport() {
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('all');
$this->Task->params = array('schema' => true);
$this->Task->Model->expects($this->any())->method('listAll')
->will($this->returnValue(array('Articles', 'comments')));
$filename = '/my/path/ArticleFixture.php';
$this->Task->expects($this->at(0))->method('createFile')
->with($filename, $this->stringContains('public $import = array(\'model\' => \'Article\''));
$filename = '/my/path/CommentFixture.php';
$this->Task->expects($this->at(1))->method('createFile')
->with($filename, $this->stringContains('public $import = array(\'model\' => \'Comment\''));
$this->Task->expects($this->exactly(2))->method('createFile');
$this->Task->all();
}
/**
* test interactive mode of execute
*