Browse Source

Add switches for fixture/test disabling.

Have options for disabling test/fixture generation as people might want
to skip that.
mark_story 12 years ago
parent
commit
de5ca3625c

+ 37 - 20
src/Console/Command/Task/ModelTask.php

@@ -106,20 +106,26 @@ class ModelTask extends BakeTask {
 			return $this->all();
 		}
 
-		$model = $this->args[0];
+		$this->generate($this->args[0]);
+	}
+
+/**
+ * Generate code for the given model name.
+ *
+ * @param string $name The model name to generate.
+ * @return void
+ */
+	public function generate($name) {
 		$table = $this->getTable();
 
-		$object = $this->getTableObject($model, $table);
+		$object = $this->getTableObject($name, $table);
 		$associations = $this->getAssociations($object);
 		$primaryKey = $this->getPrimaryKey($model);
 		$displayField = $this->getDisplayField($model);
 
-		if ($this->bake($object, false)) {
-			if ($this->_checkUnitTest()) {
-				$this->bakeFixture($model, $useTable);
-				$this->bakeTest($model);
-			}
-		}
+		$this->bake($object, false);
+		$this->bakeFixture($model, $useTable);
+		$this->bakeTest($model);
 	}
 
 /**
@@ -848,18 +854,6 @@ class ModelTask extends BakeTask {
 	}
 
 /**
- * Assembles and writes a unit test file
- *
- * @param string $className Model class name
- * @return string
- */
-	public function bakeTest($className) {
-		$this->Test->plugin = $this->plugin;
-		$this->Test->connection = $this->connection;
-		return $this->Test->bake('Model', $className);
-	}
-
-/**
  * Outputs the a list of possible models or controllers from database
  *
  * @param string $useDbConfig Database configuration name
@@ -969,6 +963,10 @@ class ModelTask extends BakeTask {
 			'help' => __d('cake_console', 'The primary key if you would like to manually set one.')
 		])->addOption('display-field', [
 			'help' => __d('cake_console', 'The displayField if you would like to choose one.')
+		])->addOption('no-test', [
+			'help' => __d('cake_console', 'Do not generate a test case skeleton.')
+		])->addOption('no-fixture', [
+			'help' => __d('cake_console', 'Do not generate a test fixture skeleton.')
 		])->epilog(
 			__d('cake_console', 'Omitting all arguments and options will list ' .
 				'the table names you can generate models for')
@@ -986,9 +984,28 @@ class ModelTask extends BakeTask {
  * @see FixtureTask::bake
  */
 	public function bakeFixture($className, $useTable = null) {
+		if (!empty($this->params['no-fixture'])) {
+			return;
+		}
 		$this->Fixture->connection = $this->connection;
 		$this->Fixture->plugin = $this->plugin;
 		$this->Fixture->bake($className, $useTable);
 	}
 
+/**
+ * Assembles and writes a unit test file
+ *
+ * @param string $className Model class name
+ * @return string
+ */
+	public function bakeTest($className) {
+		if (!empty($this->params['no-test'])) {
+			return;
+		}
+		$this->Test->plugin = $this->plugin;
+		$this->Test->connection = $this->connection;
+		return $this->Test->bake('Model', $className);
+	}
+
+
 }

+ 32 - 6
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -615,10 +615,10 @@ class ModelTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeFixture() {
-		$this->markTestIncomplete('Not done here yet');
 		$this->Task->plugin = 'TestPlugin';
-		$this->Task->interactive = true;
-		$this->Task->Fixture->expects($this->at(0))->method('bake')->with('BakeArticle', 'bake_articles');
+		$this->Task->Fixture->expects($this->at(0))
+			->method('bake')
+			->with('BakeArticle', 'bake_articles');
 		$this->Task->bakeFixture('BakeArticle', 'bake_articles');
 
 		$this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
@@ -627,15 +627,28 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * Ensure that the fixture baking can be disabled
+ *
+ * @return void
+ */
+	public function testBakeFixtureDisabled() {
+		$this->Task->params['no-fixture'] = true;
+		$this->Task->plugin = 'TestPlugin';
+		$this->Task->Fixture->expects($this->never())
+			->method('bake');
+		$this->Task->bakeFixture('BakeArticle', 'bake_articles');
+	}
+
+/**
  * Ensure that the test object is correctly called.
  *
  * @return void
  */
 	public function testBakeTest() {
-		$this->markTestIncomplete('Not done here yet');
 		$this->Task->plugin = 'TestPlugin';
-		$this->Task->interactive = true;
-		$this->Task->Test->expects($this->at(0))->method('bake')->with('Model', 'BakeArticle');
+		$this->Task->Test->expects($this->at(0))
+			->method('bake')
+			->with('Model', 'BakeArticle');
 		$this->Task->bakeTest('BakeArticle');
 
 		$this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
@@ -644,6 +657,19 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * Ensure that test baking can be disabled.
+ *
+ * @return void
+ */
+	public function testBakeTestDisabled() {
+		$this->Task->params['no-test'] = true;
+		$this->Task->plugin = 'TestPlugin';
+		$this->Task->Test->expects($this->never())
+			->method('bake');
+		$this->Task->bakeTest('BakeArticle');
+	}
+
+/**
  * test confirming of associations, and that when an association is hasMany
  * a question for the hasOne is also not asked.
  *