Browse Source

If a controller does not exist guess at the model name and generate views.

If we don't have a concrete controller, we can use the controller name
as a guess for the table class name. From there we can generate views.
They may not be the best views but better than an error.
mark_story 12 years ago
parent
commit
8ac7fc5b4a

+ 9 - 13
src/Console/Command/Task/ViewTask.php

@@ -18,6 +18,7 @@ use Cake\Console\Shell;
 use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\ORM\Table;
+use Cake\ORM\TableRegistry;
 use Cake\Utility\Inflector;
 
 /**
@@ -248,21 +249,16 @@ class ViewTask extends BakeTask {
 			$plugin = $this->plugin . '.';
 		}
 
-		if (!class_exists($this->controllerClass)) {
-			$file = $this->controllerClass . '.php';
-			$this->err(__d(
-				'cake_console',
-				"The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.",
-				str_replace('\\', '/', $file)
-			));
-			return $this->_stop();
+		if (class_exists($this->controllerClass)) {
+			$controllerObj = new $this->controllerClass();
+			$controllerObj->plugin = $this->plugin;
+			$controllerObj->constructClasses();
+			$modelClass = $controllerObj->modelClass;
+			$modelObj = $controllerObj->{$modelClass};
+		} else {
+			$modelObj = TableRegistry::get($this->controllerName);
 		}
 
-		$controllerObj = new $this->controllerClass();
-		$controllerObj->plugin = $this->plugin;
-		$controllerObj->constructClasses();
-		$modelClass = $controllerObj->modelClass;
-		$modelObj = $controllerObj->{$modelClass};
 
 		if ($modelObj) {
 			$primaryKey = (array)$modelObj->primaryKey();

+ 24 - 45
tests/TestCase/Console/Command/Task/ViewTaskTest.php

@@ -371,33 +371,6 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
- * test bake() with a -plugin param
- *
- * @return void
- */
-	public function testBakeWithPlugin() {
-		$this->markTestIncomplete('Model baking will not work as models do not work.');
-
-		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->plugin = 'TestTest';
-		$this->Task->name = 'View';
-
-		//fake plugin path
-		Plugin::load('TestTest', array('path' => APP . 'Plugin/TestTest/'));
-		$path = APP . 'Plugin/TestTest/View/ViewTaskComments/view.ctp';
-
-		$result = $this->Task->getContent('index');
-		$this->assertNotContains('List Test Test.view Task Articles', $result);
-
-		$this->Task->expects($this->once())
-			->method('createFile')
-			->with($path, $this->anything());
-
-		$this->Task->bake('view', true);
-		Plugin::unload();
-	}
-
-/**
  * test bake actions baking multiple actions.
  *
  * @return void
@@ -468,15 +441,14 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithActionParam() {
-		$this->markTestIncomplete('Model baking will not work as models do not work.');
+		$this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
 		$this->Task->args[0] = 'ViewTaskComments';
 		$this->Task->args[1] = 'view';
 
-		$this->Task->expects($this->once())->method('createFile')
-			->with(
-				TMP . 'ViewTaskComments/view.ctp',
-				$this->anything()
-			);
+		$this->Task->expects($this->once())
+			->method('bake')
+			->with('view', true);
+
 		$this->Task->execute();
 	}
 
@@ -487,20 +459,27 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithController() {
-		$this->markTestIncomplete('Model baking will not work as models do not work.');
+		$this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
 		$this->Task->args[0] = 'ViewTaskComments';
 
-		$this->Task->expects($this->at(0))->method('createFile')
-			->with(
-				TMP . 'ViewTaskComments/index.ctp',
-				$this->anything()
-			);
-		$this->Task->expects($this->at(1))->method('createFile')
-			->with(
-				TMP . 'ViewTaskComments/add.ctp',
-				$this->anything()
-			);
-		$this->Task->expects($this->exactly(2))->method('createFile');
+		$this->Task->expects($this->exactly(4))
+			->method('bake');
+
+		$this->Task->expects($this->at(0))
+			->method('bake')
+			->with('index', $this->anything());
+
+		$this->Task->expects($this->at(1))
+			->method('bake')
+			->with('view', $this->anything());
+
+		$this->Task->expects($this->at(2))
+			->method('bake')
+			->with('add', $this->anything());
+
+		$this->Task->expects($this->at(3))
+			->method('bake')
+			->with('edit', $this->anything());
 
 		$this->Task->execute();
 	}