Skip to content

Commit

Permalink
Add controller() method and various other fixes.
Browse files Browse the repository at this point in the history
The new method makes it easier to work with controller classnames by
centralizing some logic. It also makes testing much easier.
  • Loading branch information
markstory committed Mar 25, 2014
1 parent 1d2a648 commit 3f2166c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 20 deletions.
61 changes: 47 additions & 14 deletions src/Console/Command/Task/ViewTask.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Console\Shell;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\ORM\Table;
use Cake\Utility\Inflector;

/**
Expand Down Expand Up @@ -46,6 +47,13 @@ class ViewTask extends BakeTask {
*/
public $controllerName = null;

/**
* Classname of the controller being used
*
* @var string
*/
public $controllerClass = null;

/**
* The template file to use
*
Expand Down Expand Up @@ -98,7 +106,7 @@ public function execute() {
}

$action = null;
$this->controllerName = $this->_controllerName($this->args[0]);
$this->controller($this->args[0]);

$this->Project->interactive = false;
if (strtolower($this->args[0]) === 'all') {
Expand Down Expand Up @@ -129,15 +137,35 @@ public function execute() {
}
}

/**
* 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)) {
Expand All @@ -154,7 +182,7 @@ protected function _methodsToBake() {
unset($methods[$i]);
}
}
if ($method[0] === '_' || $method == strtolower($this->controllerName . 'Controller')) {
if ($method[0] === '_') {
unset($methods[$i]);
}
}
Expand All @@ -177,7 +205,7 @@ public function all() {
$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) {
Expand Down Expand Up @@ -262,7 +290,7 @@ protected function _loadController() {
$plugin = $this->plugin . '.';
}

$controllerClassName = $this->controllerName . 'Controller';
$controllerClassName = $this->controllerName;
$controllerClassName = App::className($plugin . $controllerClassName, 'Controller');

if (!class_exists($controllerClassName)) {
Expand All @@ -275,15 +303,15 @@ protected function _loadController() {
$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;
Expand All @@ -294,8 +322,13 @@ protected function _loadController() {
$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'
);
}

/**
Expand Down Expand Up @@ -459,10 +492,10 @@ public function getOptionParser() {
/**
* 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 = [];

Expand Down
65 changes: 59 additions & 6 deletions tests/TestCase/Console/Command/Task/ViewTaskTest.php
Expand Up @@ -28,10 +28,9 @@
*/
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'
]);
}
Expand All @@ -54,7 +53,7 @@ public function intialize(array $config) {
*/
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
Expand Down Expand Up @@ -113,6 +112,8 @@ public function setUp() {
$this->Task->path = TMP;
$this->Task->Template->params['theme'] = 'default';
$this->Task->Template->templatePaths = ['default' => CAKE . 'Console/Templates/default/'];

Configure::write('App.namespace', 'TestApp');
}

/**
Expand All @@ -125,6 +126,58 @@ public function tearDown() {
unset($this->Task);
}

/**
* 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.
*
Expand Down Expand Up @@ -197,10 +250,10 @@ public function testGetContentWithRoutingPrefix() {
* @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')
Expand Down

0 comments on commit 3f2166c

Please sign in to comment.