From a54e8f238e7a8fc5ba4d877ec5e3d1786a71bf58 Mon Sep 17 00:00:00 2001 From: Gilles Wittenberg Date: Thu, 29 Aug 2013 09:13:54 +0200 Subject: [PATCH] Add y/yes option to schema create and update subcommands --- lib/Cake/Console/Command/SchemaShell.php | 15 ++++-- .../Case/Console/Command/SchemaShellTest.php | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Console/Command/SchemaShell.php b/lib/Cake/Console/Command/SchemaShell.php index 223b1179b..58f03d86c 100644 --- a/lib/Cake/Console/Command/SchemaShell.php +++ b/lib/Cake/Console/Command/SchemaShell.php @@ -333,7 +333,7 @@ class SchemaShell extends AppShell { $this->out("\n" . __d('cake_console', 'The following table(s) will be dropped.')); $this->out(array_keys($drop)); - if ($this->in(__d('cake_console', 'Are you sure you want to drop the table(s)?'), array('y', 'n'), 'n') === 'y') { + if (!empty($this->params['yes']) || $this->in(__d('cake_console', 'Are you sure you want to drop the table(s)?'), array('y', 'n'), 'n') === 'y') { $this->out(__d('cake_console', 'Dropping table(s).')); $this->_run($drop, 'drop', $Schema); } @@ -341,7 +341,7 @@ class SchemaShell extends AppShell { $this->out("\n" . __d('cake_console', 'The following table(s) will be created.')); $this->out(array_keys($create)); - if ($this->in(__d('cake_console', 'Are you sure you want to create the table(s)?'), array('y', 'n'), 'y') === 'y') { + if (!empty($this->params['yes']) || $this->in(__d('cake_console', 'Are you sure you want to create the table(s)?'), array('y', 'n'), 'y') === 'y') { $this->out(__d('cake_console', 'Creating table(s).')); $this->_run($create, 'create', $Schema); } @@ -392,7 +392,7 @@ class SchemaShell extends AppShell { $this->out("\n" . __d('cake_console', 'The following statements will run.')); $this->out(array_map('trim', $contents)); - if ($this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n') === 'y') { + if (!empty($this->params['yes']) || $this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n') === 'y') { $this->out(); $this->out(__d('cake_console', 'Updating Database...')); $this->_run($contents, 'update', $Schema); @@ -496,6 +496,11 @@ class SchemaShell extends AppShell { $exclude = array( 'help' => __d('cake_console', 'Tables to exclude as comma separated list.') ); + $yes = array( + 'short' => 'y', + 'help' => __d('cake_console', 'Do not prompt for confirmation. Be careful!'), + 'boolean' => true + ); $parser = parent::getOptionParser(); $parser->description( @@ -523,7 +528,7 @@ class SchemaShell extends AppShell { ))->addSubcommand('create', array( 'help' => __d('cake_console', 'Drop and create tables based on the schema file.'), 'parser' => array( - 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'), + 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot', 'yes'), 'args' => array( 'name' => array( 'help' => __d('cake_console', 'Name of schema to use.') @@ -536,7 +541,7 @@ class SchemaShell extends AppShell { ))->addSubcommand('update', array( 'help' => __d('cake_console', 'Alter the tables based on the schema file.'), 'parser' => array( - 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot', 'force'), + 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot', 'force', 'yes'), 'args' => array( 'name' => array( 'help' => __d('cake_console', 'Name of schema to use.') diff --git a/lib/Cake/Test/Case/Console/Command/SchemaShellTest.php b/lib/Cake/Test/Case/Console/Command/SchemaShellTest.php index a78c0fbdd..4f4c96c35 100644 --- a/lib/Cake/Test/Case/Console/Command/SchemaShellTest.php +++ b/lib/Cake/Test/Case/Console/Command/SchemaShellTest.php @@ -426,6 +426,29 @@ class SchemaShellTest extends CakeTestCase { $this->assertContains('public $aros_acos = array(', $contents); } +/** + * Test schema run create with --yes option + * + * @return void + */ + public function testCreateOptionYes() { + $this->Shell = $this->getMock( + 'SchemaShell', + array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'), + array(&$this->Dispatcher) + ); + + $this->Shell->params = array( + 'connection' => 'test', + 'yes' => true, + ); + $this->Shell->args = array('i18n'); + $this->Shell->expects($this->never())->method('in'); + $this->Shell->expects($this->exactly(2))->method('_run'); + $this->Shell->startup(); + $this->Shell->create(); + } + /** * Test schema run create with no table args. * @@ -536,6 +559,33 @@ class SchemaShellTest extends CakeTestCase { $this->Shell->update(); } +/** + * test run update with --yes option + * + * @return void + */ + public function testUpdateWithOptionYes() { + $this->Shell = $this->getMock( + 'SchemaShell', + array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'), + array(&$this->Dispatcher) + ); + + $this->Shell->params = array( + 'connection' => 'test', + 'force' => true, + 'yes' => true, + ); + $this->Shell->args = array('SchemaShellTest', 'articles'); + $this->Shell->startup(); + $this->Shell->expects($this->never())->method('in'); + $this->Shell->expects($this->once()) + ->method('_run') + ->with($this->arrayHasKey('articles'), 'update', $this->isInstanceOf('CakeSchema')); + + $this->Shell->update(); + } + /** * test that the plugin param creates the correct path in the schema object. *