Browse Source

Merge pull request #4144 from cakephp/issue-4135

Fix plugin option missing on all, and add missing option on fixture task
José Lorenzo Rodríguez 11 years ago
parent
commit
6b42c131bd

+ 1 - 4
src/Console/Command/Task/ControllerTask.php

@@ -66,11 +66,8 @@ class ControllerTask extends BakeTask {
  * @return void
  */
 	public function all() {
-		$controllersCreated = 0;
 		foreach ($this->listAll() as $table) {
-			$controller = $this->_controllerName($table);
-			$this->bake($controller);
-			$controllersCreated++;
+			$this->main($table);
 		}
 	}
 

+ 23 - 44
src/Console/Command/Task/FixtureTask.php

@@ -71,12 +71,17 @@ class FixtureTask extends BakeTask {
 			'short' => 's',
 			'boolean' => true
 		])->addOption('records', [
-			'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', 'Generate a fixture with records from the non-test database. Used with --count and --conditions to limit which records are added to the fixture.'),
 			'short' => 'r',
 			'boolean' => true
+		])->addOption('import-records', [
+			'help' => __d('cake_console', 'Set to true to import records from the live table when the generated fixture is used.'),
+			'boolean' => true
 		])->addOption('conditions', [
 			'help' => __d('cake_console', 'The SQL snippet to use when importing records.'),
 			'default' => '1=1',
+		])->addSubcommand('all', [
+			'help' => __d('cake_console', 'Bake all fixture files for tables in the chosen connection.')
 		]);
 
 		return $parser;
@@ -118,31 +123,8 @@ class FixtureTask extends BakeTask {
 		$tables = $this->Model->listAll($this->connection, false);
 
 		foreach ($tables as $table) {
-			$model = $this->_modelName($table);
-			$importOptions = [];
-			if (!empty($this->params['schema'])) {
-				$importOptions['schema'] = $model;
-			}
-			$this->bake($model, false, $importOptions);
-		}
-	}
-/**
- * Interacts with the User to setup an array of import options. For a fixture.
- *
- * @param string $modelName Name of model you are dealing with.
- * @return array Array of import options.
- */
-	public function importOptions($modelName) {
-		$options = [];
-
-		if (!empty($this->params['schema'])) {
-			$options['schema'] = $modelName;
-		}
-		if (!empty($this->params['records'])) {
-			$options['records'] = true;
-			$options['fromTable'] = true;
+			$this->main($table);
 		}
-		return $options;
 	}
 
 /**
@@ -150,13 +132,11 @@ class FixtureTask extends BakeTask {
  *
  * @param string $model Name of model to bake.
  * @param string $useTable Name of table to use.
- * @param array $importOptions Options for public $import
  * @return string Baked fixture content
  * @throws \RuntimeException
  */
-	public function bake($model, $useTable = false, array $importOptions = []) {
+	public function bake($model, $useTable = false) {
 		$table = $schema = $records = $import = $modelImport = null;
-		$importBits = [];
 
 		if (!$useTable) {
 			$useTable = Inflector::tableize($model);
@@ -164,20 +144,19 @@ class FixtureTask extends BakeTask {
 			$table = $useTable;
 		}
 
-		if (!empty($importOptions)) {
-			if (isset($importOptions['schema'])) {
-				$modelImport = true;
-				$importBits[] = "'model' => '{$importOptions['schema']}'";
-			}
-			if (isset($importOptions['records'])) {
-				$importBits[] = "'records' => true";
-			}
-			if ($this->connection !== 'default') {
-				$importBits[] .= "'connection' => '{$this->connection}'";
-			}
-			if (!empty($importBits)) {
-				$import = sprintf("[%s]", implode(', ', $importBits));
-			}
+		$importBits = [];
+		if (!empty($this->params['schema'])) {
+			$modelImport = true;
+			$importBits[] = "'model' => '{$model}'";
+		}
+		if (!empty($this->params['import-records'])) {
+			$importBits[] = "'records' => true";
+		}
+		if (!empty($importBits) && $this->connection !== 'default') {
+			$importBits[] = "'connection' => '{$this->connection}'";
+		}
+		if (!empty($importBits)) {
+			$import = sprintf("[%s]", implode(', ', $importBits));
 		}
 
 		$connection = ConnectionManager::get($this->connection);
@@ -193,14 +172,14 @@ class FixtureTask extends BakeTask {
 			$schema = $this->_generateSchema($data);
 		}
 
-		if (empty($importOptions['records']) && !isset($importOptions['fromTable'])) {
+		if (empty($this->params['records']) && empty($this->params['import-records'])) {
 			$recordCount = 1;
 			if (isset($this->params['count'])) {
 				$recordCount = $this->params['count'];
 			}
 			$records = $this->_makeRecordString($this->_generateRecords($data, $recordCount));
 		}
-		if (!empty($this->params['records']) || isset($importOptions['fromTable'])) {
+		if (!empty($this->params['records']) && empty($this->params['import-records'])) {
 			$records = $this->_makeRecordString($this->_getRecordsFromTable($model, $useTable));
 		}
 		return $this->generateFixtureFile($model, compact('records', 'table', 'schema', 'import'));

+ 1 - 3
src/Console/Command/Task/ModelTask.php

@@ -129,9 +129,7 @@ class ModelTask extends BakeTask {
 			if (in_array($table, $this->skipTables)) {
 				continue;
 			}
-			$modelClass = $this->_modelName($table);
-			$this->out(__d('cake_console', 'Baking %s', $modelClass));
-			$this->bake($modelClass);
+			$this->main($table);
 		}
 	}
 

+ 49 - 74
tests/TestCase/Console/Command/Task/FixtureTaskTest.php

@@ -89,57 +89,15 @@ class FixtureTaskTest extends TestCase {
 	}
 
 /**
- * test importOptions with overwriting command line options.
- *
- * @return void
- */
-	public function testImportOptionsWithCommandLineOptions() {
-		$this->Task->params = ['schema' => true, 'records' => true];
-
-		$result = $this->Task->importOptions('Article');
-		$expected = ['fromTable' => true, 'schema' => 'Article', 'records' => true];
-		$this->assertEquals($expected, $result);
-	}
-
-/**
- * test importOptions with schema.
- *
- * @return void
- */
-	public function testImportOptionsWithSchema() {
-		$this->Task->params = ['schema' => true];
-
-		$result = $this->Task->importOptions('Articles');
-		$expected = ['schema' => 'Articles'];
-		$this->assertEquals($expected, $result);
-	}
-
-/**
- * test importOptions with records.
- *
- * @return void
- */
-	public function testImportOptionsWithRecords() {
-		$this->Task->params = array('records' => true);
-
-		$result = $this->Task->importOptions('Article');
-		$expected = array('fromTable' => true, 'records' => true);
-		$this->assertEquals($expected, $result);
-	}
-
-/**
  * test generating a fixture with database conditions.
  *
  * @return void
  */
 	public function testImportRecordsFromDatabaseWithConditionsPoo() {
 		$this->Task->connection = 'test';
+		$this->Task->params = ['schema' => true, 'records' => true];
 
-		$result = $this->Task->bake('Articles', false, array(
-			'fromTable' => true,
-			'schema' => 'Articles',
-			'records' => false
-		));
+		$result = $this->Task->bake('Articles');
 
 		$this->assertContains('namespace App\Test\Fixture;', $result);
 		$this->assertContains('use Cake\TestSuite\Fixture\TestFixture;', $result);
@@ -158,7 +116,8 @@ class FixtureTaskTest extends TestCase {
  */
 	public function testImportOptionsAlternateConnection() {
 		$this->Task->connection = 'test';
-		$result = $this->Task->bake('Article', false, array('schema' => 'Article'));
+		$this->Task->params = ['schema' => true];
+		$result = $this->Task->bake('Article');
 		$this->assertContains("'connection' => 'test'", $result);
 	}
 
@@ -168,20 +127,12 @@ class FixtureTaskTest extends TestCase {
  * @return void
  */
 	public function testImportRecordsNoEscaping() {
-		$db = ConnectionManager::get('test');
-		if ($db instanceof Sqlserver) {
-			$this->markTestSkipped('This test does not run on SQLServer');
-		}
-
 		$articles = TableRegistry::get('Articles');
 		$articles->updateAll(['body' => "Body \"value\""], []);
 
 		$this->Task->connection = 'test';
-		$result = $this->Task->bake('Article', false, array(
-			'fromTable' => true,
-			'schema' => 'Article',
-			'records' => false
-		));
+		$this->Task->params = ['schema' => 'true', 'records' => true];
+		$result = $this->Task->bake('Article');
 		$this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
 	}
 
@@ -321,29 +272,53 @@ class FixtureTaskTest extends TestCase {
 	public function testBake() {
 		$this->Task->connection = 'test';
 
-		$result = $this->Task->bake('Article');
-		$this->assertContains('class ArticleFixture extends TestFixture', $result);
-		$this->assertContains('public $fields', $result);
-		$this->assertContains('public $records', $result);
-		$this->assertNotContains('public $import', $result);
+		$this->Task->expects($this->at(0))
+			->method('createFile')
+			->with($this->anything(), $this->logicalAnd(
+				$this->stringContains('class ArticleFixture extends TestFixture'),
+				$this->stringContains('public $fields'),
+				$this->stringContains('public $records'),
+				$this->logicalNot($this->stringContains('public $import'))
+			));
+		$result = $this->Task->main('Article');
+	}
 
-		$result = $this->Task->bake('Article', 'comments');
-		$this->assertContains('class ArticleFixture extends TestFixture', $result);
-		$this->assertContains('public $table = \'comments\';', $result);
-		$this->assertContains('public $fields = [', $result);
+/**
+ * test main() with importing records
+ *
+ * @return void
+ */
+	public function testMainImportRecords() {
+		$this->Task->connection = 'test';
+		$this->Task->params = ['import-records' => true];
+
+		$this->Task->expects($this->at(0))
+			->method('createFile')
+			->with($this->anything(), $this->logicalAnd(
+				$this->stringContains("public \$import = ['records' => true, 'connection' => 'test'];"),
+				$this->logicalNot($this->stringContains('public $records'))
+			));
 
-		$result = $this->Task->bake('Article', 'comments', array('records' => true));
-		$this->assertContains("public \$import = ['records' => true, 'connection' => 'test'];", $result);
-		$this->assertNotContains('public $records', $result);
+		$this->Task->main('Article');
+	}
 
-		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
-		$this->assertContains("public \$import = ['model' => 'Article', 'connection' => 'test'];", $result);
-		$this->assertNotContains('public $fields', $result);
+/**
+ * test main() with importing schema.
+ *
+ * @return void
+ */
+	public function testMainImportSchema() {
+		$this->Task->connection = 'test';
+		$this->Task->params = ['schema' => true, 'import-records' => true];
 
-		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
-		$this->assertContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];", $result);
-		$this->assertNotContains('public $fields', $result);
-		$this->assertNotContains('public $records', $result);
+		$this->Task->expects($this->once())
+			->method('createFile')
+			->with($this->anything(), $this->logicalAnd(
+				$this->stringContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];"),
+				$this->logicalNot($this->stringContains('public $fields')),
+				$this->logicalNot($this->stringContains('public $records'))
+			));
+		$this->Task->bake('Article', 'comments');
 	}
 
 /**