Browse Source

Add controller() method and various other fixes.

The new method makes it easier to work with controller classnames by
centralizing some logic. It also makes testing much easier.
mark_story 12 years ago
parent
commit
3f2166c57c

+ 47 - 14
src/Console/Command/Task/ViewTask.php

@@ -17,6 +17,7 @@ namespace Cake\Console\Command\Task;
 use Cake\Console\Shell;
 use Cake\Core\App;
 use Cake\Core\Configure;
+use Cake\ORM\Table;
 use Cake\Utility\Inflector;
 
 /**
@@ -47,6 +48,13 @@ class ViewTask extends BakeTask {
 	public $controllerName = null;
 
 /**
+ * Classname of the controller being used
+ *
+ * @var string
+ */
+	public $controllerClass = null;
+
+/**
  * The template file to use
  *
  * @var string
@@ -98,7 +106,7 @@ class ViewTask extends BakeTask {
 		}
 
 		$action = null;
-		$this->controllerName = $this->_controllerName($this->args[0]);
+		$this->controller($this->args[0]);
 
 		$this->Project->interactive = false;
 		if (strtolower($this->args[0]) === 'all') {
@@ -130,14 +138,34 @@ class ViewTask extends BakeTask {
 	}
 
 /**
+ * Set the controller related properties.
+ *
+ * @param string $name The controller name.
+ * @return void
+ */
+	public function controller($name) {
+		$this->controllerName = $name;
+		$plugin = $prefix = null;
+		if (!empty($this->params['plugin'])) {
+			$plugin = $this->params['plugin'] . '.';
+		}
+		if (!empty($this->params['prefix'])) {
+			$prefix = $this->params['prefix'] . '/';
+		}
+		$this->controllerClass = App::className($plugin . $prefix . $name, 'Controller', 'Controller');
+	}
+
+/**
  * Get a list of actions that can / should have views baked for them.
  *
  * @return array Array of action names that should be baked
  */
 	protected function _methodsToBake() {
+		$base = Configure::read('App.namespace');
+
 		$methods = array_diff(
-			array_map('strtolower', get_class_methods($this->controllerName . 'Controller')),
-			array_map('strtolower', get_class_methods('AppController'))
+			array_map('strtolower', get_class_methods($this->controllerClass)),
+			array_map('strtolower', get_class_methods($base . '\Controller\AppController'))
 		);
 		$scaffoldActions = false;
 		if (empty($methods)) {
@@ -154,7 +182,7 @@ class ViewTask extends BakeTask {
 					unset($methods[$i]);
 				}
 			}
-			if ($method[0] === '_' || $method == strtolower($this->controllerName . 'Controller')) {
+			if ($method[0] === '_') {
 				unset($methods[$i]);
 			}
 		}
@@ -177,7 +205,7 @@ class ViewTask extends BakeTask {
 		$this->interactive = false;
 		foreach ($tables as $table) {
 			$model = $this->_modelName($table);
-			$this->controllerName = $this->_controllerName($model);
+			$this->controller($model);
 			if (class_exists($model)) {
 				$vars = $this->_loadController();
 				if (!$actions) {
@@ -262,7 +290,7 @@ class ViewTask extends BakeTask {
 			$plugin = $this->plugin . '.';
 		}
 
-		$controllerClassName = $this->controllerName . 'Controller';
+		$controllerClassName = $this->controllerName;
 		$controllerClassName = App::className($plugin . $controllerClassName, 'Controller');
 
 		if (!class_exists($controllerClassName)) {
@@ -275,15 +303,15 @@ class ViewTask extends BakeTask {
 		$controllerObj->plugin = $this->plugin;
 		$controllerObj->constructClasses();
 		$modelClass = $controllerObj->modelClass;
-		$modelObj = $controllerObj->{$controllerObj->modelClass};
+		$modelObj = $controllerObj->{$modelClass};
 
 		if ($modelObj) {
-			$primaryKey = $modelObj->primaryKey;
-			$displayField = $modelObj->displayField;
+			$primaryKey = $modelObj->primaryKey();
+			$displayField = $modelObj->displayField();
 			$singularVar = Inflector::variable($modelClass);
 			$singularHumanName = $this->_singularHumanName($this->controllerName);
 			$schema = $modelObj->schema();
-			$fields = $schema->fields();
+			$fields = $schema->columns();
 			$associations = $this->_associations($modelObj);
 		} else {
 			$primaryKey = $displayField = null;
@@ -294,8 +322,13 @@ class ViewTask extends BakeTask {
 		$pluralVar = Inflector::variable($this->controllerName);
 		$pluralHumanName = $this->_pluralHumanName($this->controllerName);
 
-		return compact('modelClass', 'schema', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
-				'singularHumanName', 'pluralHumanName', 'fields', 'associations');
+		return compact(
+			'modelClass', 'schema',
+			'primaryKey', 'displayField',
+			'singularVar', 'pluralVar',
+			'singularHumanName', 'pluralHumanName',
+			'fields', 'associations'
+		);
 	}
 
 /**
@@ -459,10 +492,10 @@ class ViewTask extends BakeTask {
 /**
  * Returns associations for controllers models.
  *
- * @param Model $model
+ * @param Table $model
  * @return array $associations
  */
-	protected function _associations(Model $model) {
+	protected function _associations(Table $model) {
 		$keys = ['BelongsTo', 'HasOne', 'HasMany', 'BelongsToMany'];
 		$associations = [];
 

+ 59 - 6
tests/TestCase/Console/Command/Task/ViewTaskTest.php

@@ -28,10 +28,9 @@ use Cake\TestSuite\TestCase;
  */
 class ViewTaskCommentsTable extends Table {
 
-	public function intialize(array $config) {
+	public function initialize(array $config) {
 		$this->table('comments');
 		$this->belongsTo('Articles', [
-			'className' => 'TestTest.ViewTaskArticles',
 			'foreignKey' => 'article_id'
 		]);
 	}
@@ -54,7 +53,7 @@ class ViewTaskArticlesTable extends Table {
  */
 class ViewTaskCommentsController extends Controller {
 
-	public $modelClass = 'Cake\Test\TestCase\Console\Command\ViewTaskCommentsTable';
+	public $modelClass = 'Cake\Test\TestCase\Console\Command\Task\ViewTaskCommentsTable';
 
 /**
  * Testing public controller action
@@ -113,6 +112,8 @@ class ViewTaskTest extends TestCase {
 		$this->Task->path = TMP;
 		$this->Task->Template->params['theme'] = 'default';
 		$this->Task->Template->templatePaths = ['default' => CAKE . 'Console/Templates/default/'];
+
+		Configure::write('App.namespace', 'TestApp');
 	}
 
 /**
@@ -126,6 +127,58 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
+ * Test the controller() method.
+ *
+ * @return void
+ */
+	public function testController() {
+		$this->Task->controller('Comments');
+		$this->assertEquals('Comments', $this->Task->controllerName);
+		$this->assertEquals(
+			'TestApp\Controller\CommentsController',
+			$this->Task->controllerClass
+		);
+	}
+
+/**
+ * Test controller method with plugins.
+ *
+ * @return void
+ */
+	public function testControllerPlugin() {
+		$this->Task->params['plugin'] = 'TestPlugin';
+		$this->Task->controller('TestPlugin');
+		$this->assertEquals('TestPlugin', $this->Task->controllerName);
+		$this->assertEquals(
+			'TestPlugin\Controller\TestPluginController',
+			$this->Task->controllerClass
+		);
+	}
+
+/**
+ * Test controller method with prefixes.
+ *
+ * @return void
+ */
+	public function testControllerPrefix() {
+		$this->Task->params['prefix'] = 'Admin';
+		$this->Task->controller('Posts');
+		$this->assertEquals('Posts', $this->Task->controllerName);
+		$this->assertEquals(
+			'TestApp\Controller\Admin\PostsController',
+			$this->Task->controllerClass
+		);
+
+		$this->Task->params['plugin'] = 'TestPlugin';
+		$this->Task->controller('Comments');
+		$this->assertEquals('Comments', $this->Task->controllerName);
+		$this->assertEquals(
+			'TestPlugin\Controller\Admin\CommentsController',
+			$this->Task->controllerClass
+		);
+	}
+
+/**
  * Test getContent and parsing of Templates.
  *
  * @return void
@@ -197,10 +250,10 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeView() {
-		$this->markTestIncomplete('Model baking will not work as models do not work.');
-		$this->Task->controllerName = 'ViewTaskComments';
+		$this->Task->controllerName = __NAMESPACE__ . '\ViewTaskCommentsController';
 
-		$this->Task->expects($this->at(0))->method('createFile')
+		$this->Task->expects($this->at(0))
+			->method('createFile')
 			->with(
 				TMP . 'ViewTaskComments/view.ctp',
 				$this->stringContains('View Task Articles')