Skip to content

Commit

Permalink
Adding _mergeVars to Shell. This allows $tasks and $uses to work much
Browse files Browse the repository at this point in the history
like $uses in Controllers, and provides consistency in the framework.
Adding tests for merging vars.
  • Loading branch information
markstory committed Nov 21, 2010
1 parent a8554df commit b805355
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cake/console/shells/shell.php
Expand Up @@ -164,6 +164,36 @@ function __construct($stdout = null, $stderr = null, $stdin = null) {
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}
$merge = array();
if ($this->tasks !== null && $this->tasks !== false) {
$merge[] = 'tasks';
}
if ($this->uses !== null && $this->uses !== false) {
$merge[] = 'uses';
}
if (!empty($merge)) {
$this->_mergeVars($merge, get_parent_class($this));
}
}

/**
* Merges this objects $property with the property in $class' definition.
* This classes value for the property will be merged on top of $class'
*
* This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
* this method as an empty function.
*
* @param array $properties The name of the properties to merge.
* @param sting $class The class to merge the property with.
* @return void
*/
protected function _mergeVars($properties, $class) {
$classProperties = get_class_vars($class);
foreach ($properties as $var) {
if (isset($classProperties[$var]) && !empty($classProperties[$var]) && is_array($this->{$var})) {
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
}
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions cake/tests/cases/console/shells/shell.test.php
Expand Up @@ -69,6 +69,20 @@ public function _secret() {
protected function no_access() {

}

public function mergeVars($properties, $class) {
return $this->_mergeVars($properties, $class);
}
}

/**
* Class for testing merging vars
*
* @package cake.tests.cases.console
*/
class TestMergeShell extends Shell {
var $tasks = array('DbConfig', 'Fixture');
var $uses = array('Comment');
}

/**
Expand Down Expand Up @@ -134,6 +148,22 @@ public function testConstruct() {
$this->assertType('ConsoleOutput', $this->Shell->stderr);
}

/**
* test merging vars
*
* @return void
*/
function testMergeVars() {
$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
$this->Shell->uses = array('Posts');
$this->Shell->mergeVars(array('tasks', 'uses'), 'TestMergeShell');

$this->assertEquals(array('DbConfig', 'Fixture', 'DbConfig' => array('one', 'two')), $this->Shell->tasks);
$expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
$this->assertEquals($expected, Set::normalize($this->Shell->tasks), 'Normalized results are wrong.');
$this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
}

/**
* testInitialize method
*
Expand Down

0 comments on commit b805355

Please sign in to comment.